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
- 30
- 120
- 720
Explanation:-
Answer: Option D. -> 720None.
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
- 30
- 120
- Runtime Error
Explanation:-
Answer: Option A. -> 1fact() 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));
}
}
- 24
- 30
- 120
- 720
Explanation:-
Answer: Option C. -> 120fact() 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));
}
}
- 0
- 1
- 120
- None of the mentioned
Explanation:-
Answer: Option B. -> 1None.
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));
}
}
- 0
- 1
- Compilation Error
- Runtime Error
Explanation:-
Answer: Option D. -> Runtime ErrorSince 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
Explanation:-
Answer: Option A. -> java.langNone.
Explanation:-
Answer: Option A. -> An infinite loop occursIf a recursive method does not have a base case then an infinite loop occurs which
results in stackoverflow.
Explanation:-
Answer: Option B. -> Stack Recursions are always managed by using stack.
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
Explanation:-
Answer: Option D. -> Recursion is managed by Java's Run - Time environment.Recursion is always managed by operating system.