Q: A Java program with recursion can be completed with few lines of code when compared to using Loops. State TRUE or FALSE.
Solution: Recursion makes a program small. So, the output class file will be less in size.
Q: It is difficult to write a program with recursion than using multiple loops. State TRUE or FALSE.
Solution: Yes. Recursion is a bit tricky. If wrongly programmed, the recursive method will be executed an infinite number of times causing Stack Overflow errors.
Q: 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(); } }
Solution: Note that this recursive method does not return anything. It simply does the printing of some values.
Q: A Recursive method does not need to return a value always. State TRUE or FALSE.
Solution: TRUE.
Q: 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; } }
Solution: 10 IN 4 IN 3 IN 2 IN 1 IN 0 10
Q: 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; } }
Solution: 24 IN 4 IN 3 IN 2 IN 1 IN 0 24
Q: To end a recursive method a RETURN statement is usually kept inside ___.
Solution: A proper RETURN statement ends the recursion process when kept inside a conditional IF or ELSE block.
Q: In most of the scenarios, a recursive method returns ____.
Solution: A VOID-Recursive method is useful only for printing or logging data in general.
Q: What is the maximum number of levels in a Recursion?
Solution: 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.
Q: Which is bigger in size Stack Memory or Heap Memory?
Solution: Heap memory can store tens of thousands of objects. Stack memory is relatively small or limited.
You Have Score    | /10 |