menu

Java Control Statements


1. Which of the following is true regarding the break statement in Java?

It terminates the current loop or switch statement.

It skips the remaining statements in the current iteration of the loop.

It jumps to a specific label in the code.

It creates a new loop or switch statement.


2.

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

The code will result in an infinite loop.


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

==

!=

<=

><


4. Which control flow statement is used to jump to a specific label in the code?

continue

break

return

goto


5.

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


6. 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.


7.

What is the output of the following program?

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

5 6 7 8 9

6 7 8 9 10

5 6 7 8 9 10

None of the above


8.

What is the output of the following program?

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

1 2 3 4 5

2 3 4 5 6

0 1 2 3 4

None of the above


9. 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.


10. Which of the following is true regarding the default case in a switch statement?

It is optional, and is executed if none of the cases match the tested value.

It is mandatory, and must be the last case in the switch statement.

It is executed if the tested value matches any of the cases in the switch statement.

It is used to declare a default value for a variable.