Data Structure Questions and Answers on Sum of Digits of a Given Number using Recursion

https://www.computersprofessor.com/2017/11/data-structure-questions-and-answers-on_16.html
1. Which of the following methods can be used to find the sum of digits of a number?
a) Recursion
b) Iteration
c) Greedy algorithm
d) Both recursion and iteration
Answer: d
Explanation: Both recursion and iteration can be used to find the sum of digits of a number.
2. What can be the maximum sum of digits for a 4 digit number?
a) 1
b) 16
c) 36
d) none of the mentioned
Answer: c
Explanation: 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.
3. What can be the minimum sum of digits for a 4 digit number?
a) 0
b) 1
c) 16
d) 36
a) 0
b) 1
c) 16
d) 36
Answer: b
Explanation: The sum of digits will be minimum for the number 1000 and the sum is 1.
4. Consider the following iterative implementation to find the sum of digits of a number:
#includeint sum_of_digits(int n) { int sm = 0; while(n != 0) { _________; n /= 10; } return sm; } int main() { int n = 1234; int ans = sum_of_digits(n); printf("%d",ans); return 0; }
Which of the following lines should be inserted to complete the above code?
a) sm += n
b) sm += n%10
c) sm += n-10
d) sm += n/10
Answer: b
Explanation: 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.
5. What is the output of the following code?
#includeint sum_of_digits(int n) { int sm = 0; while(n != 0) { sm += n%10; n /= 10; } return sm; } int main() { int n = 1234; int ans = sum_of_digits(n); printf("%d",ans); return 0; }
a) 1
b) 3
c) 7
d) 10
b) 3
c) 7
d) 10
Answer: d
Explanation: The above code prints the sum of digits of the number 1234, which is 10.
6. Consider the following recursive implementation to find the sum of digits of number:
#includeint recursive_sum_of_digits(int n) { if(n == 0) return 0; return _________; } int main() { int n = 1201; int ans = recursive_sum_of_digits(n); printf("%d",ans); return 0; }
Which of the following lines should be inserted to complete the above code?
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)
Answer: c
Explanation: The line “(n % 10) + recursive_sum_of_digits(n / 10)” should be inserted to complete the above code.
7. What is the time complexity of the above recursive implementation to find the sum of digits of a number n?
a) O(n)
b) O(1)
c) O(len(n)), where len(n) is the number of digits in n
d) none of the mentioned
Answer: c
Explanation: The time complexity of the above recursive implementation to find the sum of digits of a number is O(len(n)).
8. What is the output of the following code?
#includeint recursive_sum_of_digits(int n) { if(n == 0) return 0; return n % 10 + recursive_sum_of_digits(n/10); } int main() { int n = 1234321; int ans = recursive_sum_of_digits(n); printf("%d",ans); return 0; }
a) 10
b) 16
c) 15
d) none of the mentioned
b) 16
c) 15
d) none of the mentioned
Answer: b
Explanation: The above code prints the sum of digits of the number 1234321, which is 16.
9. How many times is the function recursive_sum_of_digits() called when the following code is executed?
#includeint recursive_sum_of_digits(int n) { if(n == 0) return 0; return n % 10 + recursive_sum_of_digits(n/10); } int main() { int n = 1201; int ans = recursive_sum_of_digits(n); printf("%d",ans); return 0; }
a) 6
b) 7
c) 8
d) 9
b) 7
c) 8
d) 9
Answer: c
Explanation: The function recursive_sum_of_digits() is called 8 times, when the following code is executed.
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?
#includeint recursive_sum_of_digits(int n) { if(n == 0) return 0; return n % 10 + recursive_sum_of_digits(n/10); } int main() { int n = 1201; int ans = recursive_sum_of_digits(n); printf("%d",ans); return 0; }
a) if(n == 0) return 1
b) if(n == 1) return 0
c) if(n == 1) return 1
d) none of the mentioned
b) if(n == 1) return 0
c) if(n == 1) return 1
d) none of the mentioned
Answer: d
Explanation: None of the above mentioned base cases can replace the base case if(n == 0) return 0.
11. What is the output of the following code?
#includeint recursive_sum_of_digits(int n) { if(n == 0) return 0; return n % 10 + recursive_sum_of_digits(n/10); } int main() { int n = 10000; int ans = recursive_sum_of_digits(n); printf("%d",ans); return 0; }
a) 0
b) 1
c) runtime error
d) none of the mentioned
b) 1
c) runtime error
d) none of the mentioned
Answer: b
Explanation: The program prints the sum of digits of the number 10000, which is 1.
12. What is the output of the following code?
#includeint cnt =0; int my_function(int n, int sm) { int i, tmp_sm; for(i=1;i<=n;i++) { tmp_sm = recursive_sum_of_digits(i); if(tmp_sm == sm) cnt++; } return cnt; } int recursive_sum_of_digits(int n) { if(n == 0) return 0; return n % 10 + recursive_sum_of_digits(n/10); } int main() { int n = 20, sum = 3; int ans = my_function(n,sum); printf("%d",ans); return 0; }
a) 0
b) 1
c) 2
d) 3
b) 1
c) 2
d) 3
Answer: c
Explanation: 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