• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

MindQ question 29

 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


29. For the code:
m = 0;
while( m++ < 2 )
System.out.println( m );
Which of the following are printed to standard output?
a) 0
b) 1
c) 2
d) 3
e) Nothing and an exception is thrown
[\QUOTE]
I answered a and b but correct answers were b and c.
I think they are right but could someone explain a little bit for me.

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the first iteration m is 0 and after the condition is checked it becomes 1 because of m++. So, this prints 1.
Now second iteration starts and this time m is 1 so, condition is true and m++ gets executes. now m contains 2 and enters into loop. so, prints 2. Now again iteration starts. m is 2 and 2 is not less than 2 so it comes out of the loop.
I hope this might help u
Bye
with regards
chak

Originally posted by Randall Twede:


29. For the code:
m = 0;
while( m++ < 2 ) /* in the first iteration m is 0
System.out.println( m );
Which of the following are printed to standard output?
a) 0
b) 1
c) 2
d) 3
e) Nothing and an exception is thrown
[\QUOTE]
I answered a and b but correct answers were b and c.
I think they are right but could someone explain a little bit for me.


 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
m = 0;
while( m++ < 2 )
System.out.println( m );
1) m = 0
2) m++ returns 0 which is less than 2
2a) Increment m to 1.
3) Prints out value of m which is 1.
4) m++ returns 1 which is still less than 2
4a) Increment m to 2.
5) Prints out value of m which is 2.
6) m++ returns 2 which is not less than 2.
6a) Increment m to 3.
7) Loop ends, exit.
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys that helped. I'm not sure why I thought it would print 0 the first time.
[This message has been edited by Randall Twede (edited December 14, 2000).]
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,
I too did the mistake of choosing a when i answered the question. I think we should be more careful while trying to answer the questions. It will be obviously a loss if we make a mistake in a concept we know very well.
------------------
Regards,
Shree
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Please can u give the URL of MindQ.
Regards,
vijay
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://www.javaranch.com/maha/_Mock_Exams/MindQ_s_Sun_Certified_Java_Programmer_Practice_Test.htm
to get a list of many tests go to maha's page http://www.javaranch.com/maha/index.html
 
I just had the craziest dream. This tiny ad was in it.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic