Java MCQs – Control statements (if-else, switch-case, loops)

21.) Which of the following statements is true about the “if” statement?

A) The “if” statement can exist without the “else” statement.
B) The “else” statement can exist without the “if” statement.
C) Both “if” and “else” statements are mandatory in any program.
D) The “if” statement is optional and can be omitted.

Answer: Option A

Explanation: The “if” statement can exist on its own, without necessarily requiring an “else” statement.

22.) Which of the following statements is true about the “do-while” loop?

A) It may execute zero or more times.
B) It always executes at least once.
C) It executes a specific number of times.
D) It’s used exclusively for iterating through arrays.

Answer: Option B

Explanation: The “do-while” loop guarantees that its loop body is executed at least once, regardless of the loop condition.

23.) In a switch-case statement, can the same value appear in multiple case blocks?

A) Yes, but only if they are adjacent to each other.
B) No, each value must appear only once in the entire switch-case.
C) Yes, it’s allowed to have the same value in different case blocks.
D) It depends on the specific Java version.

Answer: Option C

Explanation: The same value can appear in different case blocks within a switch-case statement.

24.) Which loop control statement transfers control to the loop’s condition evaluation?

A) continue
B) break
C) goto
D) exit

Answer: Option B

Explanation: The “break” statement transfers control out of the loop, allowing the loop’s condition to be evaluated again.

25.) What is the main purpose of the “break” statement in a switch-case statement?

A) To compare the value of the expression.
B) To terminate the entire program.
C) To skip the current case and proceed to the next case.
D) To exit the loop or switch-case statement.

Answer: Option C

Explanation: The “break” statement is used to terminate the execution of the current case and exit the switch-case statement, moving to the next statement after the switch-case block.

26.) In a “do-while” loop, when is the loop condition evaluated?

A) Before executing the loop body
B) After executing the loop body
C) Before and after executing the loop body
D) The “do-while” loop doesn’t have a condition.

Answer: Option B

Explanation: In a “do-while” loop, the loop condition is evaluated after executing the loop body, ensuring that the loop body is executed at least once.

27.) The __ statement at the end of each block signals the end of a particular case and causes an exit from the switch statement, transferring the control to the statement following the switch.

A) switch
B) break
C) continue
D) default

Answer: Option B

Explanation: The “break” statement is used to terminate the execution of the current case block and exit the switch-case statement, moving to the next statement after the switch-case block.

28.) The __ statement tests the value of a given variable against a list of case values and when a match is found, a block of statements associated with that case is executed.

A) default
B) break
C) continue
D) switch

Answer: Option A

Explanation: The “default” case is executed when none of the other cases match the value of the expression in a switch statement.