0% found this document useful (0 votes)
3 views6 pages

Functions & Storage Classes - DPP 04 - Parakram GATE 2024 Computer Science Weekday (Hinglish)

The document contains a series of programming problems and solutions related to recursion in C, including function definitions and expected outputs. It includes multiple-choice questions (MCQs) and numerical answer type (NAT) questions, along with hints and solutions for each problem. An answer key is provided at the end for quick reference.

Uploaded by

01ankur2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

Functions & Storage Classes - DPP 04 - Parakram GATE 2024 Computer Science Weekday (Hinglish)

The document contains a series of programming problems and solutions related to recursion in C, including function definitions and expected outputs. It includes multiple-choice questions (MCQs) and numerical answer type (NAT) questions, along with hints and solutions for each problem. An answer key is provided at the end for quick reference.

Uploaded by

01ankur2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1

Branch : CSE & IT


Programming in C
Recursion -2 DPP-04

[NAT] [NAT]
1. Consider the following function: 4. Consider the following function:
int func(int a) int func(int n)
{ {
static int b=1; if(n>0){
b=b+a; return 3*func(n/4)+1;
if((b%a)%2!=0) return a+func(b+a); }
return b-a; return n;
} }
The value returned by func(5) is ___________. The value returned by func(24) is ________.

[MCQ] [NAT]
2. Consider the following function: 5. Consider the following function:
void func(int n) int func(int n)
{ {
if(n>0){ static int k=0;
func(n-1); if(n>0){
printf("%d\t", n); k++;
} return 2*func(n/2)+k;
printf("%d\t", n-1); }
} return n+k--;
The output printed by func(2) is- }
(a) –1 1 0 2 1 (b) –1 1 0 2 1 2 The value returned by func(8) is _________.
(c) –1 1 0 2 -2 1 (d) –1 1 0 2 1
[MCQ]
[NAT] 6. Consider the following function:
3. Consider the following function: int func(int n, int i)
int func(int n) {
{ if(n==0) return 0;
if(n>0){ else if(n%2){
return func(n/2)+func(n/4)+1; return func(n/2, 2*i)+i;
} }else return func(n/2, 2*i)-i;
return n+1; }
} The value returned by func(14, 1) is-
The value returned by func(6) is ______ (a) 1 (b) 13
(c) 15 (d) 0
2

[MCQ] [MCQ]
7. Consider the following function: 8. Consider the following function:
int func(int n) void display()
{ {
static int i=0; static int i;
if(n/2){ if(i<=printf("GATE24")){
i--; i=i+2;
return func(n/2)+i; display();
}else return i; }
} }
The value returned by func(7) is- int main()
(a) –6 {
(b) –12 int i=0;
(c) –18 for(i=0;i<3;i++)
(d) –21 display();
return 0;
}
The number of times printf() executed is-
(a) 6 (b) 5
(c) 7 (d) 9
3

Answer Key
1. (11) 5. (109)
2. (d) 6. (b)
3. (9) 7. (a)
4. (13) 8. (c)
4

Hints and Solutions

