1
Branch : CSE & IT Batch : Hinglish
WEEKLY TEST – 03
Subject : Programming in C
Topic : Functions and Storage Classes
Maximum Marks 20
Q.1 to 6 Carry ONE Mark Each
[MCQ] [MSQ]
1. Consider the following program: 3. Which of the following statements is/are CORRECT?
#include<stdio.h> (a) A static variable has internal linkage.
int func(int i){ (b) Default value of register variable is garbage.
i-=3; (c) Default value of global variable is garbage.
return i; (d) An extern variable can be declared multiple times
} in a program.
void main(){
[MCQ]
int i=printf("Parakram 2024");
4. Consider the following two statements:
i=func(i=func(i=func(--i)));
P: int a=2;
printf("%d", i); static int b=a;
} Q: static int j;
The output is- static int j=1;
Which of the following statements is/are
(a) Parakram 20243
INCORRECT?
(b) Parakram 20246 (a) Neither P nor Q
(c) Parakram 20249 (b) P only
(d) Parakram 2024 (c) Q only
(d) Both P and Q.
[MCQ]
2. Consider the following program: [MCQ]
5. #include<stdio.h>
#include<stdio.h>
void arc(int n){
int a=5; if (n<=2) return;
extern int a; else{
void main(){ arc(n-2);
for(;--a>0;a--) printf("%d\t", n-1);
}
printf("GATE Wallah");
}
} int main(){
The output is: arc(9);
(a) “GATE Wallah” is printed 4 times. return 0;
(b) “GATE Wallah” is printed 2 times. }
The output printed is-
(c) “GATE Wallah” is printed 5 times.
(a) 2 4 6 8 (b) 4 6 8 10
(d) Compilation error. (c) 8 6 4 2 (d) 7 9 11 13
2
[MCQ] int p=7; {p--;}
6. #include<stdio.h> printf("%d\t", p--);
int main(){ }
int p=3; printf("%d", --p);
{ return 0;
int p=6; }
printf("%d\t", --p); The output printed is-
} (a) 1 6 2 5 (b) 5 10 6 2
{ (c) 5 2 6 1 (d) 5 6 1 10
printf("%d\t",--p);
Q.7 to 13 Carry TWO Mark Each
[NAT] The value returned by func(1) is-
7. int func(int a){ (a) 7 (b) 8
static int i=3; (c) 10 (d) 12
if(a<4) return a+i++;
a=a-i--; [NAT]
return func(a)+i; 10. #include<stdio.h>
} int func(int n){
What is the value returned by func(9)? ____________
int i, j=1;
if(n<1) return j;
[NAT]
for(i=2;i<n;i*=2)
8. int func(int a, int b){
static int p, q=1; j+=func(i)+func(n-i);
if(a>=b){ return j;
a=a-++p; }
b=b+--q; The value returned by func(5) is ______
return func(a,b)+p;
}else return p-q; [NAT]
} 11. Consider the following program:
The value returned by func(4, 3) is _______________. #include<stdio.h>
void func(int a){
[MCQ]
static int i=7;
9. #include<stdio.h>
if(a<=1) return;
int func(int a){
printf("%d\t",i--);
static int x=2;
a=a-i--;
if(a>7) return a;
a=a+x; printf("%d\t",a);
x++; func(a-2);
return func(a); printf("%d\t",i++);
} }
3
int main(){ Which of the following statement(s) is/are CORRECT?
func(10); (a) The function returns 0 for all values of k.
return 0; (b) The function doesn’t print “Pankaj Sharma” for all
} values of k.
(c) The functions prints “Pankaj Sharma” infinitely or
The sum of the values printed is____________
gives stack overflow error when k=10.
(d) The function returns 0 if k=10;
[MSQ]
12. int func(int k){
[MCQ]
static int i=10;
13. int func(int a, int b){
int j;
if(a>0)
if(i==k){
return a%b + func(a/b, b/4);
printf("Pankaj Sharma");
else return 0;
j=func(i);
}
return 0;
The value returned by func(347, 32) is-
}
(a) 12 (b) 30
return 0;
(c) 18 (d) 10
}
4
Answer Key
1. (a) 8. (7)
2. (b) 9. (c)
3. (a, b, d) 10. (9)
4. (d) 11. (21)
5. (a) 12. (b, c)
6. (c) 13. (b)
7. (6)
5
Hints and Solutions
1. (a) 5. (a)
void main(){ arc(9) n 9
int i=printf("Parakram 2024");//i=13
1. if (n < = 2) → false
i=func(i=func(i=func(--i))); 2. arc(9 – 2);
//func(--i) i.e func(12) returns 9. 3. printf(“8”);
//func(i=9) i.e func(9) returns 6. arc(7) n 7
//func(i=6) i.e func(6) returns 3. 1. if (n < = 2) → false
printf("%d", i);//3 2. arc(5)
Output:
} 3. printf(“6”);
arc(5) n 5 2 4 6 8
Output: Parakram 20243
1. if (n < = 2) → false
2. (b) 2. arc(3)
#include<stdio.h> 3. printf(“4”);
int a=5; arc(3) n 3
extern int a;//It will not give any compilation error. 1. if (n < = 2) → false
void main(){ 2. arc(1)
for(;--a>0;a--)//the loop executes two times. 3. printf(“2”);
printf("GATE Wallah"); arc(1) n 1
} 1. return
“GATE Wallah” is printed 2 times.
6. (c)
3. (a, b, d)
(a) CORRECT. A static variable has internal linkage.
(b) CORRECT. Default value of register variable is
garbage.
(c) INCORRECT. Default value of global variable is
zero.
(d) CORRECT. An extern variable can be declared
multiple times in a program.
4. (d)
P is not allowed. We can initialize a static variable with
a constant only.
Q is not allowed if the static variable j has local scope.
Output is: 5 2 6 1
6
7. (6) func(3, 3)://a=3, b=3
int func(int a){ Line1: 3>=3 → TRUE
static int i=3; Line2: a=a-++p; //a=3-2=1;
if(a<4) return a+i++;//line 1 Line3: b=b+--q;//b=3-1=2;.
a=a-i--;//line 2 Line4: return func(1,2)+2; // return 3+2 i.e 5
return func(a)+i;//line 3
func(1, 2):
}
Line1: 1>=2 → FALSE
func(9): Line5: return p-q; // return 2+1 i.e return 3.
Line1: 9<4 → FALSE
Line2: a=a-i--; a=9-3=6; static i is decremented to 2. 9. (c)
Line3: Call func(6); func(1):
return (5+1);//return 6 static int x=2;
func(6): if(a>7) return a;//1>7 is FALSE
Line1: 6<4 → FALSE a=a+x; //a=1+2=3
Line2: a=a-i--; a=6-2=4; static i is decremented to 1. x++;//static x is incremented to 3.
Line3: Call func(4); return func(a);// func(3) is called.
return (4+1);//return 5 to func(9) func(3):
func(4): if(a>7) return a;//3>7 is FALSE
Line1: 4<4 → FALSE a=a+x; //a=3+3=6
Line2: a=a-i--; a=4-1=3; static i is decremented to 0. x++;//static x is incremented to 4.
Line3: Call func(3); return func(a);// func(6) is called.
return (3+1);//return 4 to func(6) func(6):
func(3): if(a>7) return a;//6>7 is FALSE
Line1: 3<4 → TRUE; return a+i++; a=a+x; //a=6+4=10
//return (3+0) to func(4) then static i is x++;//static x is incremented to 5.
incremented to 1. return func(a);// func(10) is called.
func(10):
8. (7) if(a>7) return a;//10>7 is TRUE, so, 10 is returned.
int func(int a, int b){ Output: 10
static int p, q=1;
if(a>=b){ //Line 1 10. (9)
a=a-++p;//Line 2 func(5):
b=b+--q;//Line 3 n=5; j=1;
return func(a,b)+p;//Line 4 if(n<1) return j; //5<1 is FALSE
}else return p-q;//Line 5 for i=2:
} j+=func(i)+func(n-i);//j=j+func(2)+func(3);
func(2) returns 1, func(3) returns 3.So, j=1+1+3=5
func(4, 3)://a=4, b=3
for i=4:
Line1: 4>=3 → TRUE
j+=func(i)+func(n-i);//j=j+func(4)+func(1);
Line2: a=a-++p; //a=4-1=3;
func(1) returns 1, func(4) returns 3.So, j=5+3+1=9
Line3: b=b+--q;//b=3+0=3;
return j; //return 9;
Line4: return func(3, 3)+2; // return 5+2 i.e 7
7
11. (21) 12. (b, c)
func(8): When k!=10, then the functions returns 0. So, (a, d) are
if(a<=1) return; //8<=1: FALSE INCORRECT.
1. printf("%d\t",i--);//7 is printed, static i is The function prints “Pankaj Sharma” for k=10. So, b is
decremented to 6. correct.
a=a-i--;//a=10-6=4; static i is decremented to 5. The function prints “Pankaj Sharma” for k=10 infinite
2. printf("%d\t",a);//4 is printed. times or until runtime stack overflows. So, (c) is
correct.
func(a-2);//func(2) is called.
6. printf("%d\t",i++);//4 is printed, static i is
incremented to 5. 13. (b)
func(347, 32): //a=347, b=32
func(2):
if(a>0) //347>0 → TRUE
if(a<=1) return; //2<=1: FALSE
return a%b + func(a/b, b/4);//func(10, 8) is called. So,
3. printf("%d\t",i--);//5 is printed, static i is
27+3 i.e 30 is returned
decremented to 4.
a=a-i--;//a=2-4=-2; static i is decremented to 3. func(10, 8): //a=10, b=8
4. printf("%d\t",a);//-2 is printed. if(a>0) //10>0 → TRUE
func(a-2);//func(0) is called. It simply returns. return a%b + func(a/b, b/4);//func(1, 2) is called. So,
5. printf("%d\t",i++); //3 is printed, static i is 2+1 is returned
incremented to 4. func(1, 2): //a=1, b=2
Output: 7 4 5 -2 3 4 if(a>0) //1>0 → TRUE
Sum: 21 return a%b + func(a/b, b/4);//func(0, 0) is called. So,
1+0 is returned.
func(0, 0) returns 0;
For more questions, kindly visit the library section: Link for web: https://smart.link/sdfez8ejd80if
PW Mobile APP: https://smart.link/7wwosivoicgd4