Java MCQs on recursion of Java Programming Language.
1. What is Recursion in Java? Answer: b 2. Which of these data types is used by operating system to manage the Recursion in Java? Answer: b 3. Which of these will happen if recursive method does not have a base case? Answer: a 4. Which of these is not a correct statement? Answer: d 5. Which of these packages contains the exception Stack Overflow in Java? Answer: a 6. What will be the output of the following Java program? a) 0 7. What will be the output of the following Java program? a) 0 8. What will be the output of the following Java program? a) 24 9. What will be the output of the following Java program? a) 1 10. What will be the output of the following Java program? a) 1
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
Clarification: Recursion is the process of defining something in terms of itself. It allows us to define a method that calls itself.
a) Array
b) Stack
c) Queue
d) Tree
Clarification: Recursions are always managed by using stack.
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
Clarification: If a recursive method does not have a base case then an infinite loop occurs which results in Stack Overflow.
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
Clarification: Recursion is always managed by operating system.
a) java.lang
b) java.util
c) java.io
d) java.system
Clarification: None.
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));
}
}
b) 1
c) Compilation Error
d) Runtime Error
Clarification: Since the base case of the recursive function func() is not defined hence infinite loop occurs and results in Stack Overflow.
Output:
$ javac Output.javac
$ java Output
Exception in thread "main" java.lang.StackOverflowError
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));
}
}
b) 1
c) 120
d) None of the mentioned
Clarification: None.
Output:
$ javac Output.javac
$ java Output
1
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));
}
}
b) 30
c) 120
d) 720
Clarification: 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
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));
}
}
b) 30
c) 120
d) Runtime Error
Clarification: 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
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));
}
}
b) 30
c) 120
d) 720
Clarification: None.
Output:
$ javac Output.javac
$ java Output
720