1. (11) 3. (9)
func(6):
func(5):
if(n>0){ //6>0 is TRUE
static int b=1; //static b is initialized to 1.
1. return func(n/2)+func(n/4)+1;//return
b=b+a; //b=1+5=6 func(3)+func(1)+1; return 5+3+1=9;
if((b%a)%2!=0) //(6%5)%2!=0 is TRUE }
Line 1: return a+func(b+a);// return 5+func(6+5); func(3):
func(11) is called. Returns 5+6 i.e 11.
if(n>0){ //3>0 is TRUE
Line 2: return b-a;
1. return func(n/2)+func(n/4)+1;//return
func(11): func(1)+func(0)+1; return 3+1+1=5;
}
static int b=1; //static b is initialized to 1.
b=b+a; //b=6+11=17 func(1):

if((b%a)%2!=0) //(17%11)%2!=0 is FALSE if(n>0){ //3>0 is TRUE


1. return func(n/2)+func(n/4)+1;//return
Line 1: return a+func(b+a);
func(0)+func(0)+1;//return 3;
Line 2: return b-a; // return (17-11) i.e 6 to Line 1 }
of func(5);
func(0):
return 1;
2. (d)
func(2): 4. (13)
2>0 True func(24):
func(1) is called. if(n>0){ //24>0 is TRUE
return 3*func(n/4)+1; //func(6) is called.
printf("%d\t", n); // 2 is printed.
Returns 3*4+1; Returns 13.
printf("%d\t", n-1); //1 is printed. }
func(1): func(6):
if(n>0){ //6>0 is TRUE
1>0 True
return 3*func(n/4)+1; //func(1) is called.
func(0) is called. Returns 3*1+1; returns 4;
printf("%d\t", n); // 1 is printed. }
printf("%d\t", n-1); //0 is printed. func(1):
if(n>0){ //1>0 is TRUE
func(0):
return 3*func(n/4)+1; //func(0) is called.
0>0 is FALSE Returns 1;
printf("%d\t", n-1); //-1 is printed. }
Output: -1 1 0 2 1 func(0):
return 0;
5

5. (109) func(1,8):
func(8): 1%2 is 1, so else if part is executed.
static int k=0; return func(0, 16)+8; //fun(0,16) returns 0
if(n>0){//8>0 is TRUE //Returns 8 to func(3, 4)
k++; //static k is incremented to 1.
return 2*func(n/2)+k;//func(4) is called. 7. (a)
Returns (2*53+3) i.e 109
func(7):
}
static int i=0;
func(4): if(n/2){//7/2= 3 is TRUE
static int k=0; i--;//static i is decremented to -1
if(n>0){//4>0 is TRUE return func(n/2)+i; //func(3) is called. func(7)
k++; //static k is incremented to 2. returns -4-2 i.e -6
return 2*func(n/2)+k;//func(2) is called. }else return i;
Returns (2*25+3) i.e 53
} func(3):
static int i=0;
func(2): if(n/2){//3/2= 1 is TRUE
static int k=0;
i--;//static i is decremented to -2
if(n>0){//2>0 is TRUE
return func(n/2)+i; //func(1) is called. func(1)
k++; //static k is incremented to 3. returns -2. func(3) returns -2-2 i.e -4
return 2*func(n/2)+k;//func(1) is called. }else return i;
Returns (2*11+3) i.e 25
}
8. (c)
func(1): For i=0 in main():
static int k=0; display():
if(n>0){//1>0 is TRUE static int i;//i=0
k++; //static k is incremented to 4. if(i<=printf("GATE24")){//i<=6; printf() executed
return 2*func(n/2)+k;//func(0) is called. i=i+2;//i=2
Returns(2*4+3) i.e 11
display();
}
}
f(0) returns (0+4). Static k is decremented to 3.
display():
6. (b) static int i;
func(14, 1): if(i<=printf("GATE24")){//2<=6; printf() executed
14%2 is 0, so else part is executed. i=i+2;//i=4
return func(7, 2)-1;// //Returns 14-1 i.e 13.
display();
func(7,2): }
7%2 is 1, so else if part is executed.
display():
return func(3, 4)+2; //Returns 12+2 i.e 14 to
func(14, 1) static int i;
if(i<=printf("GATE24")){//4<=6; printf() executed
func(3,4): i=i+2;//i=6
3%2 is 1, so else if part is executed. display();
return func(1, 8)+4; //Returns 8+4 i.e 12 to func(7, 2)
}
6

display(): For i=1 in main:


static int i; display():
if(i<=printf("GATE24")){//6<=6; printf() executed static int i;
i=i+2;//i=8 if(i<=printf("GATE24")){//8<=6 is FALSE but
printf() executed
display();
i=i+2;
}
display();
display(): }
static int i;
For i=2 in main():
if(i<=printf("GATE24")){//8<=6 is FALSE but
display():
printf() executed
static int i;
i=i+2;
if(i<=printf("GATE24")){//8<=6 is FALSE but
display(); printf() executed
} i=i+2;
display();
}
Total number of times printf() executed is 7.

For more questions, kindly visit the library section: Link for app: https://physicswallah.live/tabs/tabs/library-tab
For more questions, kindly visit the library section: Link for web: https://links.physicswallah.live/vyJw
Any issue with DPP, please report by clicking here- https://forms.gle/t2SzQVvQcs638c4r5

PW Mobile APP: https://play.google.com/store/apps/details?id=xyz.penpencil.physicswala


For PW Website: https://www.physicswallah.live/contact-us

You might also like