Java Quizmaster For Beginners
Java Quizmaster For Beginners
for Beginners
Sar Maroof
Copyright © 2017 Sar Maroof
ISBN: 978-1-975-78178-1
Table of contents
Introduction .................................................................................................................... 4
4
Introduction
T his book is organized to learn Java in 17 days, and it guides you to master Java
code by solving 105 quizzes and 117 assignments. It has already been published
both in English and Dutch.
Any prior background in coding does not require to start with this book. It explains
Java in an easy way with simple examples and many exercises. That makes it ideal for
beginners.
If you have already experience with Java or other programming languages, this book
helps you to enrich your experience by solving many quizzes and executing
assignments.
Read below the explanation of how this book is organized to learn standard Java step
by step in 17 days..
1. This book contains 17 chapters, and each chapter covers a Java topic that
starts with a simple explanation and examples.
2. The next step allows you to solve the quizzes regarding each specific chapter.
For each quiz, there is a step by step explanation of the answer..
3. By each quiz, there are one or more assignments. You will be asked to change
the code or add your own code to the quiz to achieve a specific goal..
4. It is your time from chapter 5 to write your own Java code. You will be asked
to execute a certain assignment and write code from scratch regarding each
chapter.
Sar Maroof
5
DOWNLOAD THE CODE ON : WWW.SARMAROOF.COM
Required knowledge
T his book focuses on learning how to program by practicing with code. There will
be little theoretical explanation – just enough to solve the quizzes. That is a chal-
lenge because it is not easy to write a tiny executable program about only one Java top-
ic. In every executable program, you see different topics. All the programs demonstrate
a particular Java topic. This book divides the Java programming language into some
subjects. In each chapter, a number of small executable programs is offered regards
that specific topic. To make this possible, it is important to be familiar with the follow-
ing basics. It's not required that you understand all the points below properly because
details will be covered later in this book.
The only thing you need to learn to start with is what the role of these points is for
the program to compile and run. Each chapter of this book begins with a brief explana-
tion, after which the quizzes follow. The codes are complete that you can test, compile,
and run.
1. Java editor
We use as Java editor, Eclipse including JDK 7 (Java Development Kit) or higher.
Eclipse is a free Java IDE (integrated development environment). You can find and
download Eclipse on the Internet. The JDK tools are included, which is necessary
when editing Java-programs. The source codes of Java are files with the extension .java.
The compiled bytecode files have the extension .class. Java is a platform-independent
programming language, which is why you can run Java programs on every operating
system.
2. Compiling programs
Compiling is the translation of source code into machine language with the help of a
tool (compiler). After compiling a source code, the program is directly executable.
Java programs contain classes and interfaces. These two concepts are covered in details
later in this book. You only need for now is to learn that a class in Java begins with a
statement as class Myclass. Myclass is the name of the class, and you can decide the
name of the class. Every class in Java is stored in a file with the name of the class. The
class Myclass should be stored in a file with the name of the class and the exten-
sion .java. The name of the file in our example is thus MyClass.java. Each class name
6
Introduction
begins with the keyword class, and each interface name begins with the keyword inter-
face. Classes and interfaces also have members like variables and methods. Methods are
a block code between curly braces.
Example 1
class MyClass
{
// code
}
The following interface must also be stored in a file with the name MyInterface.java.
Example 2
interface MyInterface
{
// code
}
4. Statements
Statements in Java are similar to sentences in natural languages, and they are executable
units. A statement is usually ended with a semicolon (;).
Code in Java is within a start and an end brace. This is called block of code. Below are
some examples of block codes.
7
DOWNLOAD THE CODE ON : WWW.SARMAROOF.COM
}
The keyword void means that the method
doesn't return any values.
Conditional if(x > 3)
{
// code
}
Iteration for( int i=0; i<5; i ++)
{
// code
}
The main method is a method that is needed to execute a Java program. The main
method begins as follows:
For the moment it is important to learn the following about the main method. All exe-
cutable programs must have a main method such as below. The statements within the
main method are executed. The execution of the statements is done from top to bot-
tom. In the next example statement 1 is first executed, then statement 2 and at last
statement 3. The following code writes 253 to the standard output.
Example 3
8
Introduction
The statement System.out.println(); is used to test your program. It is not necessary for
now to understand all the details about this statement, but it is important to know what
you can do with this. You can use this statement to write values of variables and texts
to the standard output as shown in example 4.
Example 4
If you compile and run the code of example 4, the following will be written to the
standard output:
25
99
My name is Emma.
Age: 25 year
9
DOWNLOAD THE CODE ON : WWW.SARMAROOF.COM
To write a text to the standard output, it must be between quotation marks, but for the
value of variables that is not required, see statement 1, 2 and 3 in the previous example.
To write a combination of texts and variables to the standard output, you need to
use the plus (+) sign, see statement 4. To write the texts and the variables on one line
to the standard output you need to use print instead of println.
8. Comments
Comments are ignored by the compiler. Below are two ways to add comments to your
code.
// comment of a single-line
/* here is a comment
of multiple lines */
9. Keywords
There are important keywords in Java that we use in the quiz codes such as static and
public. We use those keywords from the beginning because they help to write small
programs. The above-mentioned keywords will be explained in details later in this
book.
The concept of static is important in Java, and it is treated in a separate chapter. The
only thing you need to learn for now is that this keyword helps to write small executa-
ble programs. That is why we use the keyword static before the name of some of the
variables in the quiz codes. The following example makes this idea clear.
Example 5
10
Introduction
The previous program writes 4 My Java code to the standard output. If you remove
the keyword static in the previous example, you get the following error message:
In order to solve this problem, we need to create an object. Creating objects is ex-
plained in chapter 5. Therefore, we declared the variable x static. This little trick helps
to avoid create objects in the beginning. That helps to create a small executable pro-
gram, and we focus on the subject that matter. The first statement in the main method
writes the value of the variable x to the standard output. The second statement writes
the text My Java code to the standard output. Don't forget that we used here print
instead of println, therefor the values of the variable and the text are written on one
line to the standard output.
Java supports controlling access to the classes and class members by using special key-
words. Using the public keyword for the name of the class or the class members (vari-
ables and methods) indicates that the class or the members are accessible from other
classes. This will be explained later in details.
11
DOWNLOAD THE CODE ON : WWW.SARMAROOF.COM
Java provides a lot of code that can be reused by programmers. It is important for eve-
ry Java programmers to use this free rich library, which is why it is introduced in this
book. For some of the assignments, you need to open this document which helps how
to use the code. You can find Java standard API here:
https://docs.oracle.com/javase/8/docs/api/
Example 6
If this code is compiled and run, the following is written to the standard output.
12
Introduction
Apostrophe : abcde'fghij
Double quotation mark : abcde"fghij
Backslash : abcde\fghij
New line : abcde
fghij
New line 2 : abcdefghij
Tab : abcde fghij
It was written "Parking is not Allowed.".
13
DOWNLOAD THE CODE ON : WWW.SARMAROOF.COM
14
Chapter 1—Data Types &
Variables
T here are 8 primitive data types in Java, which can be declared by programmers.
Those types are: byte, short, int, long, float, double, char and boolean, which
are divided into 4 categories as shown below.
15
DOWNLOAD THE CODE ON : WWW.SARMAROOF.COM
Floating-point Type
float (32) ~ -3.4 x 1038 to ~ 3.4 x 1038
The float type can be used when floating-point
types and values are needed.
Example: float f = 1.4f
double (64) ~ -1.8 x 10308 to ~1.8 x 10308
The double type can be used when floating-point
types and values are needed.
double is a default choice for decimal values.
Example: double d = 22.3;
Character Type
char (16) 0 to 65,535
The char type can be used by character types like a,
b, c, d, $
Characters of type char are always inside single
quotes.
For the type char you can use a unicode character
as 'B' or as a hexadecimal number of '\u0000' to
'\uFFFF'.
Examples:
'\u03A9' = Ω
'\u0045' = E
'\u20AC' = €
Example: char letter = 'd';
Boolean Type
boolean (1) The boolean type has two possible values either
true or false. It is false by default.
Example: boolean bool = true;
16
Chapter 1—Data Types & Variables
Examples
The variable height has the default value of 0, because it is not initialized.
There are three types of variables in Java, namely local variables, instance variables
and class variables.
1. Instance variables: An instance variable is declared within a class, but outside of the
methods, constructors or other blocks.
2. Local variables: A local variable is a variable that is declared within a method,
constructor or a block.
3. Class variables: Class variables are also called static variables. They are declared once
inside a class but outside the methods, constructors or blocks. There is only one
copy of the class variable is available.
Example
17
DOWNLOAD THE CODE ON : WWW.SARMAROOF.COM
void myMethod()
{
char gender = 'm'; // local variable
}
}
a. This code writes "29, 6552, 110.3, false, m" to the standard output.
b. This code writes "29, 6552, 110.3, true, m" to the standard output.
Explanation
All the values of the variables are printed to the standard output.
Since boolean variable "isMarried" is not initialized, its value is by default false.
18
Chapter 1—Data Types & Variables
Assignments
1. Declare a variable with the name isForeigner to know which workers are
foreigners.
2. We assume that the most workers are foreigners.
3. Add a statement to the program to write the value of the variable isForeigner to
the standard output.
4. Change the position of your previous statement in the code to see what happens.
5. Try to assign the new values 45, 298888888, 124.89, to the variables age, bank
account, and wages. What is written to the standard output if the program is
compiled and run?
a. This code writes "80, 0, 3.5, 0.0, 0.0" to the standard output.
b. This code writes "80, 0, 3.5, 0, 0" to the standard output.
19
DOWNLOAD THE CODE ON : WWW.SARMAROOF.COM
c. This code writes "80, 0, 3.5, 0.0, 0" to the standard output.
Explanation
The default value of integers is "0", but the default value of floats and doubles are
"0.0".
Assignments
1. Assign the new values 122, 43.9f, 335.35 to the variables b, f2, d, and execute the
program to see what happens.
2. Declare a character type variable called "myChar".
3. Assign the value "Q" to the variable "myChar".
4. Add a statement to the code to print the value of myChar to the standard output.
5. Change the position of your statement in the code to see what happens.
20
Chapter 1—Data Types & Variables
Explanation
Assignments
1. Declare a variable called "myVar", and assign the value 1344.98 to it.
2. Declare a variable called "myVar2" directly under myVar, and assign the value
"g" to it.
3. Declare a variable called "myVar3" directly under myVar2, and assign the
value 766 to it.
4. Add three statements to the the program to write the values of myVar,
myVar2 and myVar3 to the standard output.
21
DOWNLOAD THE CODE ON : WWW.SARMAROOF.COM
Explanation
i1 = i1 - 3 = 7- 3 = 4. the new value of i1 is 4, and that is why we use this value in the
second equation, i2 = i2 + i1 = 12 + 4 = 16.
Assignments
1. Add the statement "i1 = 9;" directly under the statement "public static void
main(String[] args)".
2. Add the statement "i2 = 8;" directly under the previous statement.
3. What is written to the standard output if you compile and run the program?
22
Chapter 2—Operators
O perators are special symbols that are used to operate on one, two or three oper-
ands and return a result. An example is:
age >= 40
Arithmetic operators
Arithmetic operators are used by working with numbers and mathematical expressions.
Relational operators
Relational operators are used by evaluating two operands for equality. The answer is
either true or false.
23
DOWNLOAD THE CODE ON : WWW.SARMAROOF.COM
Conditional operators
24
Chapter 2—Operators
if(n == 6) {
p = 4;
}
else {
p = 5;
}
Returns 4, because n is equal to 6.
Assignment operators
example 2
25
DOWNLOAD THE CODE ON : WWW.SARMAROOF.COM
int x = 17;
x % 3; means x = de rest van 17/3 = 2.
Unary opeators
The if-block
Example
26
Chapter 2—Operators
if (13 == 6)
{
System.out.print("N");
/*
* This Block is ignored. N is not written to
* the standard output, because 13 is not equal to 6
*/
}
if (12 <= 22)
{
System.out.print("X");
/*
* Writes X to the standard output,
* because 12 is less than 22
*/
}
if (21 > 8 && 3 != 5)
{
System.out.print("U");
/*
* Writes U to the standard output, because
* 21 is greater than 8 and 3 is not equal to 5
*/
}
}
}
27
DOWNLOAD THE CODE ON : WWW.SARMAROOF.COM
double d2 = 3.7;
System.out.print(x / y + ", ");
System.out.print(x * z + ", ");
System.out.print(x + y - z + ", ");
System.out.print(x / y + z * 2 + ", ");
System.out.print(d2 - d);
}
}
a. This code writes "4, 60, 22, 10, 1.5" to the standard output.
b. This code writes "4, 60, 22, 14, 1.5 " to the standard output.
Explanation
x/y = 20/5 = 4;
x*z = 20*3 = 60;
x+y-z = 20+5-3 = 22;
x/y + z*2 = 20/5 + 3*2 = 4 + 6 = 10;
d2- d = 3.7-2.2 = 1.5;
Assignments
What does each of the following three statements write to the standard output if you
add them directly under the statement System.out.println(d2 - d);
Quiz 2: Modulus
28
Chapter 2—Operators
Explanation
Modulus % calculates the remainder of the left-hand operand divided by the right one.
Assignments
What does each of the following three statements write to the standard output if you
add them directly under the statement System.out.println(23 % 6);.
29
DOWNLOAD THE CODE ON : WWW.SARMAROOF.COM
{
public static void main(String[] args)
{
int x = 4;
int y = 6;
x--;
y++;
System.out.print(x + ", " + y);
}
}
Explanation
x = 4 and y = 6.
x-- decrements the value of x by 1.
x = x - 1 = 4 -1 = 3;
y++ increments the value of y by 1.
y =y+1=6+1=7
Assignments
Research the following and execute the program to check out your expectation.
1. Add the statement x++; directly under the statement System.out.print(x + ", "
+ y);.
2. Change the position of the statement x++; directly above the statement
System.out.print(x + ", " + y);.
Does the position of the statement x++; in de code make any difference?
30
Chapter 2—Operators
31
DOWNLOAD THE CODE ON : WWW.SARMAROOF.COM
Explanation
The conditional statement if(x == z) returns true, because both variables are equal to 3.
N will be printed to the standard output.
The conditional statement if(x >= y) returns false because x is not greater or equal to y.
The conditional statement if(x <= z) returns true, because x is equal to z and equal to
3.
The letter P is written to the standard output.
The conditional statement if(z > y) is false because z = 3, but y = 8.
The conditional statement if(y != z) is true because z doesn't equal to y.
The letter R is written to the standard output.
Assignments
1. What is written to the standard output if you make the following changes?
2. Assign a new value 15 to the variable x and add the statement
3. System.out.print("Z"); directly under the statement System.out.print("O");
4. Execute the program to check out your expectation.
32
Chapter 2—Operators
{
System.out.print("O");
}
if (!isDefect)
{
System.out.print("P");
}
}
}
Explanation
The condition if(x < y && x > 1) returns true, because both operands are true.
The condition if(z > y || x > y) returns true, because the operand z > y is true.
|| (conditional or) returns true if one or both operands are true.
The condition if( ! isDefect) returns false, because isDetected is true and the sign !
reverses the value of the boolean.
Assignments
What is written to the standard output if you make the following changes to the
program?
Compile and run the code to check out your expectation.
1. Assign the value false to the variable isDefect.
2. Assign the value 1 to the variable x.
33
DOWNLOAD THE CODE ON : WWW.SARMAROOF.COM
{
public static void main(String[] args)
{
boolean isOld = false;
int x = 5;
int y = 14;
int z = 18;
if (y > x && z > y && (x + 12) >= z)
{
System.out.print("P");
}
if (x >= 6 || z <= y || z <= 18)
{
System.out.print("Q");
}
if (!isOld || y > z)
{
System.out.print("R");
}
}
}
Explanation
The condition if(y > x && z > y && (x + 12) >= z) returns false, because one of the
operands namely (x + 12) >= z returns false.
The condition if(x >= 4 || z <= y || z <= 18) returns true, because one of the
operands return true namely z <= 18.
if( ! isOld || y > z) returns true, because one of the operands namely !isOld is true.
34
Chapter 2—Operators
Assignments
What is written to the standard output if you make the following changes to the
program?
Compile and run the code to check out your expectation.
Explanation
35
DOWNLOAD THE CODE ON : WWW.SARMAROOF.COM
i1 += 4 is equivalent to i1 = i1 + 4 = 3 + 4 = 7
i2 *= 3 is equivalent to i2 = i2 * 3 = 5 * 3 = 15
i3 /= 3 is equivalent to i3 = i3 / 3 = 12 / 3 = 4
i4 -= 12 is equivalent to i4 = i4 - 12 = 20 - 12 = 8
Assignments
What is written to the standard output if you add the following statements to the
program?
Compile and run the code to check out your expectation.
Add the following statements directly under the statement i4 -= 12;.
1. i1 ++ ;
2. i2 -= 3;
3. i3 *= 2;
4. i4 /= 4;
36
Chapter 2—Operators
Explanation
22 %= 6. is equal to 4
17%= 5 is equal to 2
30%= 3 is equal to 0
Assignments
What is written to the standard output if you add the following statements to the
program?
Compile and run the program to check out your expectation.
1. i1 %= 3;
2. i2 %= 7;
37
DOWNLOAD THE CODE ON : WWW.SARMAROOF.COM
}
}
Explanation
Assignments
Assign the value 6 to the variable x and 4 to the variable x2. Wat is written to the
standard output if you compile and run the program?
38