ProgrammingHardTCS NQT · Infosys
A recursive function sums decimal digits: int ds(int n) { return n < 10 ? n : n % 10 + ds(n / 10); }. What is ds(98765)?
- A. 29
- B. 41
- C. 35
- D. 26
Correct answer: C. 35
The digits are 9, 8, 7, 6 and 5, which sum to 35. The recursion goes one level per digit, so its depth is the number of digits.
More C Programming questions
- How many 1 bits does 0xDEADBEEF have in binary?
- uint32t value = (uint32t)-1; printf("%d", value == 0xFFFFFFFFu); What is printed?
- How many index pairs i < j in the array {3, 5, 3, 5, 7} satisfy a[i] XOR a[j] == 0?
- How many integers from 1 to 30 are multiples of 8?
- A circular buffer of size n = 5 steps back from index 0 with (idx + n - 1) % n. What index results?
- A qsort comparator orders the elements in descending order. After sorting {5, 9, 2}, what is the first element?