293). 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
Show Answer
Correct Answer: Recursion is a process of defining a method that calls itself repeatedly
Notes:
Answer: c
Explanation: Recursion is the process of defining something in terms of itself. It allows us to define a method that calls itself.
294). Which of these data types is used by operating system to manage the Recursion in Java?
[A]Array
[B]Stack
[C]Queue
[D]Tree
Show Answer
Correct Answer: Stack
Notes:
Answer: b
Explanation: Recursions are always managed by using stack.
295). 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
Show Answer
Correct Answer: An infinite loop occurs
Notes:
Answer: a
Explanation: If a recursive method does not have a base case then an infinite loop occurs which results in Stack Overflow.
296). 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
Show Answer
Correct Answer: Recursion is managed by Java Runtime environment
Notes:
Answer: d
Explanation: Recursion is always managed by operating system.
297). Which of these packages contains the exception Stack Overflow in Java?
[A] java.lang
[B] java.util
[C] java.io
[D]java.system
Show Answer
Correct Answer: java.lang
Notes:
Answer: a
Explanation: None.
298). What will be the output of the following Java 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
Show Answer
Correct Answer: Runtime Error
Notes:
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:$ javac Output.javac
$ java Output
Exception in thread "main" java.lang.StackOverflowError
299). What will be the output of the following Java 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
Show Answer
Correct Answer: 1
Notes:
Answer: b
Explanation: None.
Output:
$ javac Output.javac
$ java Output
1
300). What will be the output of the following Java 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
Show Answer
Correct Answer: 120
Notes:
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
301). What will be the output of the following Java 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
Show Answer
Correct Answer: 1
Notes:
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
302). What will be the output of the following Java 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));
}
}
Show Answer
Correct Answer: 720
Notes:
Answer: d
Explanation: None.
Output:
$ javac Output.javac
$ java Output
720