Data Structure Questions and Answers on Factorial using Recursion for Freshers


1. Which of the following methods can be used to find the factorial of a number?

a) Recursion
b) Iteration
c) Dynamic programming
d) All of the mentioned
Answer: d

Explanation: All of the above mentioned methods can be used to find the factorial of a number.
2. Which of the following recursive formula can be used to find the factorial of a number?

a) fact(n) = n * fact(n)
b) fact(n) = n * fact(n+1)
c) fact(n) = n * fact(n-1)
d) fact(n) = n * fact(1)
Answer: c

Explanation: fact(n) = n * fact(n – 1) can be used to find the factorial of a number.
3. Consider the following iterative implementation to find the factorial of a number:
int main()
{
    int n = 6, i;
    int fact = 1;
    for(i=1;i<=n;i++)
      _________;
    printf("%d",fact);
    return 0;
}
Which of the following lines should be inserted to complete the above code?

a) fact = fact + i
b) fact = fact * i
c) i = i * fact
d) i = i + fact
Answer: b

Explanation: The line “fact = fact * i” should be inserted to complete the above code.
4. Consider the following recursive implementation to find the factorial of a number:
int fact(int n)
{
     if(_________)
        return 1;
     return n * fact(n - 1);
}
int main()
{
      int n = 5;
      int ans = fact(n);
      printf("%d",ans);
      return 0;
}
Which of the following lines should be inserted to complete the above code?

a) n = 0
b) n != 0
c) n == 0
d) n == 1
Answer: c

Explanation: The line “n == 0” should be inserted to complete the above code.
Note: “n == 1” cannot be used because it does not take care of the case when n = 0, i.e when we want to find the factorial of 0.
5. What is the time complexity of the above recursive implementation to find the factorial of a number?

a) O(1)
b) O(n)
c) O(n2)
d) O(n3)
Answer: b

Explanation: The time complexity of the above recursive implementation to find the factorial of a number is O(n).
6. What is the space complexity of the above recursive implementation to find the factorial of a number?

a) O(1)
b) O(n)
c) O(n2)
d) O(n3)
Answer: a

Explanation: The space complexity of the above recursive implementation to find the factorial of a number is O(1).
7. Consider the following recursive implementation to find the factorial of a number:
int fact(int n)
{
     if(n == 0)
        return 1;
     return n * fact(n - 1);
}
int main()
{
      int n = 5;
      int ans = fact(n);
      printf("%d",ans);
      return 0;
}
Which of the following lines is the base case?

a) return 1
b) return n * fact(n-1)
c) if(n == 0)
d) none of the mentioned
Answer: c

Explanation: The line “if(n == 0)” is the base case.
8. What is the output of the following code?
int fact(int n)
{
      if(n == 0)
        return 1;
      return n * fact(n - 1);
}
int main()
{
      int n = 0;
      int ans = fact(n);
      printf("%d",ans);
      return 0;
}
a) 0
b) 1
c) 2
d) 3
Answer: b

Explanation: The program prints 0!, which is 1.
9. What is the output of the following code?
int fact(int n)
{
      if(n == 0)
        return 1;
      return n * fact(n - 1);
}
int main()
{
      int n = 1;
      int ans = fact(n);
      printf("%d",ans);
      return 0;
}
a) 0
b) 1
c) 2
d) 3
Answer: b

Explanation: The program prints 1!, which is 1.
10. How many times will the function fact() be called when the following code is executed?
int fact(int n)
{
      if(n == 0)
        return 1;
      return n * fact(n - 1);
}
int main()
{
      int n = 5;
      int ans = fact(n);
      printf("%d",ans);
      return 0;
}
a) 4
b) 5
c) 6
d) 7
Answer: c

Explanation: The fact() function will be called 6 times with the following arguments:
fact(5), fact(4), fact(3), fact(2), fact(1), fact(0).
11. What is the output of the following code?
int fact(int n)
{
      if(n == 0)
        return 1;
      return n * fact(n - 1);
}
int main()
{
      int n = 5;
      int ans = fact(n);
      printf("%d",ans);
      return 0;
}
a) 24
b) 120
c) 720
d) 1
Answer: b

Explanation: The function prints 5!, which is 120.

Related

C# Questions & Answers on Properties and its Applications for Freshers

1. The correct way to implement a read only property add, in a math class is?a) class math { int ad; public int add { get { ...

Linux Questions & Answers on Linux Commands for Freshers– 2

1. Command used to determine the path of an executable file is a) whichb) wherec) wexecd) what Answer: a 2. Command used to count number of character in a file is a) grepb) wcc) countd) cut Ans...

C# Questions & Answers on Introduction of Properties for Freshers

1. Choose the wrong statement about the properties used in C#.NET? a) Each property consists of accessor as get and setb) A property cannot be either read or write onlyc) Properties can be used to ...

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

item