• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3752
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic