1Z0-803 · Question #70
Given: public class MyFor { public static void main(String[] args) { for (int ii = 0; ii < 4; ii++) { System.out.println("ii = "+ ii); ii = ii +1; } } } What is the result?
The correct answer is A. ii = 0. The code initializes a loop counter ii to 0, and the first iteration prints its initial value before ii is incremented twice within the loop's execution.
Question
Given:
public class MyFor { public static void main(String[] args) { for (int ii = 0; ii < 4; ii++) { System.out.println("ii = "+ ii); ii = ii +1; } } } What is the result?
Options
- Aii = 0
- Bii = 0
- Cii =
- DCompilation fails.
How the community answered
(55 responses)- A82% (45)
- B9% (5)
- C4% (2)
- D5% (3)
Why each option
The code initializes a loop counter `ii` to 0, and the first iteration prints its initial value before `ii` is incremented twice within the loop's execution.
In the first iteration of the `for` loop, the variable `ii` is initialized to 0. The `System.out.println` statement executes immediately, printing "ii = 0" before the `ii = ii + 1` statement and the `ii++` increment in the loop header modify `ii`.
Option B is identical to A, indicating a likely duplicate choice or formatting error, but technically it represents the same first output line.
The output `ii =` implies that the value of `ii` was not concatenated, which contradicts the `"+ ii"` part of the print statement.
The code has no syntax errors and compiles successfully, as it's a valid Java `for` loop structure.
Concept tested: Java for loop execution flow
Source: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html
Topics
Community Discussion
No community discussion yet for this question.