The Gateway to Computer Science Excellence
+28 votes

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);
    printf("%d", n);

The output printed when $\text{fun1}(5)$ is called is

  1. $53423122233445$  
  2. $53423120112233$                       
  3. $53423122132435$  
  4. $53423120213243$                       
in Programming by Veteran (431k points)
edited by | 5.8k views

11 Answers

+58 votes
Best answer

  • Unroll recursion up to a point where we can distinguish the given options and choose the correct one!
  • Options B and D are eliminated.
  • A is the answer.
by Veteran (57.2k points)
selected by
@Debashish could u tell me what is the difference between

if we replace fun1(++n) with fun1(n+1) ?

and also fun1(n+1) with fun1(n-1)?
let n=3, then fun1(++n) is equivalent to call fun1(4) and at the time value of n get modified to n=4.
whereas fun1(n+1) is equivalent to call fun1(4) but value of n will not get effected i.e ll remains same to n=3.

in 2nd part, fun1(n+1) is just equal to call fun1(4)  and fun1(n-1) is equivalent to fun1(2),
in this case for both value of n will remains same i.e n=3.
yes, but in case of printing ++n is getting immediate value.

but n-2 is not getting immediate value, it printing previous value, why?
bcz ++n is equivalent to write n=n+1(increment+assign at same time)
where as n-2 is not equivalent to say n=n-2
n-2 calculated at the time but it result will not assigned to it you can use that intermediate for sort duration(at that time only) after that it automatically erased
where u got this property for preincrement?

Is postincrement work in same way?
According to dennis ritchie






do same operation


means increment and assignment done in same time.
yes final result of all three expression ll be same only if pi=&y

here first assign then modify the value in foo() function call.

that is why it goes to infinite loop rt?

@Debashish Deka



This is what we call a "GATE" mind (y)
(++n) => modification
+27 votes

hence correct answer is a.

by Loyal (8k points)
Thank you @Arnabi for giving solution
+11 votes


In fun2,value of n also get incremented after each function call.

by Active (4.8k points)
+6 votes



by Boss (42.5k points)
+5 votes

A is the correct answer

by (467 points)
+5 votes

Ans is A 

by Active (2.4k points)
I have another query...fun2(++n) and fun2(n++)----------- Both are same right?

Here, we won't check pre/post increment?
in fun2(++n) , let take fun2(3) , value of n=3 and its ++n so first update the value of n , become n=4 and then passed as fun1(4)

but in fun2(n++) , let take same example fun2(3) , value of n=3 and its n++ so first passed value as fun1(3) then change value of n=4

( here n is local variable )
+4 votes

regarding confusion between pre and post.

by Loyal (6.8k points)

see , if u say ++x means

first x+1

then x=x+1

that means x is changing 2 times in between 1 sequence point

which will give undefined behaviour

So, ur assumption is not correct for ++x

++x is only increment





k = ++x means pre-increment.

that means x is increased first by one then assign it k.

++n : n = n+1 or n + = 1;

but n-2 are different thing.

if n = 5

f1(n-2)  = f(5-2) = f(3) but n is still same.

but f2(++n) = f2(n = n + 1) =f2( n + = 1 ) =f2( n = 5 + 1 ) = f2( n = 6 ) = f2(6) and n=6(n is increased by 1)  

also f2(n++) = f2(n = n + 1) = f2( n + = 1 )= f2(5) and n=6(n is increased by 1)  

please correct me if i'm wrong??



In case of post increment A(x++) = A(x); x++;

Is these two operations are atomic, I mean new activation record will be created immediately after A(x) or after execution of x++;

after A(x) new activation record will be created.
0 votes
option A is coorect
by (365 points)

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,737 questions
57,367 answers
105,266 users