Recursion(Computer Science > Java Program ) Questions and Answers

Question 1.


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));
}
}
  1.    1
  2.    30
  3.    120
  4.    720
Explanation:-
Answer: Option D. -> 720

None.
Output:
$ javac Output.javac
$ java Output
720



Question 2.


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));
}
}
  1.    1
  2.    30
  3.    120
  4.    Runtime Error
Explanation:-
Answer: Option A. -> 1

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



Question 3.


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));
}
}
  1.    24
  2.    30
  3.    120
  4.    720
Explanation:-
Answer: Option C. -> 120

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



Question 4.


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));
}
}
  1.    0
  2.    1
  3.    120
  4.    None of the mentioned
Explanation:-
Answer: Option B. -> 1

None.
Output:
$ javac Output.javac
$ java Output
1



Question 5.


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));
}
}
  1.    0
  2.    1
  3.    Compilation Error
  4.    Runtime Error
Explanation:-
Answer: Option D. -> Runtime Error

Since the base case of the recursive function func() is not defined hence infinite loop 

occurs and results in stackoverflow.
Output:
$ javac Output.javac
$ java Output
Exception in thread "main" java.lang.StackOverflowError



Question 6.

Which of these packages contains the exception Stackoverflow in Java?


  1.    java.lang
  2.    java.util
  3.    java.io
  4.    java.system
Explanation:-
Answer: Option A. -> java.lang

None.



Question 7.

Which of these will happen if recursive method does not have a base case?


  1.    An infinite loop occurs
  2.    System stops the program after some time.
  3.    After 1000000 calls it will be automatically stopped.
  4.    None of the mentioned
Explanation:-
Answer: Option A. -> An infinite loop occurs

If a recursive method does not have a base case then an infinite loop occurs which 

results in stackoverflow.



Question 8.

Which of these data types is used by operating system to manage the Recursion 

in Java?


  1.    Array
  2.    Stack
  3.    Queue
  4.    Tree
Explanation:-
Answer: Option B. -> Stack

 Recursions are always managed by using stack.


Question 9.

What is Recursion in Java?


  1.    Recursion is a class.
  2.    Recursion is a process of defining a method that calls other methods repeatedly.
  3.    Recursion is a process of defining a method that calls itself repeatedly.
  4.    Recursion is a process of defining a method that calls other methods which in turn call again this method.
Explanation:-
Answer: Option B. -> Recursion is a process of defining a method that calls other methods repeatedly.

Recursion is the process of defining something in terms of itself. It allows us to 


Question 10.

Which of these is not a correct statement?


  1.    A recursive method must have a base case.
  2.    Recursion always uses stack.
  3.    Recursive methods are faster that programmers written loop to call the function repeatedly using a stack.
  4.    Recursion is managed by Java's Run - Time environment.
Explanation:-
Answer: Option D. -> Recursion is managed by Java's Run - Time environment.

Recursion is always managed by operating system.