Course: Easy-to-follow Java programming
The quiz questions
Answer the questions below to review what you have learned in Video 2. You will
find the right answers after the questions in the solution part.
1. Put the different parts of the while loop into the right order.
product *= number;
number++;
) {
while
number < 9
2. Put the different parts of the do-while loop into the right order.
number < 9
product *= number;
number++;
while
do
Duckademy IT courses – www.duckademy.com
3. In what order do the different parts of the for loop run?
(The question is not in what order do we put the different parts into the heading
of the for loop, but in what order will they run.)
Loop body
Initialization part (e.g. int i = 0)
Increment part (e.g. i++)
Condition part (e.g. i < 10)
4. With which commands do we use break together?
a) for
b) while
c) do-while
d) if
e) else
f) switch
g) Int
5. Which command will the program execute after continue? What is the
number of the comment before that command?
int i;
/*1*/ System.out.println("before loop");
for (/*2*/ i = 1; /*3*/ i <= 5; /*4*/ i++) {
/*5*/ System.out.println("before");
if (i == 3) {
continue;
}
/*6*/System.out.println("after");
}
/*7*/System.out.println("at the end");
Duckademy IT courses – www.duckademy.com
6. What will the following program print?
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if ((i+j) % 2 == 0) {
System.out.print("%");
} else {
System.out.print("@");
}
}
System.out.println();
}
a) %@%
@%@
%@%
b) @%@
%@%
@%@
c) %%%
@@@
%%%
d) %@%
%@%
%@%
e) %@%
f) %
@
%
Duckademy IT courses – www.duckademy.com
----------------------------------------------------------------------------------------------------------------
The answers
1. Put the different parts of the while loop into the right order.
5 product *= number;
number++;
4 ) {
2 (
1 while
3 number < 9
6 }
while
(
number < 9
) {
product *= number;
number ++;
}
2. Put the different parts of the do-while loop into the right command.
7 number < 9
3 product *= number;
number++;
4 }
2 {
5 while
6 (
1 do
8 )
9 ;
Duckademy IT courses – www.duckademy.com
do
{
product *= number;
number++;
}
while
(
number < 9
)
;
3. In what order do the different parts of the for loop run?
(The question is not in what order do we put the different parts into the heading
of the for loop, but in what order will they run.)
3 Loop body
1 Initialization part (e.g. int i = 0)
4 Increment part (e.g. i++)
2 Condition part (e.g. i < 10)
4. With which commands do we use break together?
a) for
b) while
c) do-while
d) if
e) else
f) switch
g) Int
Duckademy IT courses – www.duckademy.com
5. Which command will the program execute after continue? What is the
number of the comment before that command?
int i;
/*1*/ System.out.println("before loop");
for (/*2*/ i = 1; /*3*/ i <= 5; /*4*/ i++) {
/*5*/ System.out.println("before");
if (i == 3) {
continue;
}
/*6*/System.out.println("after");
}
/*7*/System.out.println("at the end");
6. What will the following program print?
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if ((i+j) % 2 == 0) {
System.out.print("%");
} else {
System.out.print("@");
}
}
System.out.println();
}
a) %@%
@%@
%@%
b) @%@
%@%
@%@
c) %%%
@@@
%%%
d) %@%
%@%
%@%
e) %@%
Duckademy IT courses – www.duckademy.com
f) %
@
%
Duckademy IT courses – www.duckademy.com