Data Structure Question Bank on “Sum of Digits of a Number using Recursion”.
1. Which of the following methods can be used to find the sum of digits of a number? Answer: d 2. What can be the maximum sum of digits for a 4 digit number? Answer: c 3. What can be the minimum sum of digits for a 4 digit number? Answer: b 4. Consider the following iterative implementation to find the sum of digits of a number: Which of the following lines should be inserted to complete the above code? Answer: b 5. What is the output of the following code? a) 1 Answer: d 6. Consider the following recursive implementation to find the sum of digits of number: Which of the following lines should be inserted to complete the above code? Answer: c 7. What is the time complexity of the following recursive implementation to find the sum of digits of a number n? a) O(n) Answer: c 8. What is the output of the following code? a) 10 Answer: b 9. How many times is the function recursive_sum_of_digits() called when the following code is executed? a) 6 10. You have to find the sum of digits of a number given that the number is always greater than 0. Which of the following base cases can replace the base case for the below code? a) if(n == 0) return 1 11. What is the output of the following code? a) 0 12. What is the output of the following code? a) 0 Answer: c
a) Recursion
b) Iteration
c) Greedy algorithm
d) Both recursion and iteration
Clarification: Both recursion and iteration can be used to find the sum of digits of a number.
a) 1
b) 16
c) 36
d) 26
Clarification: The sum of digits will be maximum when all the digits are 9. Thus, the sum will be maximum for the number 9999, which is 36.
a) 0
b) 1
c) 16
d) 36
Clarification: The sum of digits will be minimum for the number 1000 and the sum is 1.#include
a) sm += n
b) sm += n%10
c) sm += n-10
d) sm += n/10
Clarification: The line “sm += n % 10” adds the last digit(LSB) of the number to the current sum. Thus, the line “sm += n%10” should be added to complete the above code.#include
b) 3
c) 7
d) 10
Clarification: The above code prints the sum of digits of the number 1234, which is 10.#include
a) (n / 10) + recursive_sum_of_digits(n % 10)
b) (n) + recursive_sum_of_digits(n % 10)
c) (n % 10) + recursive_sum_of_digits(n / 10)
d) (n % 10) + recursive_sum_of_digits(n % 10)
View Answer
Clarification: The line “(n % 10) + recursive_sum_of_digits(n / 10)” should be inserted to complete the above code.#include
b) O(1)
c) O(len(n)), where len(n) is the number of digits in n
d) O(1/2)
Clarification: The time complexity of the above recursive implementation to find the sum of digits of a number is O(len(n)).#include
b) 16
c) 15
d) 14
Clarification: The above code prints the sum of digits of the number 1234321, which is 16.#include
b) 7
c) 5
d) 9
Answer: c
Clarification: The function recursive_sum_of_digits() is called 8 times, when the following code is executed.#include
b) if(n == 1) return 0
c) if(n == 1) return 1
d) no need to modify the base case
Answer: d
Clarification: None of the above mentioned base cases can replace the base case if(n == 0) return 0.#include
b) 1
c) runtime error
d) -1
Answer: b
Clarification: The program prints the sum of digits of the number 10000, which is 1.#include
b) 1
c) 2
d) 3
Clarification: The code prints the count of numbers between 1 and 20 such that the sum of their digits is 3. There are only two such numbers: 3 and 12.