Test Series - java

Test Number 55/64

Q: 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.  -
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.
A. FALSE
B. TRUE
C.  -
D.  -
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();
  }
}
A. 4 3 2 1
B. 3 2 1
C. 3 2 1 0
D. Compiler error
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.
A. TRUE
B. FALSE
C.  -
D.  -
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;
  }
}
A. 10
B. 6
C. 0
D. Infinite loop
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;
  }
}
A. 24
B. 6
C. 0
D. Infinite loop
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 ___.
A. IF block
B. ELSE block
C. IF or ELSE block
D. None
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 ____.
A. void
B. Some value or object
C.  -
D.  -
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?
A. 8
B. 16
C. 32
D. No limit
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?
A. Stack Memory
B. Heap Memory
C.  -
D.  -
Solution: Heap memory can store tens of thousands of objects. Stack memory is relatively small or limited.

You Have Score    /10