Java Questions & Answers – Recursion

This section of our 1000+ Java MCQs focuses on recursion of Java Programming Language.

1. What is Recursion in Java?
a) Recursion is a class
b) Recursion is a process of defining a method that calls other methods repeatedly
c) Recursion is a process of defining a method that calls itself repeatedly
d) Recursion is a process of defining a method that calls other methods which in turn call again this method
View Answer

Answer: c
Explanation: Recursion is the process of defining something in terms of itself. It allows us to define a method that calls itself.

2. Which of these data types is used by operating system to manage the Recursion in Java?
a) Array
b) Stack
c) Queue
d) Tree
View Answer

Answer: b
Explanation: Recursions are always managed by using stack.

3. Which of these will happen if recursive method does not have a base case?
a) An infinite loop occurs
b) System stops the program after some time
c) After 1000000 calls it will be automatically stopped
d) None of the mentioned
View Answer

Answer: a
Explanation: If a recursive method does not have a base case then an infinite loop occurs which results in Stack Overflow.

4. Which of these is not a correct statement?
a) A recursive method must have a base case
b) Recursion always uses stack
c) Recursive methods are faster that programmers written loop to call the function repeatedly using a stack
d) Recursion is managed by Java Runtime environment
View Answer

Answer: d
Explanation: Recursion is always managed by operating system.
advertisement
advertisement

5. Which of these packages contains the exception Stack Overflow in Java?
a) java.lang
b) java.util
c) java.io
d) java.system
View Answer

Answer: a
Explanation: None.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

6. What will be the output of the following Java program?

  1.     class recursion 
  2.     {
  3.         int func (int n) 
  4.         {
  5.             int result;
  6.             result = func (n - 1);
  7.             return result;
  8.         }
  9.     } 
  10.     class Output 
  11.     {
  12.         public static void main(String args[]) 
  13.         {
  14.             recursion obj = new recursion() ;
  15.             System.out.print(obj.func(12));
  16.         }
  17.     }

a) 0
b) 1
c) Compilation Error
d) Runtime Error
View Answer

Answer: d
Explanation: Since the base case of the recursive function func() is not defined hence infinite loop occurs and results in Stack Overflow.
Output:

advertisement
$ javac Output.javac
$ java Output
Exception in thread "main" java.lang.StackOverflowError

7. What will be the output of the following Java program?

advertisement
  1.     class recursion 
  2.     {
  3.         int func (int n) 
  4.         {
  5.             int result;
  6.             if (n == 1)
  7.                 return 1;
  8.             result = func (n - 1);
  9.             return result;
  10.         }
  11.     } 
  12.     class Output 
  13.     {
  14.         public static void main(String args[]) 
  15.         {
  16.             recursion obj = new recursion() ;
  17.             System.out.print(obj.func(5));
  18.         }
  19.     }

a) 0
b) 1
c) 120
d) None of the mentioned
View Answer

Answer: b
Explanation: None.
Output:

$ javac Output.javac
$ java Output
1

8. What will be the output of the following Java program?

  1.     class recursion 
  2.     {
  3.         int fact(int n) 
  4.         {
  5.             int result;
  6.             if (n == 1)
  7.                 return 1;
  8.             result = fact(n - 1) * n;
  9.             return result;
  10.         }
  11.     } 
  12.     class Output 
  13.     {
  14.         public static void main(String args[]) 
  15.         {
  16.             recursion obj = new recursion() ;
  17.             System.out.print(obj.fact(5));
  18.         }
  19.     }

a) 24
b) 30
c) 120
d) 720
View Answer

Answer: c
Explanation: fact() method recursively calculates factorial of a number, when value of n reaches 1, base case is excuted and 1 is returned.
Output:

$ javac Output.javac
$ java Output
120

9. What will be the output of the following Java program?

  1.     class recursion 
  2.     {
  3.         int fact(int n) 
  4.         {
  5.             int result;
  6.             if (n == 1)
  7.                 return 1;
  8.             result = fact(n - 1) * n;
  9.             return result;
  10.         }
  11.     } 
  12.     class Output 
  13.     {
  14.         public static void main(String args[]) 
  15.         {
  16.             recursion obj = new recursion() ;
  17.             System.out.print(obj.fact(1));
  18.         }
  19.     }

a) 1
b) 30
c) 120
d) Runtime Error
View Answer

Answer: a
Explanation: fact() method recursively calculates factorial of a number, when value of n reaches 1, base case is excuted and 1 is returned.
Output:

$ javac Output.javac
$ java Output
1

10. What will be the output of the following Java program?

  1.     class recursion 
  2.     {
  3.         int fact(int n) 
  4.         {
  5.             int result;
  6.             if (n == 1)
  7.                 return 1;
  8.             result = fact(n - 1) * n;
  9.             return result;
  10.         }
  11.     } 
  12.     class Output 
  13.     {
  14.         public static void main(String args[]) 
  15.         {
  16.             recursion obj = new recursion() ;
  17.             System.out.print(obj.fact(6));
  18.         }
  19.     }

a) 1
b) 30
c) 120
d) 720
View Answer

Answer: d
Explanation: None.
Output:

$ javac Output.javac
$ java Output
720

Sanfoundry Global Education & Learning Series – Java Programming Language.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.