nerdexam
Oracle

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.

Using Loop Constructs

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)
  • A
    82% (45)
  • B
    9% (5)
  • C
    4% (2)
  • D
    5% (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.

Aii = 0Correct

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`.

Bii = 0

Option B is identical to A, indicating a likely duplicate choice or formatting error, but technically it represents the same first output line.

Cii =

The output `ii =` implies that the value of `ii` was not concatenated, which contradicts the `"+ ii"` part of the print statement.

DCompilation fails.

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

#for loop#loop iteration#code tracing

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice