File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes while() question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "while() question" Watch "while() question" New topic

while() question

Ranch Hand

Joined: Aug 28, 2004
Posts: 70
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;
} 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.
Piyush Jain
Ranch Hand

Joined: Apr 25, 2003
Posts: 60
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.
vydhehi paruchuri

Joined: Aug 27, 2003
Posts: 22
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 .

James Chegwidden
Ranch Hand

Joined: Oct 06, 2002
Posts: 201
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;
}while(i < max);

which is the sam as the for loop.

Author and Instructor, my book
Louie van Bommel
Ranch Hand

Joined: Aug 17, 2004
Posts: 76

[ September 13, 2004: Message edited by: Louie van Bommel ]
Jimmy Praet

Joined: Sep 06, 2004
Posts: 21
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.
Ranch Hand

Joined: Aug 28, 2004
Posts: 70
If it can compile successfully,it will be max>min.
So the answer is B.
Thanks .
Jimmy Praet

Joined: Sep 06, 2004
Posts: 21
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
I agree. Here's the link:
subject: while() question
It's not a secret anymore!