1
Branch : CSE & IT Batch:Hinglish
WEEKLY TEST – 04
Subject : Programming in C
Topic : Recursion and arrays
Maximum Marks 20
Q.1 to 6 Carry ONE Mark Each
[MCQ] (a) Compilation error
1. Consider the following program: (b) 313
#include <stdio.h> (c) 212
int main() (d) 311
{
int b[5]={1, 2, 3, 4, 5}; [MCQ]
int *ptr[5]={b+3, b+2, b, b+1, b+4}; 4. Consider the following program:
int **q=ptr; #include <stdio.h>
*++*q; int main()
printf("%d\t%d\t%d", q-ptr, *q-b, **q); {
return 0; int b[3]={6, 12, 20};
} int *ptr[3]={b, b+2, b+1};
The output of the program is- printf("%d", *ptr[*ptr[1]-*ptr[2]-8]);
(a) 0 1 2 (b) 0 4 5 return 0;
(c) 1 2 3 (d) Compilation error. }
The output is:
(a) Segmentation Fault
[NAT] (b) Compilation Error
2. Consider the following function: (c) 6
void f(int n) (d) 12
{
if(n<2) {printf("%d", n-3); return;} [NAT]
printf("%d", n-2);
f(n-1); 5. Consider the following function:
} int f(int x)
The sum of all the values printed when f(7) is called is {
. if(x<=1) return x;
if(x%3) return f(x/3)+x;
return f(x/2)+x;
[MCQ] }
3. Consider the following program: The value returned by f(24) is .
#include <stdio.h>
int main() [MCQ]
{
int b[5]={1, 2, 3, 4, 5}; 6. Consider the following program:
int *ptr[5]={b+3, b+2, b, b+1, b+4}; #include <stdio.h>
int **q=&ptr[3]; int main()
--**q; {
printf("%d\t%d\t%d", q-ptr, *q-b, **q); int a[][3][2]={0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
return 0; printf("%u\t", a);
} printf("%u\t", **a+1);
The output is- printf("%u\t", *(*(*a+2))+1);
2
printf("%u\t", a+1); The output is-
return 0; (a) 1000 1002 5 1012
} (b) 1000 1004 6 1012
Assume array index starts from 1000 and integer size (c) 1000 1008 7 1024
is 2 bytes. (d) 1000 1012 7 1024
Q.7 to 13 Carry TWO Mark Each
[NAT] {
7. Consider the following program: int a[]={7, 2, 5, 10, 13};
#include <stdio.h> int count=1, i=4;
void func(int *q) while(i){
{ count=count+(*(a+i)-i);
int *p; i--;
p=q++; }
*q=*q-*p; printf("%d", count);
*p=*p-1; return 0;
} }
int main() The output is .
{
int i; [MCQ]
int a[][3]={0, 1, 2, 3, 4, 5, 6, 7, 8}; 10. Consider the following program:
for(i=0;i<2;i++) #include <stdio.h>
func(a[i]); void doSomething(int **p)
return 0; {
} **p++;
The sum of elements of the first two rows of the array printf("%d\t", **p);
is . }
int main()
[NAT] {
8. Consider the following program: int b[5]={6, 12, 20, 13, 7};
#include<stdio.h> int *ptr[5]={b+4, b+3, b+2, b+1, b};
int f(int *arr, int n) int i;
{ for(i=0;i<4; i++)
int c; doSomething(ptr+i);
if(n<=1) return *arr-n; return 0;
else if(*arr%5==0) }
return *(arr+1) - f(arr+1, n-1); The output is-
else (a) 13 12 6 20 (b) 13 20 12 6
return *arr + f(arr+1, n-1); (c) 7 20 12 13 (d) 20 12 13 7
}
int main(){ [MCQ]
int a[]={1, 2, 5, 10};
11. Consider the following function:
printf("%d",f(a,sizeof(a)/sizeof(a[0])));
int func(int n)
return 0;
{
}
static int i=1, j;
The output is .
if(n<0) return 0;
j+=func(n-i);
[NAT] i+=2;
9. Consider the following program: return j+n;
#include<stdio.h> }
int main() The above function computes-
3
(a) x2+2x+1 (b) 2x+1 printf("%u\t", *p[1]);
(c) 2x-1 (d) 2x printf("%u\t", *(*(p+2)+1));
printf("%u\t",**p+3);
[NAT] printf("%u",*(*p+3));
12. Consider the following function: }
int func(int a) int main()
{ {
static int x; int a[]={1, 2, 13, 7};
if(a>5) return a-x--; int b[]={3, 9, 6};
a=a+x++; int c[]={4, 5, 7};
return func(a)+x; int *arr[]={a, b, c};
} doSomething(arr);
The value returned by func(4) is . return 0;
}
The output is:
[MCQ] (a) 9 5 3 4 7 (b) 3 5 9 7 4
13. Consider the following program: (c) 9 3 5 7 7 (d) 9 3 5 4 7
#include <stdio.h>
void doSomething(int **p)
{
printf("%u\t", p[1][1]);
4
Answer Key
1. (b) 8. (4)
2. (13) 9. (21)
3. (d) 10. (b)
4. (c) 11. (c)
5. (46) 12. (10)
6. (a) 13. (d)
7. (10)
5
Hints and Solutions
1. (b)
1000 1002 1004 1006 1008 =*ptr[*ptr[1]-*ptr[2]-8]
1 2 3 4 5 =*ptr[*1004-*1002-8]
=*ptr[20-12-8]
2000 2004 2008 2012 2016
=*ptr[0]
10061008 1004 1000 1002 1008
=*1000
3000 =6
2000
5. (46)
*++*q;//*++*2000 = *++1006 =*1008 24%3 is 0. Return f(24/2)+24. Return 22+24 i.e 46
printf("%d\t%d\t%d", q-ptr, *q-b, **q);
q-ptr= (2000-2000)/4 = 0/4 =0 12%3 is 0. Return f(12/2)+12. Return 10+12 i.e 22
*q-b = *2000-1000= (1008-1000)/2=4 6%3 is 0. Return f(6/2)+6. Return 4+6 i.e 10
**q=**2000=*1008=5 3%3 is 0. Return f(3/2)+3. Return 1+3 i.e 4
1%3 is 1. Return f(1/3)+1. Return 1
2. (13)
The printed values are:
5 4 3 2 1 0 -2 6. (a)
Sum: 13 printf("%u\t", a);//It points to the 0th 3D array.
printf("%u\t", **a+1); //It points to the 1st element of
3. (d)
1000 1002 1004 1006 1008 the 0th row of the 0th 3D array. So, 1002 is printed.
1 21 3 4 5 printf("%u\t", *(*(*a+2))+1); //It is the 1st element of
the 2nd row of the 0th 3D array. So, 5 is printed.
2000 2004 2008 2012 2016
1006 1004 1000 1002 1008 printf("%u\t", a+1);//It points to the 1st 3D array. So,
1012 is printed.
3000
2012
7. (10)
--**q;//--**2012 = --*1002 =*1008 1000 1002 1004 1006
printf("%d\t%d\t%d", q-ptr, *q-b, **q); 1 2 5 10
q-ptr= (2012-2000)/4 = 12/4 =3
*q-b = *2012-1000= (1002-1000)/2=1 The given function func(*q)is called for the 0th and 1st
**q=**2012=*1002=1 row of the 2D array.
It decrements the 0th element of a row by 1. It subtracts
4. (c) the value of the 0th element from the 1st element in any
1000 1002 1004 row. So, the two rows are-
6 12 20 -1 1 2
215
2000 2004 2008 Sum: 10
1000 1004 1002
6
8. (4) count=count+(*(a+3)-3);//(*(1006)-3)=7;
sizeof(a)/sizeof(a[0]) gives the size of the array. Size of count=10+7=17
the array is 4. i--;//i=2
f(0,4): }
Line1: arr=100, n=4; while(2)
Line2: 4<=1->FALSE; {
Line3:*arr i.e 1%5!=0, so else part is executed. count=count+(*(a+2)-2);//(*(1004)-2)=3;
f(*arr+1, n-1)=f(102,3) =3 count=17+3=20
*arr+f(*arr+1, n-1)=1+3=4 i--;//i=1
return 4; //return 4 to main }
f(102,3): while(1)
Line1: arr=102, n=3; {
Line2: 3<=1->FALSE; count=count+(*(a+1)-1);//(*(1002)-1)=1;
Line3:*arr i.e 2%5!=0, so else part is executed. count=20+1=21
f(*arr+1, n-1)=f(104,2) =1 i--;//i=1
*arr+c=2+1 i.e 3 }
return 3; //return 3 to Line3 of f(100,4) printf("%d", count);//21 is printed.
f(104,2):
Line1: arr=104, n=2; 10. (b)
Line2: 2<=1->FALSE; 1000 1002 1004 1006 1008
6 12 20 13 7
Line3:*arr i.e 5%5==0, so else if part is executed.
f(*arr+1, n-1)=f(106,1)=9 2000 2004 2008 2012 2016
*(arr+1)-c=(10-9) i.e 1 1008 1006 1004 1002 1000
return 1; //return 1 to Line3 of f(102,3) For i=0:
f(106,1): doSomething(2000+0):
Line1: arr=106, n=1; p=2000
**p++;//p=2004
Line2: 1<=1->TRUE; //Return *arr-n i.e. (10-1) i.e 9 to
printf("%d\t", **p);//**2004= 13 is printed.
Line3 of f(104,2) For i=1:
Output: 4 doSomething(2000+1):
p=2004
9. (21) **p++;//p=2008
1000 1002 1004 1006 1008 printf("%d\t", **p);//**2008= 20 is printed.
7 2 5 10 13 For i=2:
doSomething(2000+2):
count 1 p=2008
while(4) **p++;//p=2012
{ printf("%d\t", **p);//**2012= 12 is printed.
count=count+(*(a+4)-4);//(*(1008)-4)=9; For i=3:
count=1+9=10 doSomething(2000+3):
i--;//i=3 p=2012
**p++;//p=2016
}
printf("%d\t", **p);//**2016= 6 is printed.
while(3) Output: 13 20 12 6
{
7
11. (c) 1000 2000 3000
The above function computes 2x-1. doSomething(4000):
p 4000
12. (10) printf("%u\t", p[1][1]);
func(4): //*(*(p+1)+1)=*(*(4000+1)+1)=*(*4004+1)=*(2000+
a=a+x++; //a=4+0=4. Static x is incremented to 1. 1)=*2002=9
printf("%u\t",
return func(4)+x; return 10 *p[1]);//**(p+1)=**(4000+1)=**4004=*2000=3
func(4): printf("%u\t", *(*(p+2)+1));= *(*(4000+2)+1)=
a=a+x++; //a=4+1=5. Static x is incremented to 2. *(*4008+1)=*(3000+1)=*3002=5
printf("%u\t",**p+3);=**4000+3=*1000+3=1+3=4
return func(5)+x; return 6+2; return 8 printf("%u",*(*p+3));=*(*4000+3)=*(1000+3)
func(5): =*1006=7
a=a+x++; //a=5+2=7. Static x is incremented to 3. Output: 9 3 5 4 7
return func(7)+x; return 6
func(7):
return a-x--;
return 7-3=4; x is decremented to 2.
13. (d)
1000 1002 1004 1006
1 2 13 7
2000 2002 2004
3 9 6
3000 3002 3004
4 5 7
4000 4004 4008
For more questions, kindly visit the library section: Link for web: https://smart.link/sdfez8ejd80if
PW Mobile APP: https://smart.link/7wwosivoicgd4