Java Quiz on recursive calls

What will be the output of following Java quiz ?

 

In following Java program, two JavaQuiz objects are created and a method callAgain() is called to manipulate the value of state variable.

What will be the output when this program is executed ?
 

Java quiz on recursive calls
 

Answer

 

For the first call to callAgain(), the “if” check is false. So, state is just incremented to 2 and printed.

For second call, the “if” condition is true, so callAgain() is invoked one more time. state becomes 6 and is returned, but not stored. When control is returned to second invocation of callAgain(), state is increased to 7.

So, correct answer is “2 7”.
 

You may also like

Top 10 popular and tricky Java puzzles

Top Java articles : March 2016

Best 25 Java articles on the web in 2015

 
 

© 2016, https:. All rights reserved. On republishing this post, you must provide link to original post

You may also like...

Leave a Reply.. code can be added in <code> </code> tags