• 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

Doubt from majji test

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can pls. someone explain me this?
What will happen when you invoke the following method?
1: void infiniteLoop()
2: {
3: byte b = 1;
4:
5: while ( ++b > 0 )
6: ;
7: System.out.println("Welcome to Java");
8: }

A) The loop never ends(infiniteLoop).
B) Prints "Welcome to Java".
C) Compilation error at line 5. ++ operator should not be used for byte type variables.
D) Prints nothing.

Correct Answer:B
Thank you
VR
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

5: while ( ++b > 0 )
6: ;
The while loop doesn't have any statements to execute. In this case it continues with the next statement i.e line 7.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by VR:
Can pls. someone explain me this?
What will happen when you invoke the following method?
1: void infiniteLoop()
2: {
3: byte b = 1;
4:
5: while ( ++b > 0 )
6: ;
7: System.out.println("Welcome to Java");
8: }

A) The loop never ends(infiniteLoop).
B) Prints "Welcome to Java".
C) Compilation error at line 5. ++ operator should not be used for byte type variables.
D) Prints nothing.

Correct Answer:B
Thank you
VR



Byte ranges from -128 to 127.When the while loop is executed upto b= 127 and in the next iteration it becomes negative(-128) and hence comes out of the while loop and prints the "Welcome to Java".Hope this clears your doubt.
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think b would be promoted 2 int for comparison ...
then also it shold throw an exception when a num greater then
-2^31 is supplied 2 it ... it cant print the option c
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all. Jaypii, you cleared my doubt.
Regards
VR
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, if you put this statement System.out.println("b = " + b); after while ( ++b > 0 ), it prints from b = 2 to b = 127 and then it prints Welcome to Java.
It corroborates Jaypii's explanation, though it wasn't expected by me. Thanks...
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic