1Z0-803 · Question #79
What should keyword1 and keyword2 be respectively, in oreder 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) { k
The correct answer is D. continue, continue. This question requires determining the correct continue or break keywords to modify loop behavior and produce the specified output 2345.
Question
What should keyword1 and keyword2 be respectively, in oreder 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
(37 responses)- A3% (1)
- B11% (4)
- C8% (3)
- D78% (29)
Why each option
This question requires determining the correct `continue` or `break` keywords to modify loop behavior and produce the specified output `2345`.
If `keyword2` were `break`, the loop would terminate when `i` equals 3, meaning 4 and 5 would not be printed.
If `keyword1` were `break`, the loop would terminate when `i` equals 1, preventing any numbers from being printed after 1. If `keyword2` were `break`, the loop would terminate when `i` equals 3, preventing 4 and 5 from being printed.
If `keyword1` were `break`, the loop would terminate when `i` equals 1, causing nothing to be printed after the first element.
When `i` is 1, `i < 2` is true, so `keyword1` (`continue`) executes, skipping `System.out.println(i)` for `i=1` and moving to the next iteration. When `i` is 3, `i == 3` is true, so `keyword2` (`continue`) executes. Since there are no more statements after this block in the current iteration, it simply proceeds to the next iteration, allowing `System.out.println(i)` to execute for `i=3, 4, 5`.
Concept tested: Java loop control statements - continue and break
Topics
Community Discussion
No community discussion yet for this question.