nerdexam
Oracle

1Z0-808 · Question #64

What should keyword1 and keyword2 be respectively, in order to produce output 2345? int [] array = {1,2,3,4,5}; for (int i: array) { if (i < 2) { keyword1 ; } System.out.println(i); if (i == 3) {…

The correct answer is D. continue, continue. Option D works because continue as keyword1 skips System.out.println(i) when i < 2 (i.e., when i=1), preventing 1 from printing while allowing the loop to continue through 2, 3, 4, and 5. Using continue as keyword2 when i == 3 has no practical effect here since there is no code…

Using Loop Constructs

Question

What should keyword1 and keyword2 be respectively, in order to produce output 2345? int [] array = {1,2,3,4,5}; for (int i: array) { if (i < 2) { keyword1 ; } System.out.println(i); if (i == 3) { keyword2 ; } }

Options

  • Acontinue, break
  • Bbreak, break
  • Cbreak, continue
  • Dcontinue, continue

How the community answered

(41 responses)
  • A
    5% (2)
  • B
    2% (1)
  • C
    15% (6)
  • D
    78% (32)

Explanation

Option D works because continue as keyword1 skips System.out.println(i) when i < 2 (i.e., when i=1), preventing 1 from printing while allowing the loop to continue through 2, 3, 4, and 5. Using continue as keyword2 when i == 3 has no practical effect here since there is no code after that second if block - the loop simply advances to the next iteration naturally, printing 4 and 5 as expected.

Why the distractors fail:

  • A (continue, break): keyword1=continue correctly skips 1, but keyword2=break exits the entire loop at i == 3, producing only 23.
  • B (break, break): keyword1=break exits the loop immediately on the first iteration (i=1), producing no output at all.
  • C (break, continue): Same problem as B - keyword1=break kills the loop at i=1 before any output.

Memory tip: Think of break as breaking out of the room (loop ends entirely) and continue as skipping your turn (current iteration ends, but the loop keeps going). Any time you see break as keyword1 here, 1 never prints and neither does anything else.

Topics

#continue keyword#break keyword#loop control flow#for-each loops

Community Discussion

No community discussion yet for this question.

Full 1Z0-808 Practice