SCJP Flow Control (Loops, Branching, and Labeled Statements) — Questions and Answers
Question 1: What is the output of the following code? int x = 5; do { System.out.print(x + " "); x--; } while (x > 5);
- 5 (Correct answer)
- No output is produced
- 5 4 3 2 1
- Compilation error
Correct answer: 5
A do-while loop always executes its body at least once before checking the condition. Here x=5 is printed, then x becomes 4, and 4>5 is false so the loop exits. Output is '5'.
Question 2: Which of the following data types can be used as a switch expression in Java SE 6?
- long
- String
- int (Correct answer)
- boolean
Correct answer: int
In Java SE 6, switch expressions are limited to byte, short, char, and int (and their wrappers, plus enum). String support was added in Java 7. long and boolean are never valid switch types.
Question 3: What does the following code print? outer: for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (j == 1) break outer; System.out.print(i + "" + j + " "); } } System.out.print("done");
- 00 done (Correct answer)
- 00 10 20 done
- 00 01 02 done
- done
Correct answer: 00 done
`break outer` terminates the entire outer for-loop when j==1. The only iteration that prints before that happens is i=0, j=0, producing '00'. Then j becomes 1, break outer fires, and execution jumps to the `System.out.print("done")` line.
Question 4: Which statement about the enhanced for-loop (for-each) is TRUE?
- It can be used to modify the elements of a primitive array in place
- It requires the collection to implement java.lang.Iterable (Correct answer)
- It maintains an explicit index variable you can access
- It can iterate over a Map directly without calling entrySet()
Correct answer: It requires the collection to implement java.lang.Iterable
The enhanced for-loop works with arrays and any object that implements java.lang.Iterable. It provides no index variable, cannot structurally modify the underlying collection, and Map does not implement Iterable so you must use keySet(), values(), or entrySet().
Question 5: What happens when the following code is compiled? void test() { return; System.out.println("hello"); }
- It compiles and 'hello' is never printed
- It compiles and throws a RuntimeException
- It fails to compile due to unreachable code (Correct answer)
- It prints 'hello' because println is always executed
Correct answer: It fails to compile due to unreachable code
The Java compiler detects unreachable statements and reports a compile-time error. The `System.out.println` after an unconditional `return` can never execute, so the class will not compile.
Question 6: What is the output of the following code? for (int i = 0; i < 5; i++) { if (i % 2 == 0) continue; System.out.print(i + " "); }
- 0 2 4
- 1 3 (Correct answer)
- 0 1 2 3 4
- 1 2 3 4
Correct answer: 1 3
`continue` skips the rest of the loop body for that iteration and moves to the next. When i is even (0, 2, 4), `continue` fires and the print is skipped. When i is odd (1, 3), the print executes. Output is '1 3'.
What is the output of the following code?
int x = 5;
do {
System.out.print(x + " ");
x--;
} while (x > 5);