ProgrammingHardTCS NQT · Accenture
How many times does the letter a occur in "panda"? The program counts with recursion.
How many times does the letter a occur in "panda"? The program counts with recursion.
#include <stdio.h>
int cnt(const char *s) {
if (*s == '\0') return 0;
return (*s == 'a') + cnt(s + 1);
}
int main(void) {
printf("%d", cnt("panda"));
}- A. 3
- B. 2
- C. 4
- D. 1
Correct answer: B. 2
The recursion adds 1 for each a it meets and recurses on the rest of the string, and panda has two a's, so it prints 2.
More C Programming questions
- What is printed by this recursive Pascal triangle entry? c #include <stdio.h> int C(int n, int k) { if (k == 0 || k == n) return 1; return…
- What is printed? c #include <stdio.h> int main(void) { int c = 0; for (int i = 0; i < 5; i++) { switch (i) { case 2: break; default: c++…
- What is printed? c #include <stdio.h> int main(void) { int c = 0; for (int i = 0; i < 5; i++) { switch (i) { case 1: continue; } c++; }…
- What is printed? c #include <stdio.h> int main(void) { int m[2][3] = {{1, 2, 3}, {4, 5, 6}}; printf("%d", ((m + 1) + 2)); }
- What is printed? c #include <stdio.h> int f(void) { static int a[3]; a[0]++; return a[0]; } int main(void) { int x = f(); int y = f(); int…
- What is printed? Assume short is 2 bytes with 2-byte alignment and char is 1 byte. c #include <stdio.h> struct S { short a; char b; }; int…