GATE CSE 2017 Session 1 Q45: Output of mutually recursive fun1(5) and fun2 with printf
- Programming and Data Structures
- 2 marks
- MCQ
- −2/3 if wrong
Syllabus topics: Recursion; Programming in C
Question
Consider the following two functions.
void fun1(int n) {
if(n == 0) return;
printf("%d", n);
fun2(n - 2);
printf("%d", n);
}
void fun2(int n) {
if(n == 0) return;
printf("%d", n);
fun1(++n);
printf("%d", n);
}The output printed when fun1(5) is called is
Try it: multiple choice: exactly one correct option
Show answer and explanation
Answer: A (official GATE 2017 final answer key)
Concept used: Tracing mutual recursion: each call prints before and after the nested call, and the pre-increment in fun2 changes the local n that is printed afterwards.
First step: Trace the calls fun1(5) → fun2(3) → fun1(4) → fun2(2) → fun1(3) → fun2(1) → fun1(2) → fun2(0), writing down the value of n printed on the way in, then unwind printing the (possibly incremented) n on the way out.
Full step-by-step solution
Loading…
Answer from the official final answer key. Found a mistake? Use Report issue and we will fix it.
More questions on Recursion
Every PYQ on this topic →Share this question
Source: official GATE 2017 Session 1 CS question paper and final answer key, organised by IIT Roorkee. Question paper · Answer key
More Programming and Data Structures PYQs
- 2026 Session 1 · Q23Non-leaf indices in a 1-indexed binary heap with n odd
- 2026 Session 1 · Q24Empty slots after linear-probing inserts with h(x) = (x+7) mod 11
- 2026 Session 1 · Q33Maximum height of a full binary tree with 23 nodes
- 2026 Session 1 · Q34Output of a C function with a shadowed block variable
- 2026 Session 1 · Q39Recursive length of a non-empty singly linked list
- 2026 Session 1 · Q40Third insertion that can never yield a complete BST of 1..15