wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Loops Quiz

Total questions: 16

Worksheet time: 1hrs 8mins

Name
Class
Date
1.
How many times will the following code print "Welcome to Java"?
int count = 0;
while (count < 10) {
 System.out.println("Welcome to Java");
 count++;
}
a)
8
b)
9
c)
10
d)
0
2.
What is the output of the following code?
int x = 0;
while (x < 4) {
 x = x + 1;
}
System.out.println("x is " + x);
a)
0
b)
1
c)
3
d)
4
3.
 Analyze the following code.
int count = 0;
while (count < 100) {
 // Point A
 System.out.println("Welcome to Java!");
 count++;
 // Point B
}
 // Point C
a)
count < 100 is always true at Point B 
b)
 count < 100 is always false at Point B
c)
 count < 100 is always true at Point A  and count < 100 is always false at Point C
d)
 count < 100 is always true at Point Coint C
4.
How many times will the following code print "Welcome to Java"?
int count = 0;
do {
 System.out.println("Welcome to Java");
 count++;
} while (count < 10);
a)
0
b)
9
c)
8
d)
10
5.
Which of the following loops prints "Welcome to Java" 10 times?
a)
A:
for (int count = 1; count <= 10; count++) {
 System.out.println("Welcome to Java");
b)
for (int count = 2; count < 10; count++) {
 System.out.println("Welcome to Java");
}
c)
for (int count = 1; count < 10; count++) {
 System.out.println("Welcome to Java");
}
d)
for (int count = 0; count <= 10; count++) {
 System.out.println("Welcome to Java");
}
6.
The following loop displays _______________.
for (int i = 1; i <= 10; i++) {
 System.out.print(i + " ");
 i++;
}
a)
1 2 3 4 5 6 7 8 9
b)
1 2 3 4 5 6 7 8 9 10
c)
1 2 3 4 5 
d)
1 3 5 7 9
7.
What is the output for y?
int y = 0;
for (int i = 0; i<10; ++i) {
 y += i;
}
System.out.println(y);
a)
1
b)
36
c)
45
d)
9
8.
How many times is the println statement executed?
for (int i = 0; i < 10; i++) 
 for (int j = 0; j < 10; j++)
 System.out.println(i * j);
a)
100
b)
10
c)
45
d)
20
9.
What is the number of iterations in the following loop:
 for (int i = 1; i < n; i++) {
 // iteration
 }
a)
2*n
b)
 n
c)
n - 1
d)
n + 1
10.
What will be displayed when the following code is executed?
 int number = 6;
 while (number > 0) {
 number -= 3;
 System.out.print(number + " ");
 }
a)
6,3,0
b)
6,3
c)
3,0
d)
0
11.

What loop should you use if you know how many times you are going to loop?

a)

For loop

b)

Do while loop

c)

While loop

d)

No loop

12.

for(int i; i<10; i++) {} Why won’t this code segment run?

a)

There is a missing semicolon at the end.

b)

For should be capitalized

c)

The person did not set the beginning value of i.

d)

The person did not set the ending value of i.

13.

Which of the following includes the correct syntax for a for loop?

a)

for(int i = 0, i<10, i++)

b)

for(int i =0, run = 10)

c)

for(int i = 0; run = 10)

d)

for(int i = 0; i < 10; i++)

14.

What is output by the code? Pretend it says System.out.println

a)

2

b)

3

c)

4

d)

5

e)

6

15.

What is ouptut by the code? Pretend it says System.out.println

a)

2

b)

3

c)

4

d)

5

e)

6

16.

What is output by the code?

a)

123

b)

12

c)

1234

d)

12345

e)

4321