What will be the output of the code after the given code executes? int a = 5;…
2024
What will be the output of the code after the given code executes? int a = 5; int b = a++; System.out.println("a=" + a + " b=" + b);
Answer: A. a = 6 b = 5 — Step 1: The variable a is initialized to 5. Step 2: The statement b = a++ uses the post-increment operator. This means the current value of a (5) is assigned…
- A.
a = 6 b = 5
- B.
a = 5 b = 6
- C.
a = 6 b = 6
- D.
a = 5 b = 5
- E.
Question not attempted
Attempted by 331 students.
Show answer & explanation
Correct answer: A
Step 1: The variable a is initialized to 5.
Step 2: The statement b = a++ uses the post-increment operator. This means the current value of a (5) is assigned to b, and then a is incremented by 1, so a becomes 6.
Step 3: The print statement System.out.println("a=" + a + " b=" + b); outputs the values of a and b. Since a is now 6 and b is 5, the output is "a=6 b=5".