1Z0-803 · Question #178
Given: What is the result?
The correct answer is A. 2 4 6 8. The program iterates through numbers from 1 up to (but not including) 10 and conditionally prints only the even numbers encountered within that range.
Question
Given:
What is the result?
Options
- A2 4 6 8
- B2 4 6 8 9
- C1 3 5 7
- D1 3 5 7 9
How the community answered
(25 responses)- A76% (19)
- B8% (2)
- C4% (1)
- D12% (3)
Why each option
The program iterates through numbers from 1 up to (but not including) 10 and conditionally prints only the even numbers encountered within that range.
The loop initializes 'i' to 1 and continues as long as 'i' is less than 10. Inside the loop, an 'if' condition checks if 'i' is an even number using the modulo operator ('i % 2 == 0'), printing only '2', '4', '6', and '8' before the loop terminates.
This choice includes '9', which is an odd number and would not be printed by the 'if (i % 2 == 0)' condition.
This choice includes only odd numbers, which are excluded by the 'if (i % 2 == 0)' condition.
This choice includes only odd numbers, which are excluded by the 'if (i % 2 == 0)' condition, and also includes '9' which is odd.
Concept tested: Loop iteration with conditional logic
Source: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html
Topics
Community Discussion
No community discussion yet for this question.