← All SCJP Flashcard Decks

Flow Control (Loops, Branching, and Labeled Statements) Flashcards

6 cards from real SCJP practice questions. Tap to flip, then mark Knew It or Still Learning — missed cards come back until you master them.

Read the first 6 Flow Control (Loops, Branching, and Labeled Statements) flashcards as text
  1. What is the output of the following code? int x = 5; do { System.out.print(x + " "); x--; } while (x > 5);

    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'.

  2. Which of the following data types can be used as a switch expression in Java SE 6?

    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.

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

    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.

  4. Which statement about the enhanced for-loop (for-each) is TRUE?

    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().

  5. What happens when the following code is compiled? void test() { return; System.out.println("hello"); }

    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.

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

    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'.

How this page was made: written with AI tools and published by Nathan Beja, founder of Practice Test Geeks, who is responsible for its content. We have not yet matched this exam's facts to an official source, so confirm fees, dates and rules with the exam body. Not reviewed by a subject-matter expert. Editorial policy