NEW
Font size
S
M
L
XL
WorksheetsLoops
Total questions: 10
Worksheet time: 46mins
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++;
}
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);
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
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);
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");
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");
}
System.out.println("Welcome to Java");
}
c)
for (int count = 1; count < 10; count++) {
System.out.println("Welcome to Java");
}
System.out.println("Welcome to Java");
}
d)
for (int count = 0; count <= 10; count++) {
System.out.println("Welcome to Java");
}
System.out.println("Welcome to Java");
}
6.
The following loop displays _______________.
for (int i = 1; i <= 10; i++) {
System.out.print(i + " ");
i++;
}
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);
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);
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
}
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 + " ");
}
int number = 6;
while (number > 0) {
number -= 3;
System.out.print(number + " ");
}
a)
6,3,0
b)
6,3
c)
3,0
d)
0
Reset
