1.
#include “stdio.h”
int main()
{
int arr[5];

// Assume that base address of arr is 2000 and size of integer
// is 32 bit
arr++;
printf(“%u”, arr);

return 0;
}
(a) 2002
(b) 2004
(c) 2020
(d) lvalue required

Answer
(d) Array name in C is implemented by a constant pointer. It is not possible to apply increment and decrement on constant types.

2.
#include “stdio.h”

int main()
{
int arr[5];
// Assume base address of arr is 2000 and size of integer is 32 bit
printf(“%u %u”, arr + 1, &arr + 1);

return 0;
}
(a) 2004 2020
(b) 2004 2004
(c) 2004 Garbage value
(d) The program fails to compile because Address-of operator cannot be used with array name

Answer
(a) Name of array in C gives the address(except in sizeof operator) of the first element. Adding 1 to this address gives the address plus the sizeof type the array has. Applying the Address-of operator before the array name gives the address of the whole array. Adding 1 to this address gives the address plus the sizeof whole array.

3.
int main()
{
int i;
int arr[5] = {1};
for (i = 0; i < 5; i++)
printf(“%d “, arr[i]);
return 0;
}

(a) 1 followed by four garbage values
(b) 1 0 0 0 0
(c) 1 1 1 1 1
(d) 0 0 0 0 0

Answer
(b) In C/C++, if we initialize an array with fewer members, all remaining members are automatically initialized as 0.
For example, the following statement initializes an array of size 1000 with values as 0.
int arr[1000] = {0};

4.
#include “stdio.h”

int main()
{
int a[] = {1, 2, 3, 4, 5, 6};
int *ptr = (int*)(&a+1);
printf(“%d “, *(ptr-1) );
return 0;
}

(a) 1
(b) 2
(c) 6
(d) Runtime Error

Answer
(c) &a is address of the whole array a[]. If we add 1 to &a, we get “base address of a[] + sizeof(a)”. And this value is type casted to int *. So ptr points the memory just after 6 is stored. ptr is type casted to “int *” and value of *(ptr-1) is printed. Since ptr points memory after 6, ptr – 1 points to 6.

5.
#include “stdio.h”
#define SIZE(arr) sizeof(arr) / sizeof(*arr);
void fun(int* arr, int n)
{
int i;
*arr += *(arr + n – 1) += 10;
}

void printArr(int* arr, int n)
{
int i;
for(i = 0; i < n; ++i)
printf(“%d “, arr[i]);
}

int main()
{
int arr[] = {10, 20, 30};
int size = SIZE(arr);
fun(arr, size);
printArr(arr, size);
return 0;
}
(a) 20 30 40
(b) 20 20 40
(c) 50 20 40
(d) Compile-time error

Answer
(c) The crux of the question lies in the expression: *arr += *(arr + n – 1) += 10; The composite operator (here +=) has right to left associativity. First 10 is added to the last element of the array. The result is then added to the first element of the array.

6.
# include “stdio.h”
int main ()
{
char a [4] = “sca”;
int i, j;
for (i = 0, j = 3; i < j; a [i++] = a [j–]);
printf (“%s\n”, a);
}
(a) acs
(b) Null String
(c) sca
(d) asacs

Answer
(b) As at the base address or starting of the string “Null” is placed, so while reading array if Null comes it assumes that this is the end of array, so it terminates here only.

7.
# include “stdio.h”
int main ()
{
int i, j;
char a [2] [3] = {{‘a’, ‘b’, ‘c’}, {‘d’, ‘e’, ‘f’}};
char b [3] [2];
char *p = *b;
for (i = 0; i < 2; i++) {
for (j = 0; j < 3; j++) {
*(p + 2*j + i) = a [i] [j];
}
}
}

(a)
a b
c d
e f
(b)
a d
b e
c f
(c)
a c
e b
d f

(d)
a e
d c
b f

Answer
(b) *p= a[0][0] *(p+2) = a[0][1] *(p+4) = a[0][2] *(p+1) = a[1][0] *(p+3) = a[1][1] *(p+5) = a[1][2]

8.
#include “stdio.h”
int main()
{
static int x[2][2] = {1, 2, 3, 4};
int a, b;
static int *y[] = {(int*)x, (int*)x+1, (int*)x+2};
for(a=0; a<2; a++)
{
for(b=0; b<2; b++)
{
printf(“%d, %d, %d, %d\n”, *(*(y+a)+b), *(*(b+y)+a),
*(*(a+y)+b), *(*(y+b)+a));
}
}
return 0;
}

(a)
1, 1, 1, 1
2, 3, 2, 3
3, 2, 3, 2
4, 4, 4, 4

(b)
1, 2, 1, 2
2, 3, 2, 3
3, 4, 3, 4
4, 2, 4, 2

(c)
1, 1, 1, 1
2, 2, 2, 2
2, 2, 2, 2
3, 3, 3, 3

(d)
1, 2, 3, 4
2, 3, 4, 1
3, 4, 1, 2
4, 1, 2, 3

Answer
(c) step1:y=0;
step2:a=0,b=0
*(*(y+a)+b)=x[0]=1
*(*(b+x)+a)=x[0]=1
*(*(a+y)+b)=x[0]=1
*(*(y+b)+a)=x[0]=1

step2:a=0,b=1
*(*(y+a)+b)=x[1]=2
*(*(b+y)+a)=x[1]=2
*(*(a+y)+b)=x[1]=2
*(*(y+b)+a)=x[1]=2
step3:a=1,b=0
*(*(y+a)+b)=x[1]=2
*(*(b+y)+a)=x1]=2
*(*(a+y)+b)=x[1]=2
*(*(y+b)+a)=x[1]=2
step4:a=1,b=1
*(*(y+a)+b)=x[2]=3
*(*(b+y)+a)=x[2]=3
*(*(a+y)+b)=x[2]=3
*(*(y+b)+a)=x[2]=3

9.
#include “stdio.h”
int main()
{
int a[10][20][30] = {0};
a[5][2][1] = 2;
return 0;
}
Run on IDE
Which of the following will print the value 2 for the above code?
(a) printf(“%d”,*(((a+5)+2)+1));
(b) printf(“%d”,***((a+5)+2)+1);
(c) printf(“%d”,*(*(*(a+5)+2)+1));
(d) None of these
Answer

(c) To access a value at a particular index we using pointer,we use *P+indexno

10.
int main()
{
int a[5] = {2, 3};
printf(“%d, %d, %d\n”, a[2], a[3], a[4]);
return 0;
}
(a) Garbage Values
(b) 2,3,3
(c) 3,2,2
(d) 0,0,0
Answer

(d) When an automatic array is partially initialized, the remaining elements are initialized to 0.
Arrays in C