Given the code fragment:
int b = 4;
b — ;
System.out.println (– b);
System.out.println(b);
What is the result?
A.
2
2
B.
2
1
C.
3
2
D.
3
3
Explanation:
Variable b is set to 4.
Variable b is decreased to 3.
Variable b is decreased to 2 and then printed. Output: 2
Variable b is printed. Output: 2
A
The Answer is A. The pre- and post-fix decrement operators should have been written –b and b–, respectively. The pre-fix operator (–b) is executed immediately before the variable b is used, decrementing it by 1. The post-fix operator (b–) is executed immediately after the variable b is used, decrementing it by 1.