menu

Java Control Statements

Java Control Statements - Important Points


16.

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

A. 0 1 2 3 4

B. 1 2 3 4 5

C. 0 1 2 3 4 5

D. None of the above

Discuss Work Space

Answer: option a

Explanation:

The code initializes the variable i to 0, then enters a do-while loop that runs at least once and continues to run as long as i is less than 5. The loop body prints the value of i and increments it by 1. This loop will run 5 times, printing the numbers 0 through 4.

17.

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

A. 2 4 6 8 10

B. 1 3 5 7 9

C. 2 4 6 8

D. 1 3 5 7

Discuss Work Space

Answer: option b

Explanation:

The continue statement skips the remaining statements in the current iteration of the loop if the condition is true. In this code, the continue statement is executed if i % 2 == 0, which skips even numbers.

18.

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

A. 0 1 2 3 4

B. 1 2 3 4 5

C. 0 1 2 3 4 5

D. The code will result in an infinite loop.

Discuss Work Space

Answer: option a

Explanation:

The do-while loop in this code executes the statement block at least once, and then checks the condition at the end of the loop. The output of the code will be 0 1 2 3 4.


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

Unlock Your Future

Join Now
Worried for Placements in 2025?

Join FAST TRACK Course

Join Now
Supercharge Your SUCCESS

Join All in One Placement Mock Tests-2025

Join Now