What is the output of the following code? public class Demo { public static…

2024

What is the output of the following code? public class Demo { public static void main(String[] args) { int x = 5; System.out.println(++x * 2); } }

  1. A.

    12

  2. B.

    11

  3. C.

    10

  4. D.

    Compile time error

  5. E.

    Question not attempted

Attempted by 285 students.

Show answer & explanation

Correct answer: A

Step 1: The variable x is initialized to 5.

Step 2: The prefix increment operator (++x) increases the value of x by 1 before it is used in the expression. So, x becomes 6.

Step 3: The expression becomes 6 * 2, which evaluates to 12.

Step 4: The System.out.println statement prints the result, which is 12.

Explore the full course: Up Lt Grade Assistant Teacher 2025