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

11.) In a for loop, where is the initialization step typically placed?

A) Before the loop
B) After the loop
C) Within the loop body
D) Initialization is not required in a for loop.

Answer: Option A

Explanation: The initialization step is placed before the loop and is executed only once, at the beginning of the loop.

12.) Which control statement is used to exit from a loop or switch-case statement prematurely?

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

Answer: Option D

Explanation: The “break” statement is used to exit from a loop or switch-case statement before the normal loop condition or switch-case evaluation is complete.

13.) Which of the following is true about the “default” case in a switch statement?

A) It is mandatory in every switch statement.
B) It can be placed anywhere in the switch statement.
C) It is executed when no other case matches the expression value.
D) It is used to compare the value of the expression.

Answer: Option C

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

14.) The “if-else if-else” ladder allows you to:

A) Execute only the “if” block.
B) Execute any number of “if” blocks.
C) Execute only the first matching block.
D) Execute all the blocks in sequence.

Answer: Option C

Explanation: In an “if-else if-else” ladder, only the block associated with the first condition that evaluates to true will be executed. Subsequent blocks are skipped.

15.) Which loop is best suited for situations where you don’t know in advance how many times the loop needs to execute?

A) for loop
B) while loop
C) do-while loop
D) It’s not possible to determine without additional information.

Answer: Option B

Explanation: The “while” loop is ideal for situations where the exact number of iterations is not known in advance.

16.) What is the primary purpose of the “break” statement in a loop?

A) To exit the program.
B) To skip the current iteration of the loop and continue to the next iteration.
C) To terminate the loop and resume execution after the loop.
D) To compare values.

Answer: Option B

Explanation: The “break” statement is used to prematurely exit a loop, skipping the remaining code in the current iteration and moving to the next iteration.

17.) Which control statement is used to execute a block of code if a certain condition is true and a different block if the condition is false?

A) if-else
B) if-else if-else
C) switch-case
D) for loop

Answer: Option A

Explanation: The “if-else” statement allows you to execute different blocks of code based on whether a specified condition is true or false.

18.) The “switch-case” statement is best suited for:

A) Evaluating complex conditions.
B) Evaluating floating-point numbers.
C) Evaluating a single expression against multiple possible values.
D) Evaluating boolean expressions.

Answer: Option C

Explanation: The “switch-case” statement is used to compare a single expression against multiple possible values and execute code based on the matched value.

19.) Which loop executes the loop body at least once, even if the condition is false?

A) for loop
B) while loop
C) do-while loop
D) switch-case loop

Answer: Option C

Explanation: The “do-while” loop executes the loop body at least once, regardless of whether the condition is true or false.

20.) In a “for” loop, what happens if the update statement is missing?

A) The loop will not execute.
B) The loop will enter an infinite loop.
C) The loop will execute only once.
D) The loop will produce a compilation error.

Answer: Option B

Explanation: If the update statement is missing in a “for” loop, the loop will continue to execute indefinitely, as the loop control variable will not be modified.