ProgrammingVery hardProduct company OA
What does r(3) print, and how many digits would r(6) print?
What does r(3) print, and how many digits would r(6) print?
void r(int n) {
if (n == 0) return;
r(n - 1);
printf("%d", n);
r(n - 1);
}- A. 3212123, and 63 digits
- B. 121312, and 63 digits
- C. 1213121, and 64 digits
- D. 1213121, and 63 digits
Correct answer: D. 1213121, and 63 digits
r(n) is r(n − 1), then n, then r(n − 1) again: r(1) = 1, r(2) = 121, r(3) = 1213121. The length obeys L(n) = 2L(n − 1) + 1 with L(0) = 0, so L(n) = 2^n − 1 and r(6) prints 63 digits.
More C Programming questions
- What is printed? c const char s = "abcdefgh"; const char p = s + 2, q = strchr(s, 'g'); printf("%td %c %s", q - p, (p + (q - p) / 2), q);
- Given int f(int); and int (fp)(int) = f; which of these calls are valid? (i) fp(2) (ii) (fp)(2) (iii) (fp)(2) (iv) (&fp)(2)
- What is printed? c union U { int i; float f; } u = {3.9}; printf("%d", u.i);
- What is printed? c int i = 0, c = 0; while (i < 10 && ++c && i++ % 3 != 2); printf("%d %d", i, c);
- Given this allocation, which code frees everything without leaking or touching freed memory? c int m = malloc(3 sizeof m); for (int i = 0…
- With char a = "hi", b = "hi"; what can be said about the value of a == b?