Oracle Certified Professional Java Se 11 Developer · Free Practice Question Hard

Question 108

Question ID: UKOCP10968


What will be the result of compiling and executing Test class?

  • A 2
  • B 3
  • C 1
  • D Compilation error
Reveal correct answer

Correct answer: B

Explanation

UKOCP10968:

i++ == 2 || --i == 2 && --i == 2; [Given expression]. 

(i++) == 2 || --i == 2 && --i == 2; [Postfix has got higher precedence than other operators]. 

(i++) == 2 || (--i) == 2 && (--i) == 2; [After postfix, precedence is given to prefix].   

((i++) == 2) || ((--i) == 2) && ((--i) == 2); [== has higher precedence over && and ||]. 

((i++) == 2) || (((--i) == 2) && ((--i) == 2)); [&& has higher precedence over ||]. 

Let's start solving it:

((i++) == 2) || (((--i) == 2) && ((--i) == 2)); [i=2, res=false]. 

(2 == 2) || (((--i) == 2) && ((--i) == 2)); [i=3, res=false]. 

true || (((--i) == 2) && ((--i) == 2)); [i=3, res=false].  || is a short-circuit operator, hence no need to evaluate expression on the right. 

res is true and i is 3.

Discussion

Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.

You must be logged in to post a comment.

Preparing For

Your Certification?

255+ certifications
Detailed explanations
Free PDF samples

Has All The Questions You Need