Courseiva
Primitives, Strings and OperatorsmediumMultiple ChoiceObjective-mapped

1Z0-811 Primitives, Strings and Operators Practice Question

What is the output of the following code? int i = 0; i = i++ + ++i; System.out.println(i);

⚠ Common exam trap

The trap here is that candidates often misapply operator precedence or confuse the order of evaluation with the order of side effects, specifically forgetting that post-increment returns the original value before the increment, while pre-increment returns the value after the increment.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

2

The expression `i = i++ + ++i` evaluates as follows: initially `i = 0`. In `i++`, the post-increment operator returns the current value (0) and then increments `i` to 1. Then `++i` pre-increments `i` from 1 to 2 and returns 2. The sum is 0 + 2 = 2, which is assigned to `i`, overwriting the intermediate increments. Thus, the final output is 2.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • 3

    Why it's wrong here

    Incorrect evaluation order.

  • 0

    Why it's wrong here

    Incorrect evaluation order.

  • 1

    Why it's wrong here

    Incorrect evaluation order.

  • 2

    Why this is correct

    The output is 2 because Java evaluates the right-hand side of the assignment `i = i++ + ++i;` from left to right. First, `i++` uses the current value of `i` (0) for the sum, then increments `i` to 1. Next, `++i` pre-increments `i` to 2, then uses this new value (2) for the sum. The addition becomes `0 + 2`, resulting in 2, which is then assigned back to `i`. This demonstrates the precise order of operator precedence and side effects in Java's expression evaluation.

About these practice questions

One of 481 original 1Z0-811 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This 1Z0-811 practice question is part of Courseiva's free Oracle certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the 1Z0-811 exam.