OCPJP & OCAJP Mock Exam Question for the Java 7 Exam
By Cameron McKenzie and Sal Pece
As far as mathematics go, you need to understand addition, subtraction, multiplication, division and the modulus/remainder operator. You also need to know the various mathematical shortcuts like += and *= that can be used with the five basic mathematical operators.
As far as the unary operators go, you need a good understanding of both the postfix and prefix operators and how they work when they're involved in a relatively complex expressions.
A tricky OCAJP mock exam question on operator precedence
Now, every Java developer knows the basics of how addition works, or how the unary increment or decrement operators work. But given a relatively unorthodox combination of these operators, would a typical developer have confidence in predicting the result? It's an unorthodox application of these operators that tend to trip users up on these Java certification exams.
Take a look at the following code snippet. It's symptomatic of what Oracle will throw at you on the Associate or Professional certification exams for Java 7:
int x = 2; x += x++ * x++ * x++; System.out.println(x);
What gets multiplied here? Do all three increments happen and then the multiplication happens? Will we have 2*2*2? Or will the increment happen first, giving us 5*5*5? Or will the increments on the variable x happen progressively, giving us 2*3*4?
And what about the mathematical operator? x+=1 translates into x=x+1. But what does x += x++ * x++ * x++; translate into? Here's a couple of suggestions that one might suppose was correct:
x = (x+x++) * x++ * x++; x = x + (x++ * x++ * x++); x = (x + x++ ) * (x + x++ ) *(x + x++ );
Mathematical shortcuts and operator precedence
Well, as far as the mathematical shortcut goes, any time you have a += type of expression, the variable on the left hand side gets applied to the result of the right hand side when the whole thing on the right hand side has completed. So in this case, our expression can be simplified from:
x += x++ * x++ * x++;
x = (x) + (x++ * x++ * x++);
And since the initial value of x is two, it could be simplified once more to:
2 + (x++ * x++ * x++);
Increment operator and operator precedence
Now, as far as the increment operator goes, with a postfix, the variable is used, and then immediately incremented. At runtime, our code would be equivalent to:
2 + (2 * 3 * 4);
As you can see, each time the postfix is encountered, it outputs the current value of x, and then increments the variable immediately after, giving us 2*3*4, or 24. When added to the number 2, we get the final output of our code snippet, which is the number 26.
So, you've got it? It's pretty straight forward once you've been walked through it. And there's no doubt that you'll have no difficulty figuring out what the result would be if we were using a unary prefix instead of a unary postfix.
int x = 2; x += ++x * ++x * ++x; System.out.println(x);
Work it out in your head, and then throw it into a compiler and see if your thought process is correct. That's really the best way to learn.
23 Jan 2012