ProgrammingHardProduct company OA
What is printed?
What is printed?
int f(int n, int b) { return n < b ? 1 : 1 + f(n / b, b); }
int main(void) { printf("%d %d %d", f(1000, 2), f(1000, 10), f(999, 10)); }- A. 9 3 3
- B. 11 4 3
- C. 10 3 3
- D. 10 4 3
Correct answer: D. 10 4 3
f counts the digits of n in base b. 1000 lies between 2^9 = 512 and 2^10, so it needs 10 binary digits; in base 10 it has 4 digits and 999 has 3. '9' would be floor(log2 1000) without the +1.
More C Programming questions
- What is printed? c const char s = "education"; int v = 0, o = 0; for (; s; s++) switch (s) { case 'a': case 'e': case 'i': case 'o': case…
- Which of these functions return a pointer that is still valid to read after the function returns? c char f1(void) { char s[] = "hi"…
- Assume 32-bit unsigned int. What is printed? c unsigned int x = 65536; printf("%u %u", x x, x (x - 1) / 2);
- What is printed? c int i = 0; while (i++ < 5); printf("%d", i);
- What is printed? c #define LEVEL 2 #if LEVEL > 1 #define MSG "high" #elif LEVEL > 0 #define MSG "low" #endif #undef LEVEL #ifndef LEVEL…
- What is printed? c struct E { char name[8]; int age; }; struct E t[] = {{"Asha", 31}, {"Bilal", 27}, {"Chen", 45}, {"Dev", 22}}; struct E…