1
Branch : CSE & IT Batch:Hinglish
WEEKLY TEST – 05
Subject : Programming in C
Topic : Arrays and Pointers
Maximum Marks 20
Q.1 to 6 Carry ONE Mark Each
[MCQ] (a) 11 (b) 12
1. Consider the following statements: (c) 14 (d) Compilation Error
P: int a[2]={5,10}; [NAT]
printf("%d", 1[a]);
4. Consider the following program:
Q: int 2[a]={5,10};
printf("%d", 1[a]); #include <stdio.h>
Which of the following statement(s) is/are
INCORRECT? int main()
(a) Both P and Q {
(b) P only int a[]={1,3,5,7,9};
(c) Q only int i, count=0;
(d) Neither P nor Q
int *b=a+4;
[MSQ] for(i=0;i<3;i++)
2. Which of the following declaration(s) is/are NOT count=count+(*b---i);
allowed?
(a) int b[][]={1,2,3,4}; return 0;
(b) int b[][2][2]={1};
(c) int b[]={1}; }
(d) int b[][3]; The final value of count is _______.
[MCQ]
[MCQ]
3. Consider the following program:
#include <stdio.h> 5. Consider the following program:
int main() #include <stdio.h>
{ int main()
int a[3][4]={1,2,3,4,5,6,7,8,9,10,11,12}; {
printf("%d",*(*(a+**a+1)+2)+3); int a[]={1, 2, 3, 4, 5};
return 0; int *ptr=a;
} ptr+=sizeof(2*a[0]);
The output is- printf("%d",*(ptr-2));
return 0;
2
} [MCQ]
(Assume, integer size is 4 bytes) 6. Consider the following codes:
The output is- P: int *p=NULL;
printf("%d", *p);
(a) 4 (b) 3 Q: int *p;
(c) Garbage value (d) Compilation Error *p=10;
printf("%d", *p);
Which of the following is CORRECT?
(a) Neither P nor Q is valid.
(b) Only P is valid.
(c) Only Q is valid.
(d) Both P and Q are valid.
Q.7 to 13 Carry TWO Mark Each
[MCQ] #include <stdlib.h>
7. Consider the following program: 2 Marks int main() {
void *p, *q;
#include <stdio.h> int k=97;
#include <stdlib.h> char b='C';
p=&k;
int main() {
q=&b;
int i; printf("%d", *(char*)p-*(char *)q);
return 0;
int *p=(int*)malloc(3*sizeof(int));
}
for(i=0;i<3;i++) The output is-
*(p+i)=3-i; (a) Garbage value
(b) Compilation error
int *q=p;
(c) 30
printf("%u\t",*++p); (d) No output
printf("%u\t", ++*p);
printf("%u\t", p-q);
return 0;
[MCQ]
}
9. Consider the following program:
The output is-
#include <stdio.h>
(a) 1 2 3 (b) 3 2 1
int func(int *p, int n){
(c) 2 2 1 (d) 2 3 1
int sum=*(p+4);
for(int i=1;i<n-2;i++){
[MCQ] sum=sum+*(p+i)+*p++;
8. Consider the following program: }
#include <stdio.h>
3
return sum; ++*p++;
} ++*++p;
int main() }
{ int main()
int a[]={7, 1, 3, 5, 2}; {
int (*ptr)(int *, int)=func; int a[]={5,4,3,2,1};
printf("%d",(*ptr)(a, 5)); func(a+2);
return 0; printf("%d",a[2]+a[3]+a[4]);
} return 0;
The output is- }
(a) 16 The output is ______.
(b) 23
(c) Garbage value [MCQ]
(d) Compilation Error 12. Consider the following program:
#include <stdio.h>
[MCQ]
10. Consider the following functions: int f(int (*ptr)[3], int n)
P: int * f1(){ {
int x=10; if(n<=1) return 0;
return &x;
} return f(ptr+1, n-1)+**ptr;
Q: int *f2(){
static int x=10; }
return &x;
int main()
}
R: int *f3(){ {
int x=10;
int *p=&x; int a[][3]={1,2,3,4,5,6,7,8,9};
return *p;
} printf("%d",f(a,3));
Which of the given functions is/are VALID? return 0;
(a) P (b) Q
(c) R (d) P and R }
The output is-
(a) 5 (b) 11
(c) Garbage value (d) Compilation Error
[NAT]
[NAT] 13. Consider the following program:
11. Consider the following program:
#include <stdio.h>
#include <stdio.h>
int main()
void func(int *p)
{
{
4
int a[2][2][3]={0,1,2,3,4,5,6,7,8,9,10,11}; return 0;
printf("%d", *(*(*a+1)+1)+1); }
printf("%d", a[1][1][1]); The sum of printed values is __________
printf("%d", ***(a+1));
printf("%d", **(*a+1));
printf("%d", *(**a+1));
5
Answer Key
1. (c) 8. (c)
2. (a, d) 9. (a)
3. (c) 10. (b)
4. (18) 11. (8)
5. (b) 12. (a)
6. (a) 13. (25)
7. (d)
6
Hints and Solutions
1. (c) count=9+(7-1)=15;//b is decremented to a+2; b now
P: int a[2]={5,10};//It is valid declaration. points to 5.
printf("%d", 1[a]);//1[a] is equivalent to a[1].
Hence, P is correct. For i=2:
count=count+(*b---i);//post decrement is evaluated
Q: int 2[a]={5,10};//It is invalid declaration. before *.
printf("%d", 1[a]);
Hence, Q is incorrect. count=15+(5-2)=18;//b is decremented to a+1; b now
points to 3.
2. (a, d)
Final value of count=18.
int b[][]={1,2,3,4};//Not allowed
int b[][2][2]={1};//allowed 5. (b)
ptr+=sizeof(2*a[0]);
int b[]={1};//allowed
a[0] is 1. So, ptr+=sizeof(2);
int b[][3];//Not allowed
Now 2 is an [Link], ptr+=4; ptr points to 5.
*(ptr-2)=3 is printed.
3. (c)
a denotes the base address of the 0th 1D array.
**a denotes the 0th element of the 0th 1D array. 6. (a)
*(*(a+**a+1)+2)+3=*(*(a+1+1)+2)+3=*(*(a+2)+2)+ P: INCORRECT. NULL pointer dereferencing is not
3 allowed.
a+2 points to the 2nd 1D array. *(a+2) points to the 0th
element of the 2nd 1D array. Q: INCORRECT. p is an uninitialized pointer.
(*(a+2)+2) points to the 2nd element of the 2nd 1D
array. 7. (d)
*(*(a+2)+2) is the 2nd element of the 2nd 1D array. So, 100 102 104
*(*(a+2)+2)=11 3 23 1
200
*(*(a+2)+2)+3=11+3=14. p 100 102
Output: 14 For i=0:
*p=3;
4. (18) For i=1:
b points to the 4th element of a i.e 9. *(p+1)=3-1=2;
For i=0: For i=2:
count=count+(*b---i);//post decrement is evaluated *(p+2)=3-2=1;
before *. q=100;
count=0+(9-0)=9;//b is decremented to a+3; b now printf("%u\t",*++p);//*102 i.e 2 is printed.
points to 7.
For i=1: printf("%u\t", ++*p);// ++*102 i.e 3 is printed
count=count+(*b---i);//post decrement is evaluated printf("%u\t", p-q); //(102-100)/2 i.e 1 is printed.
before *.
Output: 2 3 1
7
8. (c) ++*++p; p now points to a[4]. a[4] is incremented by
main() 1.a[4]=2.
p 100 q 200 k 97 b C
100 200 a[2]+a[3]+a[4]=4+2+2=8.
printf("%d", *(char*)p-*(char *)q);
// 97-67=30
12. (a)
9. (a) f(a, 3):
ptr is the pointer to the elements of a[0].
ptr is a pointer the function func().
f(a+1, 2) is called.
func(a, 5): f(a, 3) returns 4+**a=4+1=5 to main().
p stores the starting address or the address of the 0th
element of the array. p=a, n=5; f(a+1, 2):
For i=1: ptr is a pointer to the elements of a[1].
sum=sum+*(p+1)+*p++;//Post decrement operator f(a+2, 1) is called. It returns 0.
will be evaluated before *. f(a+1, 2) returns 0+**(a+1) i.e. 0+4=4 to f(a, 3).
sum=2+1+7;//p then points to 1.
sum=10; 13. (25)
For i=2: *(*(*a+1)+1) is the 1st element in the 1st 1D array of
sum=sum+*(p+2)+*p++;//Post decrement operator the 0th 2D array.
will be evaluated before *. *(*(*a+1)+1)+1=4+1=5
sum=10+5+1;//p then points to 3. a[1][1][1] is the 1st element in the 1st 1D array of the
sum=16; 1st 2D array i.e 10
Output: 16 ***(a+1) is the 0th element in the 0th 1D array of the
1st 2D array i.e 6
10. (b) **(*a+1) is the 0th element in the 1st 1D array of the
P and R returns the address of local variable. So, P 0th 2D array i.e 3
and R are invalid. *(**a+1) is the 1st element in the 0th 1D array of the
Q returns the address of static variable. It is allowed. 0th 2D array i.e 1
11. (8) Output: 5 10 6 3 1
func(a+2): Sum: 25
p stores the address of a[2].
++*p++;a[2] is incremented by 1.a[2]=4. p now
points to a[3].
For more questions, kindly visit the library section: Link for web: [Link]
PW Mobile APP: [Link]