File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes anilbachi 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 "anilbachi question.." Watch "anilbachi question.." New topic
Author

anilbachi question..

malathi latha
Greenhorn

Joined: Feb 11, 2001
Posts: 21
Hi all
this question is from http://www.anilbachi.8m.com can any1 explain me the solution
q). What happens when you compile & run the following with input of 4

a).The method returns a value of 6
b).The method returns a value of 42
c).The method never returns due to an infinite loop
d).The method fails to compile

Added code tags to correct display ...
See http://www.javaranch.com/ubb/ubbcode.html to see how to do this on your own ... Jane

[This message has been edited by Jane Griscti (edited February 22, 2001).]
natarajan meghanathan
Ranch Hand

Joined: Feb 01, 2001
Posts: 130
Can you again post the while loop little bit clearly. I couldn't guess the output of the code from what you have posted. what is the iif(i==2) in the while loop?

------------------

***********************************************
Sun Certified Programmer for Java 2 Platform
***********************************************


Sun Certified Programmer for Java 2 Platform
malathi latha
Greenhorn

Joined: Feb 11, 2001
Posts: 21
public int test(int X){
int n=1;
int i=0;
while(i < X){
if(i==2) continue;
i++;
n*=n+1;
}
return n;
}
Suneel Setlur
Ranch Hand

Joined: Nov 26, 2000
Posts: 202
Hi
The method never returns because of an infinite loop.
Answer is C
Suneel


Suneel
malathi latha
Greenhorn

Joined: Feb 11, 2001
Posts: 21
Hi,
can u please explain me how the 'continue' effects the program flow in this case??
Thank you
Rakesh Sharma
Ranch Hand

Joined: Dec 29, 2000
Posts: 37
Dear Suneel
Can you tell me why infinite loop is formed. I think ans should be 42. We are passing x = 4 in the method.
thiru
Greenhorn

Joined: Feb 22, 2001
Posts: 6
hi,
I might be wrong..if so please correct me.
I feel the answer should be 42 i'e B.
B'caz, when i=0 n will become 6 (n=n(n+1)= 1(1+1)=2)
when i=1 n will become 6 (2(2+1) = 6)
when i=2 ,the loop continues,
when i=3, n becomes 42 (6(6+1)=42)
when i=4 ,it doesn't go into loop.
I hope I'm correct.If wrong please correct me.
Thanks,
Thiru
madhu kumar
Greenhorn

Joined: Jan 05, 2001
Posts: 20
Hi all,
the answer is 42
Thanks
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi All,
Suneel is correct. The answer is C.
You can test it with the following code:

It will run forever!
Here's what happens:
<pre>
start: x=4; n=1; i=0;
inside loop:
i < x 0 < 4 result: yes
i == 2 result: no
i++ result: i = 1

n *= n + 1 1 * (1+1) result: n = 2
next iteration:
i < x 1 < 4 result: yes
i == 2 result: no
i++ result: i = 2

n *= n + 1 2 * (2+1) result: n = 6
next iteration:
i < x 2 < 4 result: yes
i == 2 result: yes -> continue loop
next iteration:
i < x 2 < 4 result: yes
i == 2 result: yes -> continue loop
keeps going, and going and going ....
</pre>
Once i is equal to 2 ... it never changes; the 'continue' forces the JVM to skip the remainder of the loop and begin again from the start of while().
Hope that helps.

------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Thiru,
Would you please read the Name Policy and reregister under a name that complies with the rules.
Thanks for your cooperation.

------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
Bala Arul
Greenhorn

Joined: Feb 09, 2001
Posts: 29
Hi All,
The answer is :
c).The method never returns due to an infinite loop. While i==2, the loop execute the 'continue' statement. That means 'i' never get changed, since the increment to 'i' is below the 'continue' statement. So i < X is always true. (X = 4).
malathi latha
Greenhorn

Joined: Feb 11, 2001
Posts: 21
Hi
thanks all for your replies.
malathi
madhu kumar
Greenhorn

Joined: Jan 05, 2001
Posts: 20
Hi moderators,
can I use any html codes here to make my post look good.
can u help me.
thankyou.
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Madhu,
You can use both HTML and UBB Codes; as long as you don't intermingle them ... the instructions are here:
http://www.javaranch.com/ubb/ubbcode.html
Hope that helps.
Jane

thiru chakravarthi
Greenhorn

Joined: Feb 28, 2001
Posts: 7
Jane,
I have already changed my user id.
thanks,
Thiru
 
jQuery in Action, 2nd edition
 
subject: anilbachi question..
 
Similar Threads
Anilbachi scjp suit downloads, take yours
anilbachi question?
try this!
mock exam on "objectives"
Urgent help needed