ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
void p(const char *s) {
    if (!*s) return;
    putchar(*s);
    p(s + 2);
    putchar(*s);
}
int main(void) { p("abcdef"); }
  1. A. aceca
  2. B. ace
  3. C. acefeca
  4. D. aceeca

Correct answer: D. aceeca

Each call prints its character, recurses two characters further, then prints the same character again on the way back. Calls visit 'a', 'c', 'e'; from 'e' (index 4) s + 2 is index 6, the '\0', so the recursion stops there. Output: a c e on the way down, e c a on the way up → "aceeca". The step of 2 stays inside the 7-byte array, so it is well defined.

More C Programming questions

Exams that ask this topic