nerdexam
Oracle

1Z0-811 · Question #21

Given the code fragment: String[] codes = {"CA", "JP", "US", "CA", "UK"}; int count = 0; for (String c : codes) { if (c.equals("CA")) { continue; } else { count++; } } System.out.println (count)…

The correct answer is A. 3. Option A (3) is correct because continue skips the rest of the loop body for elements equal to "CA", so the two "CA" entries never reach count++. The remaining three elements - "JP", "US", and "UK" - each fall into the else branch and increment count once each, yielding 3…

Control Flow

Question

Given the code fragment: String[] codes = {"CA", "JP", "US", "CA", "UK"}; int count = 0; for (String c : codes) { if (c.equals("CA")) { continue; } else { count++; } } System.out.println (count); What is the result?

Options

  • A3
  • BA compilation error occurs.
  • C2
  • D0

How the community answered

(36 responses)
  • A
    72% (26)
  • B
    3% (1)
  • C
    8% (3)
  • D
    17% (6)

Explanation

Option A (3) is correct because continue skips the rest of the loop body for elements equal to "CA", so the two "CA" entries never reach count++. The remaining three elements - "JP", "US", and "UK" - each fall into the else branch and increment count once each, yielding 3. Option C (2) is a common trap: test-takers may mistakenly count only the distinct non-CA values or miscount the iterations. Option D (0) would only result if continue somehow blocked all increments, which it does not - it only skips iterations where the condition is true. Option B is wrong because the code is syntactically and semantically valid; continue is perfectly legal inside a for-each loop.

Memory tip: Think of continue as "skip this one and move on" - it skips only the current iteration, not the whole loop. Count how many elements do not match the continue condition, and that's your answer.

Topics

#continue statement#loop control flow#String.equals()#counter logic

Community Discussion

No community discussion yet for this question.

Full 1Z0-811 Practice