The Gateway to Computer Science Excellence
+28 votes
5.7k views

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 $\text{fun1}(5)$ is called is

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

11 Answers

0 votes
answer is A
by (391 points)
0 votes

Question can be answered in 20-30 sec

Answering to this question is very simple(elimination technique).

fun1(){

print(%d)// prints value

fun2(n-2)

print(%d)// prints same value

}

if we analyze this properly value wont change in function calling of fun1{ fun2(n-2) }.

 

first and second print statement will print same value


so starting value is 5 and ending value will be even 5.

Therefore b & d are eliminated.


fun2(){

print(%d)// prints value

fun1(++n)

print(%d)// prints Incremented value because of pre increment

}

so by analyzing this we can eliminate c.

Answer is A

by (227 points)
edited by
0 votes

For fun1(5)

fun1(5)➣ fun2(3) ➣fun1(4) ➣fun2(2)➣ fun1(3) ➣fun2(1) ➣fun1(2) ➣fun2(0)

   ↓               ↓              ↓             ↓              ↓             ↓               ↓            ↓

   5               3              4              2                3             1                2            2 

                                                                                                                    [return to fun2(n-2)   printf("%d",n) ➣ n-2=0 ➣ n=2] Image result for circular left arrow symbol

  ↓                    ↓              ↓                       ↓                 ↓                 ↓

  5                   4              4                      3                  3                  2        

 now return values to fun1 , fun2       

for fun1 return n   

for fun2  return n+2 Bcz   fun2(n-2) instruction pointer on 'n' that's n   .

so final answer is : option A : 53423122233445 

                                                           

 

by Junior (977 points)
Answer:

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,312 answers
198,343 comments
105,038 users