• 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

while() question

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given:
11. for( int i = min; i <max; i++) {
12. System.out.println(i);
13. }

If min and max are arbitrary integers, what gives the same result?
A. init i = min;
while( i < max ) {
}
B. int i = min;
do
System.out.println(i++);
} while( i< max );
C. for (int i=min; i<max; System.out.println(++I));
D. for (int i=; i++<max; System.out.println(i));
Answer: B

But I don't think here has correct answer .Do you follow me ?
I think the answer B is wrong ,when max<min.
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when max<min the code snippet given in the question will not run by itself, so you have to make that assumption that max>min.

so B is the right answer.

Also, A can't be the answer because its not printing anything.

C is wrong because its printing I not i

D is wrong because i is not initialized to anything and will give a compiler error.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
B cannot be right answer since do while loop is executed atleast once irrespective of MIN and MAX valud but taht is not true for loop .

Vydhehi
 
Author
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Peter, your thinking to much into the question here.

This question is checking to see if you can rewrite the loop using something other than a for loop to be equivalent(i.e translate from one to another.)Also, remember how to always 1. initalize 2. test and 3. update the loops.

Now look at the questions:

Is A equivalent to the question? No, missing print statement
Is C equivalent to the question? No, because it will print nothing(notice the semicolon after the for)
Is D equivalent to the question? No, the update is wrong.

B is correct. the code could be rewritten as:

int i = min;
do
{
system.out.println(x)
x++;
}while(i < max);

which is the sam as the for loop.
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

[ September 13, 2004: Message edited by: Louie van Bommel ]
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe the answer should be A, but with the print statement inserted ofcourse


Is the same as


imho. The do.. while is incorrect because it is guaranteed to be executed at least once, the for loop isn't.
 
PETER CARTER
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If it can compile successfully,it will be max>min.
So the answer is B.
Thanks .
 
Jimmy Praet
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why would it not compile if max<min?

If max and min were compile time constants then maybe it would not compile because of unreachable statements, but this isn't the case in the question so..

if max = min or max < min then B is not correct
 
reply
    Bookmark Topic Watch Topic
  • New Topic