• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

SCJP 5 Guide by K&B: Q5. Page 391 / 402

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,

Here is my question - Source: SCJP 5 Guide by K&B: Q5. Page 391 / 402

Given:



Which, inserted at line 4, produces the output 12?
A. int y = x;
B. int y = 10;
C. int y = 11;
D. int y = 12;
E. int y = 13;
F. None of the above will allow compilation to succeed.

Answer:
> C is correct. x reaches the value of 11, at which point the while test fails. x is then incremented (after the comparison test!), and the println() method runs.
> A, B, D, E, and F are incorrect based on the above.

My Question:
Since postfix operators (as well as prefix unary operators) have higher precedence than relational operators; why isn't x++ evaluated before < operator in the do{} while() expression in the question above? Going by that the correct answer should be option D.

---------
Ravinder
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When a postfix operator is used as part of an expression, the value of the variable in the expression is used before incrementing it. (That's how Java works!)

regards
Andy
 
reply
    Bookmark Topic Watch Topic
  • New Topic