ProgrammingHardProduct company OA

What is printed?

What is printed?
int hs(unsigned n) { return n ? (n & 0xF) + hs(n >> 4) : 0; }
int main(void) { printf("%d", hs(0xBEEF)); }
  1. A. 15
  2. B. 48
  3. C. 4
  4. D. 54

Correct answer: D. 54

n & 0xF is the lowest hex digit and n >> 4 drops it, so hs sums the hex digits: B + E + E + F = 11 + 14 + 14 + 15 = 54. '4' counts the digits instead of adding them.

More C Programming questions

Exams that ask this topic