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); } }
- A.
12
- B.
11
- C.
10
- D.
Compile time error
- 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.