Data Structure Questions and Answers for Aptitude test on “Sum of n Natural Numbers using Recursion”.
1. Which of the following option is wrong about natural numbers? Answer: d 2. Which of the following gives the sum of the first n natural numbers? 3. Consider the following iterative solution to find the sum of first n natural numbers: Which of the following lines completes the above code? Answer: b 4. What is the output of the following code? a) 55 Answer: d 5. What is the time complexity of the following iterative method used to find the sum of the first n natural numbers? a) O(1) Answer: b 6. Consider the following code: Which of the following lines is the recurrence relation for the above code? Answer: c 7. Consider the following code: Which of the following is the base case for the above recursive code? Answer: a 8. What is the time complexity of the following recursive implementation used to find the sum of the first n natural numbers? a) O(1) 9. Which of the following methods used to find the sum of first n natural numbers has the least time complexity? Answer: c 10. What is the output of the following code? a) 10 11. How many times is the function recursive_sum() called when the following code is executed? a) 4 Answer: c 12. What is the output of the following code? a) -1 Answer: b 13. What is the output of the following code? a) 0
a) Sum of first n natural numbers can be calculated by using Iteration method
b) Sum of first n natural numbers can be calculated by using Recursion method
c) Sum of first n natural numbers can be calculated by using Binomial coefficient method
d) No method is prescribed to calculate sum of first n natural number
Clarification: All of the above mentioned methods can be used to find the sum of first n natural numbers.
a) nC2
b) (n-1)C2
c) (n+1)C2
d) (n+2)C2
Answer: c
Clarification: The sum of first n natural numbers is given by n*(n+1)/2, which is equal to (n+1)C2.#include
a) sm = i
b) sm += i
c) i = sm
d) i += sm
Clarification: The line “sm += i” completes the above code.#include
b) 45
c) 35
d) Depends on compiler
Clarification: Since the variable “sm” is not initialized to 0, it will produce a garbage value. Some compiler will automatically initialises variables to 0 if not initialised. In that case the value is 55. Hence the value depends on the compiler.#include
b) O(n)
c) O(n2)
d) O(n3)
Clarification: The time complexity of the above iterative method used to find the sum of first n natural numbers is O(n).#include
a) (n – 1) +recursive_sum(n)
b) n + recursive_sum(n)
c) n + recursive_sum(n – 1)
d) (n – 1) + recursive_sum(n – 1)
Clarification: The recurrence relation for the above code is: n + recursive_sum(n – 1).#include
a) if(n == 0)
b) return 0
c) return n + recursive_sum(n – 1)
d) if(n == 1)
Clarification: “if(n == 0)” is the base case for the above recursive code.#include
b) O(n)
c) O(n2)
d) O(n3)
Answer: b
Clarification: The time complexity of the above recursive implementation used to find the sum of first n natural numbers is O(n).
a) Recursion
b) Iteration
c) Binomial coefficient
d) All have equal time complexity
View Answer
Clarification: Recursion and iteration take O(n) time to find the sum of first n natural numbers while binomial coefficient takes O(1) time.#include
b) 15
c) 21
d) 14
Answer: b
Clarification: The above code prints the sum of first 5 natural numbers, which is 15.#include
b) 5
c) 6
d) 7
Clarification: The function recursive_sum is called 6 times when the following code is executed.#include
b) 0
c) 1
d) runtime error
Clarification: The program prints the sum of first 0 natural numbers, which is 0.#include
b) -10
c) 1
d) runtime error
Answer: d
Clarification: The above code doesn’t handle the case of negative numbers and so the function recursive_sum() will be called again and again till the stack overflows and the program produces a runtime error.
