C# Questions & Answers on Recursion for Freshers

1. What is Recursion in CSharp defined as?

a) Recursion is another form of class
b) Recursion is another 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 calls this method
Answer: c

Explanation: Recursion is the process of defining something in terms of itself. It allows us to define method that calls itself repeatedly until it meets some base case condition.
2. Which of these will happen if recursive method does not have a base case?

a) Infinite loop condition occurrence
b) System gets hanged
c) After 10000 executions program will be automatically stopped
d) None of the mentioned
Answer: a

Explanation: If a recursive method does not have a base case which is necessary to meet the end of condition then an infinite loop occurs which results in stackoverflow exception error.
3. Which of these is not a correct statement?

a) A recursive method must have a base case
b) Recursion always uses stack
c) Recursion is always managed by C# Runtime environment
d) Recursive methods are faster that programmer written loop to call the function repeatedly using a stack
Answer: c

Explanation: No matter whatever is the programming language recursion is always managed by operating system.
4. What will be the correct output for the given code snippet?
  1.  class recursion 
  2.  {
  3.      int fact(int n) 
  4.      {
  5.          int result;
  6.          if (n == 1)
  7.          return 1;
  8.          result = fact(n - 1) * n;
  9.          return result;
  10.      }
  11.  } 
  12.  class Program 
  13.  {
  14.      public static void main(String args[]) 
  15.      {
  16.          recursion obj = new recursion() ;
  17.          Console.WriteLine(obj.fact(4));
  18.      }
  19.  }
a) 24
b) 30
c) 120
d) 144
Answer: a
5. What will be the correct output the for the given code snippet?
  1.  class maths 
  2.  {
  3.      int fact(int n) 
  4.      {
  5.          int result;
  6.          if (n == 1)
  7.          return 1;
  8.          result = fact(n - 1) * n;
  9.          return result;
  10.      }
  11.  } 
  12.  class Output 
  13.  {
  14.      static void main(String args[]) 
  15.      {
  16.          maths obj = new maths() ;
  17.          Console.WriteLine(obj.fact(1));
  18.      }
  19.  }
a) 2
b) 10
c) 1
d) 0
Answer: c

Explanation: fact() calculates recursively the factorial of a number when n turns to be 1, base case is executed consecutively and hence 1 is returned.
Output: 1
6. What will be the correct output for the given code snippet?
  1. class maths 
  2.  {
  3.      int fact(int n) 
  4.      {
  5.          int result;
  6.          if (n == 1)
  7.          return 1;
  8.          result = fact(n - 1) * n;
  9.          return result;
  10.      }
  11.  } 
  12.  class Output 
  13.  {
  14.      static void main(String args[]) 
  15.      {
  16.          maths obj = new maths() ;
  17.          Console.WriteLine(obj.fact(4)*obj.fact(2));
  18.      }
  19.  }
a) 64
b) 60
c) 120
d) 48
Answer: d

Explanation: 4! = 4*3*2*1 & 2! = 2*1 .So, 24*2 = 48.
Output : 48
7. What will be the correct output for the given code snippet?
  1.  class maths 
  2.  {
  3.      int fact(int n) 
  4.      {
  5.          int result;
  6.          if (n == 1)
  7.          return 1;
  8.          result = fact(n - 1) * n;
  9.          return result;
  10.      }
  11.  } 
  12.  class Output 
  13.  {
  14.      static void main(String args[]) 
  15.      {
  16.          maths obj = new maths() ;
  17.          Console.WriteLine(obj.fact(4)*(3));
  18.      }
  19.  }
a) 64
b) 60
c) 72
d) 84
Answer: c

Explanation: 4! = 4 * 3 *2 * 1 = 24 * 3 = 72.Not factorial of 3 but just multiply the number with 3.
Output : 72
8. Which of these data types is used by operating system to manage the Recursion in Csharp?

a) Array
b) Queue
c) Tree
d) Stack
Answer: d
9. What will be the output of the given code snippet?
  1.  class maths
  2.  {
  3.      public int fact(int n)
  4.      {
  5.          int result;
  6.          result = fact(n - 1) * n;
  7.          return result;
  8.      }
  9.  } 
  10.  class Program
  11.  {
  12.      static void Main(string[] args)
  13.      {            
  14.          maths obj = new maths();
  15.          Console.WriteLine(obj.fact(4));
  16.          Console.ReadLine();
  17.      }
  18.  }
a) 24
b) 30
c) Compile time error
d) Runtime Error
Answer: d

Explanation: Absence of base case condition. So absence of limit or end of for execution of a loop and hence results in stackoverflow exception error.
10. What will be the correct output for the given code snippet?
  1.  class maths
  2.  {
  3.      public int fact(int n)
  4.      {
  5.          int result;
  6.          if (n == 2)
  7.          return 1;
  8.          result = fact(n - 1) * n;
  9.          return result;
  10.      }
  11.  } 
  12.  class Program
  13.  {
  14.      static void Main(string[] args)
  15.      {            
  16.          maths obj = new maths();
  17.          Console.WriteLine(obj.fact(4));
  18.          Console.ReadLine();
  19.      }
  20.  }
a) 24
b) 0
c) 12
d) 1
Answer: c

Explanation: fact() calculates factorial of number ‘4’ but this time base case condition is executed upto 2 only.As soon as n reaches 2 it returns 2.

Related

Multiple Choice Questions 387032105426660479

Post a Comment

emo-but-icon

item