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.
Piyush Jain
Ranch Hand
Joined: Apr 25, 2003
Posts: 60
posted
0
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
Greenhorn
Joined: Aug 27, 2003
Posts: 22
posted
0
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
James Chegwidden
Author
Ranch Hand
Joined: Oct 06, 2002
Posts: 201
posted
0
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.
Mr. C<br /> <br />Author and Instructor<br />My book:<br /><a href="http://www.aw-bc.com/catalog/academic/product/0,1144,1576761614,00.html" target="_blank" rel="nofollow">http://www.aw-bc.com/catalog/academic/product/0,1144,1576761614,00.html</a>
Louie van Bommel
Ranch Hand
Joined: Aug 17, 2004
Posts: 76
posted
0
[ September 13, 2004: Message edited by: Louie van Bommel ]
Jimmy Praet
Greenhorn
Joined: Sep 06, 2004
Posts: 21
posted
0
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
Joined: Aug 28, 2004
Posts: 70
posted
0
If it can compile successfully,it will be max>min. So the answer is B. Thanks .
Jimmy Praet
Greenhorn
Joined: Sep 06, 2004
Posts: 21
posted
0
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: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.