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
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
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
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
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
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
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
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
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
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