GATE Computer ScienceHardGATE CS

Assume uint16_t is a 16-bit unsigned type, int is 32 bits and the characters are ASCII. What is printed? uint16_t h = 7; for (const char *p = "GATECS2025"; *p; p++) h = h * 31 + *p; printf("%u", h);

  1. A. 59716
  2. B. 59715
  3. C. 24179
  4. D. 65535

Correct answer: B. 59715

Each step computes h·31 + c in int and converts back to uint16_t, which reduces the value modulo 65536 (well defined for unsigned types). Iterating over the ten characters gives 59715.

More Programming and Data Structures (GATE) questions

Exams that ask this topic