menu

Java Control Statements


1.

What is the output of the following program?

public class Main {
public static void main(String[] args) {
int i = 0;
while (i < 10) {
i++;
if (i % 2 == 0) {
continue;
}
System.out.print(i + " ");
}
}
}

2 4 6 8 10

1 3 5 7 9

2 4 6 8

1 3 5 7


2. Which control flow statement is used to skip the remaining statements in the current iteration of a loop and move on to the next iteration?

continue

break

return

skip


3. Which of the following is not a valid loop control statement in Java?

continue

break

return

goto


4. Which of the following statements is true about nested loops in Java?

Nested loops can only be used with for loops, not with while or do-while loops.

A break statement inside a nested loop terminates only the innermost loop.

A continue statement inside a nested loop skips only the innermost loop.

Nested loops are never used in real-world programming.


5. What is the purpose of the labeled statement in Java?

To define a new variable in the code.

To provide a descriptive name for a loop or if statement.

To mark a specific point in the code that can be jumped to using the goto statement.

To create a custom exception class.


6. Which of the following is not a valid relational operator in Java?

==

!=

<=

><


7. What is the difference between a switch statement and a series of if-else statements in Java?

A switch statement can only test for equality, while if-else statements can test for any condition.

A switch statement can test for any condition, while if-else statements can only test for equality.

A switch statement is more efficient than a series of if-else statements for testing a single variable against multiple values.

There is no difference between a switch statement and a series of if-else statements.


8. Which control flow statement is used to execute a set of statements at least once?

while loop

do-while loop

for loop

if statement


9.

What is the output of the following program?

public class Main {
public static void main(String[] args) {
int i = 0;
do {
System.out.print(i + " ");
i++;
} while(i < 5);
}
}

0 1 2 3 4

1 2 3 4 5

0 1 2 3 4 5

None of the above


10.

Which control flow statement is used to exit from a loop?

continue

break

return

exit