ProgrammingHardWipro · Cognizant pattern
What is printed?
What is printed?
int isPalindrome(const char *s, int left, int right) {
if (left >= right) return 1;
if (s[left] != s[right]) return 0;
return isPalindrome(s, left + 1, right - 1);
}
int main(void) {
const char *str = "racecar";
printf("%d", isPalindrome(str, 0, 6));
}- A. 0
- B. 7
- C. Undefined behaviour: recursion depth depends on string length
- D. 1
Correct answer: D. 1
isPalindrome compares characters from both ends inward, recursing only while they match. "racecar" reads the same forwards and backwards, so every comparison succeeds all the way until left >= right (meeting in the middle), returning 1 (true).
More C Programming questions
- What is printed? c int f(int n) { if (n == 0) return 0; printf("%d ", n % 10); return f(n / 10); } int main(void) { f(4567); }
- What is printed? c void printPattern(int n) { if (n == 0) return; printPattern(n - 1); for (int i = 0; i < n; i++) printf(""); printf("…
- What is printed? c int f(int n) { if (n == 0) return 1; int a = f(n - 1); return a n; } int main(void) { long long result = 1; for (int i…
- What happens? c struct Node { int data; struct Node next; }; struct Node createNode(int val) { struct Node n; n.data = val; n.next = NULL…
- What is printed? c void outerFunc(void) { int x = 10; { int y = 20; printf("%d %d ", x, y); } // printf("%d", y); // line X } int…
- What is printed? c int idgenerator(void) { static int nextid = 100; return nextid++; } int main(void) { int a = idgenerator(); int b =…