Recursion in Java Interview Questions and Answers
Table of Contents
Study and learn Interview MCQ Questions and Answers on Java Recursion. Attend job interviews easily with these Multiple Choice Questions. Recursion in java is a process in which a method calls itself continuously. A method in java that calls itself is called recursive method. It makes the code compact but complex to understand. Syntax: … methodname();//calling same method
Recursion in Java
Recursion is the technique of making a function call itself. This technique provides a way to break complicated problems down into simple problems which are easier to solve.
Recursion may be a bit difficult to understand. The best way to figure out how it works is to experiment with it.
Halting Condition
Just as loops can run into the problem of infinite looping, recursive functions can run into the problem of infinite recursion. Infinite recursion is when the function never stops calling itself. Every recursive function should have a halting condition, which is the condition where the function stops calling itself. In the previous example, the halting condition is when the parameter k
becomes 0.
It is helpful to see a variety of different examples to better understand the concept. In this example, the function adds a range of numbers between a start and an end. The halting condition for this recursive function is when end is not greater than start:
Recursion in Java using Methods Explained Tutorial
Java programming language supports Recursion. Recursion is a process of calling the same method from within the method. It is achieved only with methods. Let us know more about Recursion using this tutorial.
Java Recursion process uses the Stack memory to store all local variables. The variables or objects are added to and removed from the stack after each method call. Storage for the local variables is allocated fresh on each method invocation. So, there will be a total of N memory allocations if N is the depth of recursion.
Recursion of Methods Explained with Examples
A Java class contains so many methods. You can call a method from another method with some arguments and get a return-value from the method after completion of execution. This is very clear. In Recursion, you should call the same method from the same method.
Recursion Syntax:
type METHOD_NAME(type param1, type param2..) { METHOD_NAME(arguments); //Calling the same method }
Let us check the below example using recursion that calculates the sum of N numbers.
public class RecursionTest { int calcSum(int a) { int tmp = 0; System.out.print(a); if(a > 0) { System.out.print("+"); tmp = a + calcSum(a-1); } return tmp; } public static void main(String[] args) { RecursionTest rt = new RecursionTest(); System.out.print("\nSum of first 5 digits: " + rt.calcSum(5)); } } //OUTPUT //5+4+3+2+1+0 //Sum of first 5 digits: 15
Remember that we have stopped this never-ending recursion loop using an IF statement. Even, ELSE statement can also be used. Here the same method you called, again and again, is “int calcSum(int)“.
The output of the Recursion statement is given below for each invocation.
5 + calcSum(4) 5 + calcSum(4) + calcSum(3) 5 + calcSum(4) + calcSum(3) + calcSum(2) 5 + calcSum(4) + calcSum(3) + calcSum(2) + calcSum(1) 5 + calcSum(4) + calcSum(3) + calcSum(2) + calcSum(1) + calcSum(0) 5 + calcSum(4) + calcSum(3) + calcSum(2) + calcSum(1)+ 0 5 + calcSum(4) + calcSum(3) + calcSum(2) + 1 + 0 5 + calcSum(4) + calcSum(3) + 2 + 1 + 0 5 + calcSum(4) + 3 + 2 + 1 + 0 5 + 4 + 3 + 2 + 1 + 0 = 15
Here, Recursion occurred for 5 times as the very first call to the method “calcSum” is a normal call.
Note: In Recursion, completion of methods occurs from the latest to the oldest method.
[WpProQuiz 161]
Recursion in JAVA Interview MCQ
1) Recursion in Java is a way of calling the method from within the same method. State TRUE or FALSE.
A) TRUE
B) FALSE
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
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
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
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
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
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
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
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
Answer [=] B
Explanation:
Heap memory can store tens of thousands of objects. Stack memory is relatively small or limited.