ProgrammingHardTCS NQT · Infosys
A recursive function is written as int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }. What is gcd(1071, 462)?
- A. 7
- B. 42
- C. 21
- D. 3
Correct answer: C. 21
Euclid's steps are 1071 mod 462 = 147, 462 mod 147 = 21, 147 mod 21 = 0, so the answer is 21. The recursion depth is only logarithmic in the inputs.
More C Programming questions
- Starting at n = 27, the loop while (n != 1) { n = (n % 2) ? 3 n + 1 : n / 2; steps++; } runs with int n. What is steps at the end?
- Using square-and-multiply on int values with a reduction modulo 1000 after each multiplication, what is 3^13 mod 1000?
- How many 6-bit masks have exactly two 1 bits and have bit 0 cleared?
- The binary-reflected Gray code of i is g(i) = i ^ (i >> 1). What is g(4)?
- What is popcount(0xFFFF0000 ^ 0x0F0F0F0F)?
- int q = NULL; if (q != NULL && q == 3) printf("yes"); else printf("no"); What is printed?