Oracle
1Z0-803 · Question #256
Given: public class Natural { private int i; void disp() { while (i <= 5) { for (int i=1; i <=5;) { System.out.print(i + " "); i++; } i++; } } public static void main(String[] args) { new Natural().di
The correct answer is D. Prints 1 2 3 4 5 six times. 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
Using Loop Constructs
Question
Given:
public class Natural { private int i; void disp() { while (i <= 5) { for (int i=1; i <=5;) { System.out.print(i + " "); i++; } i++; } } public static void main(String[] args) { new Natural().disp(); } } What is the result?
Options
- APrints 1 2 3 4 5 once
- BPrints 1 3 5 once
- CPrints 1 2 3 4 5 five times
- DPrints 1 2 3 4 5 six times
- ECompilation fails
How the community answered
(26 responses)- A12% (3)
- B23% (6)
- C4% (1)
- D58% (15)
- E4% (1)
Explanation
1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
Topics
#loop constructs#nested loops#variable scope#variable shadowing
Community Discussion
No community discussion yet for this question.