C Programming - Weekly Test 04 - Test Paper
C Programming - Weekly Test 04 - Test Paper
[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
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
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
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