250+ TOP MCQs on Sum of Digits of a Number using Recursion and Answers

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?
a) Recursion
b) Iteration
c) Greedy algorithm
d) Both recursion and iteration

Answer: d
Clarification: 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) 26

Answer: c
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.

3. What can be the minimum sum of digits for a 4 digit number?
a) 0
b) 1
c) 16
d) 36

Answer: b
Clarification: 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:

#include
int 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
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.

5. What is the output of the following code?

#include
int 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

Answer: d
Clarification: 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:

#include
int 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)
View Answer

Answer: c
Clarification: 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 following recursive implementation to find the sum of digits of a number n?

#include
int 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;
}

a) O(n)
b) O(1)
c) O(len(n)), where len(n) is the number of digits in n
d) O(1/2)

Answer: c
Clarification: 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?

#include
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 = 1234321;
      int ans = recursive_sum_of_digits(n);
      printf("%d",ans);
      return 0;
}

a) 10
b) 16
c) 15
d) 14

Answer: b
Clarification: 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?

#include
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 = 1201;
      int ans = recursive_sum_of_digits(n);
      printf("%d",ans);
      return 0;
}

a) 6
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.

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?

#include
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 = 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) 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.

11. What is the output of the following code?

#include
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 = 10000;
      int ans = recursive_sum_of_digits(n);
      printf("%d",ans);
      return 0;
}

a) 0
b) 1
c) runtime error
d) -1
Answer: b
Clarification: The program prints the sum of digits of the number 10000, which is 1.

12. What is the output of the following code?

#include
int 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

Answer: c
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.

Leave a Reply

Your email address will not be published. Required fields are marked *