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…
Question
Options
- Acontinue, break
- Bbreak, break
- Cbreak, continue
- Dcontinue, continue
How the community answered
(41 responses)- A5% (2)
- B2% (1)
- C15% (6)
- D78% (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=
continuecorrectly skips1, but keyword2=breakexits the entire loop ati == 3, producing only23. - B (break, break): keyword1=
breakexits the loop immediately on the first iteration (i=1), producing no output at all. - C (break, continue): Same problem as B - keyword1=
breakkills the loop ati=1before 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
Community Discussion
No community discussion yet for this question.