Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

anilbachi question..

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
***********************************************
 
malathi latha
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public int test(int X){
int n=1;
int i=0;
while(i < X){
if(i==2) continue;
i++;
n*=n+1;
}
return n;
}
 
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
The method never returns because of an infinite loop.
Answer is C
Suneel
 
malathi latha
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
can u please explain me how the 'continue' effects the program flow in this case??
Thank you
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
the answer is 42
Thanks
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
thanks all for your replies.
malathi
 
madhu kumar
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi moderators,
can I use any html codes here to make my post look good.
can u help me.
thankyou.
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jane,
I have already changed my user id.
thanks,
Thiru
reply
    Bookmark Topic Watch Topic
  • New Topic