aspose file tools*
The moose likes Blatant Advertising and the fly likes OCAJP OCPJP Java 7 Mock Certification Exam Question: Use parentheses to override operator precedence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Other » Blatant Advertising
Bookmark "OCAJP OCPJP Java 7 Mock Certification Exam Question: Use parentheses to override operator precedence" Watch "OCAJP OCPJP Java 7 Mock Certification Exam Question: Use parentheses to override operator precedence" New topic
Author

OCAJP OCPJP Java 7 Mock Certification Exam Question: Use parentheses to override operator precedence

Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

I just posted a question and answer describing a tricking mock exam question that you'll run into on the OCAJP exam. You might even find it on the OCPJP exam as well.

The question itself walks through a discussion of the following code snippet:



A mock Java exam question would ask what the output would be, and provide a few sample answers, although I personally think an aspiring Java Professional should be able to figure out the answer without being given a few multiple choice answers to sort through.

OCAJP Mock Exam Question: Unary Operatrors, Math Shortcuts and Overriding Operator Precedence

TheServerSide article contains a crosslink to this page for discussion. Let the discussions begin!
Jeff Hill
Greenhorn

Joined: Aug 24, 2008
Posts: 11
Hi Cameron,
I'm not sure whether the discussion was to be here or in the Q&A thread, but I don't want to look at the Q&A first and see the answer (even though I know it ;-)

The thing to remember is that, in any expression involving a post-fix operation, the value of the variable x before the "++" operation is used in evaluating the expression each time it appears. Further, the entire expression on the right will be evaluated before the "+=" operator is applied.

Since evaluation proceeds left-to-right:
     x += x++ * x++ * x++;
by substitution, this is equivalent to:
     x += (x) * (x+1) * (x+2)
or:
     x += (x^2 + x) * (x+2)
or:
     x += (x^3 + 2x^2 + x^2 + 2x)
or:
     x += (x^3 + 3x^2 + 2x)
or:
     x = x^3 + 3x^2 + 3x
so if x=2:
     x = 2^3 + 3 (2^2) + 3(2)
or:
     x = 8 + 12 + 6
     x = 26

If you think that's a toughie, try figuring out the cost of upgrading from the original "Java Platform 2" certification to the array of certifications available now that cover the same technical territory, and whether there is any "upgrade" discount!

JeffInTampa
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

Very sweet.

I ran a conversation on this question over at TheServerSide: Interesting OCAJP/OCPJP Mock Exam Question

The general consensus was that they hated the fact certification exams too often test people based on riddles. They didn't like it.

Jeff Hill
Greenhorn

Joined: Aug 24, 2008
Posts: 11
The sad fact is one is likely to see such code in the wild, and have to maintain it, so IMHO such questions are valid. If nothing else they make you pay attention during the test. I'll bet those who complained also wanted a "participation" certificate if they didn't pass!

Besides, anything that makes you think can help stave off dementia.

JeffInTampa
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3285
    
    7
It is entirely possible that one might have to maintain such a cryptic code base. However, one might have to do several other things as well while maintaining a code base. Does that mean they should all be on the test?

I feel that in the limited time and number of questions that a candidate has to be tested within, it believe it is better to focus on basic programming and language skills than on deciphering cryptic code.

A recruiter can always have a specialized evaluation if they have a special need for the candidate to be able to decipher cryptic code. After all, one test cannot fit all

Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: OCAJP OCPJP Java 7 Mock Certification Exam Question: Use parentheses to override operator precedence