Java Multiple Choice Questions & Answers on Recursion for Freshers

https://www.computersprofessor.com/2018/01/java-multiple-choice-questions-answers_31.html
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.
Answer: b
Explanation: Recursion is the process of defining something in terms of itself. It allows us to define 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
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
Answer: a
Explanation: If a recursive method does not have a base case then an infinite loop occurs which results in stackoverflow.
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’s Run – Time environment.
Answer: d
Explanation: Recursion is always managed by operating system.
5. Which of these packages contains the exception Stackoverflow in Java?
a) java.lang
b) java.util
c) java.io
d) java.system
Answer: a
6. What is the output of this program?
class recursion {
int func (int n) {
int result;
result = func (n - 1);
return result;
}
}
class Output {
public static void main(String args[]) {
recursion obj = new recursion() ;
System.out.print(obj.func(12));
}
}
a) 0
b) 1
c) Compilation Error
d) Runtime Error
b) 1
c) Compilation Error
d) Runtime Error
Answer: d
Explanation: Since the base case of the recursive function func() is not defined hence infinite loop occurs and results in stackoverflow.
7. What is the output of this program?
class recursion {
int func (int n) {
int result;
if (n == 1)
return 1;
result = func (n - 1);
return result;
}
}
class Output {
public static void main(String args[]) {
recursion obj = new recursion() ;
System.out.print(obj.func(5));
}
}
a) 0
b) 1
c) 120
d) None of the mentioned
b) 1
c) 120
d) None of the mentioned
Answer: b
8. What is the output of this program?
class recursion {
int fact(int n) {
int result;
if (n == 1)
return 1;
result = fact(n - 1) * n;
return result;
}
}
class Output {
public static void main(String args[]) {
recursion obj = new recursion() ;
System.out.print(obj.fact(5));
}
}
a) 24
b) 30
c) 120
d) 720
b) 30
c) 120
d) 720
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.
9. What is the output of this program?
class recursion {
int fact(int n) {
int result;
if (n == 1)
return 1;
result = fact(n - 1) * n;
return result;
}
}
class Output {
public static void main(String args[]) {
recursion obj = new recursion() ;
System.out.print(obj.fact(1));
}
}
a) 1
b) 30
c) 120
d) Runtime Error
b) 30
c) 120
d) Runtime Error
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.
10. What is the output of this program?
class recursion {
int fact(int n) {
int result;
if (n == 1)
return 1;
result = fact(n - 1) * n;
return result;
}
}
class Output {
public static void main(String args[]) {
recursion obj = new recursion() ;
System.out.print(obj.fact(6));
}
}
a) 1
b) 30
c) 120
d) 720
b) 30
c) 120
d) 720
Answer: d