ProgrammingHardTCS NQT · Infosys
What is printed?
What is printed?
int pal(const char *s, int i, int j) {
if (i >= j) return 1;
if (s[i] != s[j]) return 0;
return pal(s, i + 1, j - 1);
}
int main(void) {
const char *w[] = {"level", "abca", "a", "noon", "abcab"};
int c = 0;
for (int k = 0; k < 5; k++) c += pal(w[k], 0, (int)strlen(w[k]) - 1);
printf("%d", c);
}- A. 3
- B. 2
- C. 5
- D. 4
Correct answer: A. 3
level, a and noon are palindromes (a single letter hits i >= j at once). abca fails at b vs c, and abcab fails at once because 'a' ≠ 'b'. So 3. '4' miscounts abca, which matches only at its ends.
More C Programming questions
- With 4-byte int, what is printed? c typedef int Row[3]; Row m[2] = {{1, 2, 3}, {4, 5, 6}}; Row p = m; printf("%zu %d", sizeof(Row)…
- What is printed? c int a = 0, b = 0, c = 0; if ((a = 2) && (b = 0) || (c = 3)) a++; printf("%d %d %d", a, b, c);
- Which line has undefined behaviour? c char a[] = "hi"; char p = "hi"; a[0] = 'H'; / (i) / p[0] = 'H'; / (ii) / char q = a; q[1] = 'I'; /…
- What is printed? c int calls = 0; int C(int n, int k) { calls++; return (k == 0 || k == n) ? 1 : C(n - 1, k - 1) + C(n - 1, k); } int…
- What is printed? c enum E { A = -2, B, C, D = A + 10, E2 }; printf("%d %d %d", B, C, E2);
- What is printed? c int steps(int n) { return n == 1 ? 0 : 1 + steps(n % 2 ? 3 n + 1 : n / 2); } int main(void) { printf("%d %d", steps(6)…