Here are 10 essential multiple-choice questions on Java Control Statements and Loops, covering key concepts.
Question 1
What will be the output of the following code?
int x = 5;
if (x > 2)
if (x > 4)
System.out.println("A");
else
System.out.println("B");
A
B
Compilation Error
No Output
Question 2
Which of the following is NOT a valid control statement in Java?
if
do-while
goto
switch
Question 3
What will be the output of the following loop?
for (int i = 0; i < 3; i++) {
System.out.print(i + " ");
}
0 1 2
1 2 3
0 1 2 3
Infinite Loop
Question 4
How many times will the following loop execute?
int i = 1;
while (i <= 5) {
System.out.println("Hello");
}
5 times
6 times
Infinite times
Compilation Error
Question 5
What is the output of the following Java code?
for (int i = 0; i < 5; i++) {
if (i == 2)
break;
System.out.print(i + " ");
}
0 1 2 3 4
0 1
0 1 2
No Output
Question 6
What will be the output of this code?
for (int i = 0; i < 5; i++) {
if (i == 3)
continue;
System.out.print(i + " ");
}
0 1 2 3 4
0 1 2 4
0 1 2
Compilation Error
Question 7
What will happen if the condition in a for loop is omitted?
for (int i = 0; ; i++) {
System.out.println(i);
}
The loop executes indefinitely
Compilation Error
Runs once and terminates
Throws an exception
Question 8
What is the correct syntax for a do-while loop in Java?
do { } while (condition);
while (condition) { }
do (condition) { }
while { condition };
Question 9
What will be the output of the following nested loop?
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 2; j++) {
System.out.print(i + "," + j + " ");
}
}
1,1 1,2 2,1 2,2 3,1 3,2
1,1 2,1 3,1 1,2 2,2 3,2
1,1 1,2 1,3 2,1 2,2 2,3
Compilation Error
Question 10
What will be the output of the following while loop?
int x = 10;
while (x > 0) {
x -= 3;
System.out.print(x + " ");
}
7 4 1 -2
10 7 4 1
10 7 4 1 -2 -5
Infinite loop
There are 10 questions to complete.