ProgrammingVery hardProduct company OA

What is printed by r("ABCDE", 0)?

What is printed by r("ABCDE", 0)?
void r(const char *s, int d) {
    if (!*s) return;
    if (d % 2) putchar(*s);
    r(s + 1, d + 1);
    if (d % 2 == 0) putchar(*s);
}
  1. A. BDECA
  2. B. BDACE
  3. C. BDEAC
  4. D. ACEBD

Correct answer: A. BDECA

Characters at odd depth (B, D) print on the way down, before the recursive call. Characters at even depth (A, C, E) print on the way back up, so they come out in reverse: E, C, A. Together: BDECA. 'BDACE' prints the even ones in forward order.

More C Programming questions

Exams that ask this topic