• 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

what will be the output

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int x=1, y=6;
while (y--)
{
x++;
}
System.out.println("x="+x+"y="+y);

What is the output?
A)x=6 y=0
B)x=7 y=0
C)x=6 y=-1
D)x=7 y=-1
E) Compilation failed


Answer is D
-------------

I feel the answer should be E ? How can it be D

Pls explain this.

thx
megha
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i agree it should be "e". the expression inside the parens of a while statment MUST evaluate to 'true' or 'false'. y-- does not, and will never, evaluate to one of those two values.

now, if this were writen in C/C++, that kind of logic is valid. but this isn't the CRanch.

:-)
 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure its a compilation failure

Type mismatch: cannot convert from int to boolean

 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The ultimate test for a would be SCJPG (Sun Certified Java Programming Guru) is to find out which mock examination examples answers are sometimes very very wrong
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic