home.softwaretechit.com
Study
and learn Interview MCQ Questions and Answers on Java Recursion. Attend job
interviews easily with these Multiple Choice Questions. You can print these
Questions in default mode to conduct exams directly. You can download these
MCQs in PDF format by Choosing Print Option first and Save as PDF option next
using any Web Browser.
Go
through Java Theory Notes on Recursion before reading these
objective questions.
1) Recursion in Java is a way of calling the method
from within the same method. State TRUE or FALSE.
A)
TRUE
B)
FALSE
C)
-
D)
-
Answer [=]
A
Explanation:
Yes. It is called recursion when you refer to the same method.
2) Check the below code and state whether it is
called Recursion in Java?
void methodA()
{
methodB();
}
void methodB()
{
methodA();
}
A)
TRUE
B)
FALSE
C)
-
D)
-
Answer [=]
B
Explanation:
No, not at all. In the above code, the first method calls the second
method and the second method calls the first method.
3) Java uses ___ type of memory to implement
Recursion.
A)
Heap
B)
Stack
C)
Register
D)
None
Answer [=]
B
Explanation:
Yes. Java uses Stack type memory to implement Recursion.
4) Java uses Stack type memory instead of Heap type
memory in implementing Recursion because of ___ feature of STACK.
A)
FIFO (First In First Out)
B)
LIFO (Last In First Out)
C)
Round Robin
D)
None
Answer [=]
B
Explanation:
Recursion unwinds and completes all methods from the latest to the
oldest. So, it requires the LIFO strategy offered by Stack memory.
5) Recursion in Java applies to ___.
A)
Constructors
B)
Variables
C)
Methods
D)
Blocks
Answer [=]
C
Explanation:
Recursion works only with methods.
6) Uses are Recursion in Java are___.
A)
Traversing folders and files on a disk
B)
Traversing the nodes of a Binary Tree
C)
Evaluating Nth order equations and Factorials
D)
All the above
Answer [=]
D
7) Which is better in terms of memory utilization
Recursion or Loops in Java?
A)
Recursion
B)
Loops
C)
-
D)
-
Answer [=]
B
Explanation:
Loops or iterations are better memory-optimized than Recursion technique
that solely depends on the Stack memory.
8) Which is the common problem with Recursive
methods in Java?
A)
StackOverflowError
B)
IndexOutOfBoundsException
C)
OutOfMemoryError
D)
None
Answer [=]
A
Explanation:
StackOverError occurs when the stack has been full and can not allocate
space for continuing new method call.
9) Recursion usually stops when the Java
Runtime encounters an IF or ELSE statement with a RETURN statement. State
TRUE or FALSE.
A)
FALSE
B)
TRUE
C)
-
D)
-
Answer [=]
B
Explanation:
True
10) Attempting to call a method recursively without
a proper RETURN statement leads to ___.
A)
Stack Overflow errors
B)
Buffer Overflow errors
C)
Compile-time errors
D)
ALL
Answer [=]
A
Explanation:
Without a proper RETURN statement, the runtime calls the same method
again and again for infinite times and causes memory issues.
11) A Java program with recursion can be completed
with few lines of code when compared to using Loops. State TRUE or FALSE.
A)
FALSE
B)
TRUE
C)
-
D)
-
Answer [=]
B
Explanation:
Recursion makes a program small. So, the output class file will be less
in size.
12) It is difficult to write a program with
recursion than using multiple loops. State TRUE or FALSE.
A)
FALSE
B)
TRUE
C)
-
D)
-
Answer [=]
B
Explanation:
Yes. Recursion is a bit tricky. If wrongly programmed, the recursive
method will be executed an infinite number of times causing Stack Overflow
errors.
13) What is the output of the below Java program
with recursion?
public class RecursionTesting
{
static int
num=4;
public
static void main(String[] args)
{
new
RecursionTesting().recursiveMethod();
}
void
recursiveMethod()
{
num--;
if(num ==
0)
return;
System.out.print(num + " ");
recursiveMethod();
}
}
A)
4 3 2 1
B)
3 2 1
C)
3 2 1 0
D)
Compiler error
Answer [=]
B
Explanation:
Note that this recursive method does not return anything. It simply does
the printing of some values.
14) A Recursive method does not need to return a
value always. State TRUE or FALSE.
A)
TRUE
B)
FALSE
C)
-
D)
-
Answer [=]
A
Explanation:
TRUE.
15) What is the output of the below Java program
with Recursion?
public class RecursionTest2
{
public
static void main(String[] args)
{
RecursionTest2 rt2 = new RecursionTest2();
int sum =
rt2.summer(4);
System.out.println(sum);
}
int
summer(int in)
{
int sum =
0;
if(in ==0)
return 0;
sum = in +
summer(--in);
return
sum;
}
}
A)
10
B)
6
C)
0
D)
Infinite loop
Answer [=]
A
Explanation:
10
IN 4
IN 3
IN 2
IN 1
IN 0
10
16) What is the output of the below Java program
with Recursion?
public class RecursionTest3
{
public
static void main(String[] args)
{
RecursionTest3 rt3 = new RecursionTest3();
int
multiplication = rt3.multiplier(4);
System.out.println(multiplication);
}
int
multiplier(int in)
{
System.out.println("IN " + in);
int mul =
0;
if(in ==0)
return 1;
mul= in *
multiplier(--in);
return
mul;
}
}
A)
24
B)
6
C)
0
D)
Infinite loop
Answer [=]
A
Explanation:
24
IN 4
IN 3
IN 2
IN 1
IN 0
24
17) To end a recursive method a RETURN statement is
usually kept inside ___.
A)
IF block
B)
ELSE block
C)
IF or ELSE block
D)
None
Answer [=]
C
Explanation:
A proper RETURN statement ends the recursion process when kept inside a
conditional IF or ELSE block.
18) In most of the scenarios, a recursive method
returns ____.
A)
void
B)
Some value or object
C)
-
D)
-
Answer [=]
B
Explanation:
A VOID-Recursive method is useful only for printing or logging data in
general.
19) What is the maximum number of levels in a
Recursion?
A)
8
B)
16
C)
32
D)
No limit
Answer [=]
D
Explanation:
There is no limit. The limit actually depends on the size of Stack
Memory. If you process a large amount of data, it may lead to a Stack Overflow
error.
20) Which is bigger in size Stack Memory or Heap
Memory?
A)
Stack Memory
B)
Heap Memory
C)
-
D)
-
Answer [=]
B
Explanation:
Heap memory can store tens of thousands of objects. Stack memory is
relatively small or limited.
0 Comments