menu

Java Control Statements

Java Control Statements - Important Points


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

A. It terminates the current loop or switch statement.

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

C. It jumps to a specific label in the code.

D. It creates a new loop or switch statement.

Discuss Work Space

Answer: option a

Explanation:

The break statement in Java is used to terminate the current loop or switch statement.

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

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

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

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

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

Discuss Work Space

Answer: option a

Explanation:

The default case in a switch statement is optional, and is executed if none of the cases match the tested value.

13.

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++;
  }
 }
}

A. 5 6 7 8 9

B. 6 7 8 9 10

C. 5 6 7 8 9 10

D. None of the above

Discuss Work Space

Answer: option a

Explanation:

The code initializes the variable i to 5, then enters a while loop that checks if i is less than 10. The loop body prints the value of i and increments it by 1. This loop will run until i is no longer less than 10, printing the numbers 5 through 9.

14.

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 + " ");
  }
 }
}

A. 1 2 3 4 5

B. 2 3 4 5 6

C. 0 1 2 3 4

D. None of the above

Discuss Work Space

Answer: option a

Explanation:

The code initializes the variable i to 1, then enters a for loop that runs as long as i is less than or equal to 5. The loop body prints the value of i and increments it by 1. This loop will run 5 times, printing the numbers 1 through 5.

15.

What is the output of the following program?

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

A. 1 3 5 7 9

B. 0 2 4 6 8

C. 1 2 3 4 5 6 7 8 9 10

D. None of the above

Discuss Work Space

Answer: option a

Explanation:

The code initializes the variable i to 0, then enters a for loop that runs as long as i is less than 10. The loop body checks if i is even, and if so, it continues to the next iteration without printing anything. Otherwise, it prints the value of i. This loop will print the odd numbers from 1 to 9.


Subscribe for Latest Career Trends
Subscribe Now
Use AI and ChatGPT for Career Guidance

Unlock Your Future

Join Now
Worried for Placements in 2024?

Join FAST TRACK Course

Join Now
Supercharge Your SUCCESS

Join All in One Placement Mock Tests-2024

Join Now