ProgrammingHardProduct company OA
What is printed?
What is printed?
int f(int n) {
static int calls;
if (n == 0) { int c = calls; calls = 0; return c; }
calls++;
return f(n - 1);
}
int main(void) {
printf("%d ", f(3));
printf("%d", f(5));
}- A. 0 0
- B. 4 6
- C. 3 8
- D. 3 5
Correct answer: D. 3 5
calls counts the non-base calls and the base case hands back the count and resets it, so each top-level call reports its own depth: 3, then 5. Without the reset the second would print 8.
More C Programming questions
- What is printed? c enum Op { ADD, SUB, MUL }; int add(int a, int b) { return a + b; } int sub(int a, int b) { return a - b; } int mul(int…
- How many palindromic substrings does this code count? c const char s = "abba"; int n = 4, c = 0; for (int i = 0; i < n; i++) for (int j =…
- Assume 32-bit unsigned int. What is printed? c unsigned x = 0x27; / 10 0111 / printf("%u %u", ~x & (x + 1), x | (x + 1));
- What is printed? c int r0[] = {1, 2}, r1[] = {3, 4, 5}, r2[] = {6}; int rows[] = {r0, r1, r2}; int len[] = {2, 3, 1}; int s = 0; for (int…
- This prints every permutation of "abc". What is the FIFTH string printed? c void p(char s, int k) { if (!s[k]) { printf("%s ", s); return…
- What is printed? c int a = 1; printf("%d %d", a << 2 + 1, (a << 2) + 1 > 4 == 1);