This question comes from Jamie Jaworski's book "Java 2 Certification":
What is the value displayed by the following program?
class Question {
public static void main (
string[] args) {
int x = 0;
boolean b1, b2, b3, b4;
b1 = b2 = b3 = b4 = true;
x = (b1 | b2 & b3 ^ b4) ? x++: --x;
System.out.println(x);
}
}
A. -1
B. 0
C. 1
D. 2
The correct answer is B. I ran the code and verified it. Would someone explain to me why the answer isn't 1? Isn't the variable x the same variable that both receives the result of the ternary expression and is incremented inside the ternary expression afterwords?
The order of events as I understand it are as follows
- ternary expression evaluates to true
- current value of x (0) is returned to x
- x is incremented to 1
- x is printed
What am I missing here? Please help out a confused beginner. Thanks.