Array and Pointer C Programming Mock Test – 2

Question 1
What is the output of the following program?
Consider the starting address of int array a is 1000 and char array ch is 2000.
Assume size of int data type is 4 Byte and size of char data type is 1 Byte.
#include < stdio.h > 
int main()
{
     int a[5],*p,*q;
     char ch[5],*r,*s;
     p=&a[4];
     q=&a[1];
     r=&ch[4];
     s=&ch[1];
     printf("%ld %ld",p-q,r-s);
     return 0;
}
A
12 3
B
3 3
C
12 12
D
1 1
   C Programming
Question 1 Explanation: 


Now pointer arithmetic p-q and r-c can be computed as follow:

p-q=(address stored in p - address store in q)/ size of (int)
NOTE: Here, we have divided subtraction of two pointer by sizeof (int) because p and q are pointer of type int.
(1016-1004)/4=3

r-s=(address stored in r - address store in s)/ size of (char)
NOTE: Here, we have divided subtraction of two pointer by sizeof (char) because r and s are pointer of type char.
(2004-2001)/1=3

OUTPUT : 3 3

Click to Join Our Telegram Group for Latest Update of MOCK TEST

Click Here to Practice ALL Previous MOCK TEST FREE

(For further reading in depth Click Here
Question 2
What is the output of the following program?
Consider the starting address of int array a is 1000 and char array ch is 2000.
Assume size of int data type is 4 Byte and size of char data type is 1 Byte.
#include < stdio.h > 
int main()
{
     int a[5],*p,*q;
     char ch[5],*r,*s;
     p=&a[4];
     q=&a[1];
     r=&ch[4];
     s=&ch[1];
     printf("%ld %ld",(char *)p-(char *)q,(int *)r-(int *)s);
     return 0;
}
A
3 3
B
12 3
C
12 0
D
3 12
   C Programming
Question 2 Explanation: 


Now pointer arithmetic p-q and r-c can be computed as follow:

(char *)p-(char *)q=(address stored in p - address store in q)/ size of (char)
NOTE: Here, we have divided subtraction of two pointer by sizeof (char) because p and q are pointers converted to type char in printf statement.
(1016-1004)/1=12

(int *)r-(int *)s=(address stored in r - address store in s)/ size of (int)
NOTE: Here, we have divided subtraction of two pointer by sizeof (char) because r and s are pointers converted to type int in printf statement.
(2004-2001)/4=0

OUTPUT : 12 0

Click to Join Our Telegram Group for Latest Update of MOCK TEST

Click Here to Practice ALL Previous MOCK TEST FREE

(For further reading in depth Click Here
Question 3
What is the output of the following program?
Consider the starting address of int array a is 1000.
Assume size of int data type is 4 Byte and address stored in 8 byte.
#include < stdio.h > 
int main()
{
     int a[2][3]={2,5,8,9,3,1};
     printf("%d",a[1]-b[0]);
     return 0;
}
A
7
B
4
C
3
D
1
   C Programming
Question 3 Explanation: 


When we declare 2-D array, by default it will store in row major order in memory.
In pointer subtraction calculation, important to understand is data type of pointer while computing the subtraction.
when we write

a[0][0]
it indicates the int value stored at index [0][0] of array a.

a[0]
it indicates the pointer to int. it points to the address of 0th element of the row-0. so a[0]=1000.

a
it indicates the pointer to array of 3 (as array declared with 3 col.). It point to whole row 0 of 2-D array. So a=1000.

In given question pointer a[1]-a[0] need to computed by compiler. It can be computed as follow:

a[1] is pointer of type int pointing to the starting address of row-1 in given -D array. Hence a[1]=1012 as shown in above figure.
a[0] is pointer of type int pointing to the starting address of row-0 in given -D array. Hence a[1]=1000 as shown in above figure.
Now, a[1]-a[0] is computed by compiler as
(1012-1000)/sizeof(int) = 3.

OUTPUT : 3

Click to Join Our Telegram Group for Latest Update of MOCK TEST

Click Here to Practice ALL Previous MOCK TEST FREE

(For further reading in depth Click Here
Question 4
What is the output of the following program?
Consider the starting address of int array a is 1000.
Assume size of int data type is 4 Byte and address stored in 8 byte.
#include < stdio.h > 
int main()
{
     int a[2][3]{2,5,8,9,3,1};
     printf("%d %d %d %d",a,a+1,a[0],a[0]+1);
     return 0;
}
A
1000 1004 2 3
B
1000 1012 2 9
C
1000 1012 1000 1008
D
1000 1012 1000 1004
   C Programming
Question 4 Explanation: 


When we declare 2-D array, by default it will store in row major order in memory.
a[0] is pointer of type int pointing to the address 0th element of the row-0. Hence, a[0]=1000.
Now, a[0]+1 is pointer arithmetic computed by compiler as a[0]+1 *sizeof(int)=1000+1*4=1004

a is also pointer of type (int * [3]) or pointing to array of 3 int. So a is pointing to starting address of 0th row of 2-D array. Hence, a=1000.
a+1 is pointing to starting address of 1st row of 2-D array. Hence, a=1012.
OR
a+1 is pointer arithmetic computed by compiler as a+1 *sizeof(int *[3])=1000+3*4=1012.

OUTPUT : 1000 1012 1000 1004

Click to Join Our Telegram Group for Latest Update of MOCK TEST

Click Here to Practice ALL Previous MOCK TEST FREE

(For further reading in depth Click Here
Question 5
What is the output of the following program?
Consider the starting address of int array a is 1000.
Assume size of int data type is 4 Byte and address stored in 8 byte.
#include < stdio.h > 
int main()
{
     int a[2][3][2]={1,2,3,4,5,6,7,8,9,10,11,12};
     printf("%d %d",a[1]-a[0],a[1][0]-a[0][0]);
     return 0;
}
A
6 6
B
3 3
C
3 6
D
6 3
   C Programming
Question 5 Explanation: 


a[0][0] is pointer to int and pointing to the element a[0][0][0] as shown in above figure. Hence, a[0][0]=1000.

a[1][0] is pointer to int and pointing to the element a[1][0][0] as shown in above figure. Hence, a[1][0]=1024.

So, a[1][0]-a[0][0] computed by compiler as (1024-1000)/sizeof(int)=24/4=6.

a[0] is pointer to array of 2 int (row of 2 int) and pointing to the 0th row of the 0th 2-D array as shown in above figure. Hence, a[0][0]=1000.

a[1] is pointer to array of 2 int (row of 2 int) and pointing to the 0th row of the 1st 2-D array as shown in above figure. Hence, a[1][0]=1024.

So, a[1]-a[0] computed by coompiler as (1024-1000)/sizeof(row of two int)=24/8=3

OUTPUT : 3 6

Click to Join Our Telegram Group for Latest Update of MOCK TEST

Click Here to Practice ALL Previous MOCK TEST FREE

(For further reading in depth Click Here
Question 6
What is the output of the following program?
Consider the starting address of int array a is 1000.
Assume size of int data type is 4 Byte and address stored in 8 byte.
#include < stdio.h > 
int main()
{
     int a[2][3][2]={1,2,3,4,5,6,7,8,9,10,11,12};
     printf("%d %d %d %d %d %d",a,a+1,a[0],a[0]+1,a[0][0],a[0][0]+1);
     return 0;
}
A
1 2 1 2 1 2
B
1000 1004 1000 1004 1000 1004
C
1000 1004 1000 1004 1 2
D
1000 1024 1000 1008 1000 1004
   C Programming
Question 6 Explanation: 


When we declare 3-D array, by default it will store in row major order in memory.
a[0][0] is pointer of type int pointing to the address 0th element of the row-0 of 0th 2-D array as shown in above figure. Hence, a[0][0]=1000.
Now, a[0][0]+1 is pointer arithmetic computed by compiler as a[0]+1 *sizeof(int)=1000+1*4=1004

a[0] is also pointer of type (int * [2]) or pointing to row as array of 2 int. So a[0] is pointing to starting address of 0th row of 0th 2-D array as shown in above figure. Hence, a[0]=1000.
a[0]+1 is pointing to starting address of 1st row of 0th 2-D array as shown in above figure.
OR
a[0]+1 is pointer arithmetic computed by compiler as a+1 *sizeof(int *[2])=1000+2*4=1008.

a is pointer of type (int * [3][2]) or pointing to a 2-D array [3][2]. So a is pointing to the starting address of 0th 2-D arrar as shown in above figure. Hence, a=1000.
a+1 is pointing to starting address of 1st 2-D array as shown in above figure.
OR
a+1 is pointer arithmetic computed by compiler as a+1 *sizeof(int *[3][2])=1000+3*2*4=1024.

OUTPUT : 1000 1024 1000 1008 1000 1004

Click to Join Our Telegram Group for Latest Update of MOCK TEST

Click Here to Practice ALL Previous MOCK TEST FREE

(For further reading in depth Click Here
Question 7
What is the output of the following program?
Consider the starting address of int array a is 1000.
Assume size of int data type is 4 Byte and address stored in 8 byte.
#include < stdio.h > 
int main()
{
     int *a[5];
     printf("%d %d",a,a+1);
     return 0;
}
A
1000 1020
B
1000 1004
C
1000 1008
D
1000 1001
   C Programming
Question 7 Explanation: 


when we declare int *a[5];
a is pointer to array of 5 element each of type (int *) as shown in figure. Starting address of array a is given as 1000. Hence a=1000.
Pointer arithmetic with a+1 is computed as a+1*sizeof(int *)=1000+1*8=1008.

OUTPUT : 1000 1008

Click to Join Our Telegram Group for Latest Update of MOCK TEST

Click Here to Practice ALL Previous MOCK TEST FREE

(For further reading in depth Click Here
Question 8
What is the output of the following program?
Consider the starting address of int array a is 1000.
Assume size of int data type is 4 Byte and address stored in 8 byte.
#include < stdio.h > 
int main()
{
     int *a[5];
     printf("%d %d",a[0],a[0]+1);
     return 0;
}
A
1000 1020
B
1000 1004
C
1000 1008
D
None of the above
   C Programming
Question 8 Explanation: 


when we declare int *a[5];
a is pointer to array of 5 element each of type (int *) as shown in figure. Starting address of array a is given as 1000. a[0] is (int *) pointing to the 0th element of some array as shown in figure.
Starting address of that array has no relation with starting address of array a. Hence, it can not be computed from starting address of array a.

Click to Join Our Telegram Group for Latest Update of MOCK TEST

Click Here to Practice ALL Previous MOCK TEST FREE

(For further reading in depth Click Here
Question 9
What is the output of the following program?
Consider the starting address of int array a is 1000.
Assume size of int data type is 4 Byte and address stored in 8 byte.
#include < stdio.h > 
int main()
{
     int (*a)[5];
     printf("%d %d",a,a+1);
     return 0;
}
A
1000 1020
B
1000 1004
C
1000 1008
D
None of the above
   C Programming
Question 9 Explanation: 


when we declare int (*a)[5], it indicate that a is pointer to an array of 5 int or row of 5 int. Hence, as given starting address of array as 1000.
a=1000.

a+1 is pointer arithmetic and computed as a+1*5*sizeof(int)=1000+1*5*4=1020.

OUTPUT : 1000 1020

Click to Join Our Telegram Group for Latest Update of MOCK TEST

Click Here to Practice ALL Previous MOCK TEST FREE

(For further reading in depth Click Here
Question 10
What is the output of the following program?
Consider the starting address of int array a is 1000.
Assume size of int data type is 4 Byte and address stored in 8 byte.
#include < stdio.h > 
int main()
{
     int (*a)[5];
     printf("%d %d",a[0],a[0]+1);
     return 0;
}
A
1000 1020
B
1000 1004
C
1000 1008
D
None of the above
   C Programming
Question 10 Explanation: 


when we declare int (*a)[5], it indicate that a is pointer to an array of 5 int or row of 5 int. a[0] can be written as *(a+0). It indicates the pointer to int, pointing to the 0th element of that row pointed by pointer a. Hence, a[0]=1000.

a[0]+1 is pointer arithmetic and computed as a[0]+1*sizeof(int)=1000+4=1004.

OUTPUT : 1000 1004

Click to Join Our Telegram Group for Latest Update of MOCK TEST

Click Here to Practice ALL Previous MOCK TEST FREE

(For further reading in depth Click Here
There are 10 questions to complete.

Leave a Comment