Functions & Storage Classes _ DPP 04 __ Parakram GATE 2024 Computer Science Weekend (English)
Functions & Storage Classes _ DPP 04 __ Parakram GATE 2024 Computer Science Weekend (English)
[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
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):
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
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