ProgrammingHardProduct company OA
What is printed?
What is printed?
int sub(const char *a, const char *b) { /* is a a subsequence of b? */
if (!*a) return 1;
if (!*b) return 0;
return sub(*a == *b ? a + 1 : a, b + 1);
}
int main(void) { printf("%d%d%d", sub("ace", "abcde"), sub("aec", "abcde"), sub("", "x")); }- A. 101
- B. 111
- C. 100
- D. 001
Correct answer: A. 101
The function walks b and advances in a only on a match, so it checks whether a's letters appear in b in order. a, c, e do; for aec, after a and e there is no c left. The empty string is a subsequence of anything.
More C Programming questions
- What is printed? c char src[] = "abc", dst[8]; char d = dst; const char s = src; while ((d++ = s++)); printf("%s %td %td", dst, d - dst, s…
- What does calloc(n, size) guarantee that malloc(n size) does not?
- What is printed? c int a[3][3] = {{1}, {2, 3}, [2][1] = 7}; printf("%d %d %d %d", a[0][1], a[1][1], a[2][0], a[2][1]);
- What does f(6) print? c void f(int n) { printf("%d", n); if (n > 1) f(n / 2); printf("%d", n); }
- What is printed? c sizet a = 3, b = 5; if (a - b > 0) puts("bigger"); else puts("smaller");
- What is printed? c int g(int a, int b) { return a == b ? a : a > b ? g(a - b, b) : g(a, b - a); } int main(void) { printf("%d", g(48…