OCPJP & OCAJP Mock Exam Question for the Java 7 Exam

Looking to get Java 7 certified from Oracle, and obtain your OCPJP or OCAJP, Professional or Associate designations? Well, you need to know the tricks they throw at you with regards to operator precedence. This question hits your knowledge of the OCAJP 7 exam objective in the Using Operators and Decision Constructs, namely the objective named "Use parentheses to override operator precedence."

In order to pass the OCPJP and OCAJP exams, you need to have a strong understanding of both the five basic mathematical...

operations and the intricacies of the unary operators.

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.

Discuss this question with your friends on JavaRanch

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++;

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;

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.

Discuss this question with your friends on JavaRanch


Learning Resources for the Java and Java 7 Certification

OCP Java SE 6 Programmer Practice Exams (Exam 310-065) (Certification Press)
OCP Java SE 7 Programmer Study Guide (Certification Press)
SCJP Sun Certified Programmer for Java 6 Exam 310-065
A Programmer's Guide to Java SCJP Certification: A Comprehensive Primer (3rd Edition)
SCJA Sun Certified Java Associate Study Guide for Test CX-310-019, 2nd Edition


Check out these other tutorials from TheServerSide:

New Java 7 Features: Binary Notation and Literal Variable Initialization
New Java 7 Features: Numeric Underscores with Literals Tutorial
New Java 7 Features: Using String in the Switch Statement Tutorial
New Java 7 Features: The Try-with-resources Language Enhancement Tutorial 
New Java 7 Features: Automatic Resource Management (ARM) and the AutoCloseable Interfact Tutorial
New Java 7 Features: Suppressed Exceptions and Try-with-resources Tutorial
Java 7 Mock Certification Exam: A Tricky OCPJP Question about ARM and Try-with-Resources
OCAJP Exam to Debuts in March 2010. OCPJP Released in June?
OCPJP & OCAJP Java 7 Exams: Oracle Drops the Training Requirement
OCAJP and OCPJP Changes for Java 7:  New Objectives, a Format Change and a Price Hike



Next Steps

New to Git and distributed version control? Here are some Git examples and Jenkins-Git integration tutorials designed to help you master the popular source code versioning tool.

Dig Deeper on REST Web services

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.