Array Questions
Array Questions
Array Questions
me/campusdrive
C Arrays Questions
1. What will be the output of the C program?
#include<stdio.h>
int main(void)
int arr[5] = { 1, 2, 3, 5, 7 };
return 0;
A. 2 5
B. 3 5
C. 2 7
D. 3 7
Option: C
Explanation
let us consider 2293416 is a address of first value in arr[] array i.e) *ptr = (2293416 + 1)
i.e) *ptr = (2293436) and not 2293420 because 1 points to the next location after all the addressess of
values in an array arr[]
here, the address of a value 7 is 2293432. Then the address of *ptr is 2293436
coming to printf();
https://t.me/campusdrive
thus 2 7
✉ Answer
#include<stdio.h>
int main()
int (*ptr)[3] = a;
++ptr;
return 0;
A. 0 1 3 4
B. 0 1 0 1
C. 0 1 2 3
D. 0 1 1 2
Option: A
Explanation
https://t.me/campusdrive
Here, *ptr[3] is a pointer array which holds the address of first element in an array a[][3]. Now the
address of a[][3] and *ptr[3] are same, which means any changes made to one of the variable will affect
other variable.
now *ptr[3] looks like this *ptr[3] = {0, 1, 2}, thus first printf outputted 0 1
In the very next line we have ++ptr;, which pre-increment the address of ptr, i.e) let us consider the
address of ptr is 2293432 and after pre-increment the address of ptr will be 2293444 and not 2293436 in
this case, because we are incrementing array and not a value in an array.
Now the value of ptr looks like *ptr[3] = {3, 4, 5}, thus second printf outputted 3 4
✉ Answer
3. What will be the output of the C program by considering 'b' as a User input?
#include<stdio.h>
int main()
char temp;
printf("%d\n", temp);
return 0;
A. 2
B. 3
C. 4
D. 5
Option: C
Explanation
https://t.me/campusdrive
Let us consider the address of first element in an array arr[10] is 2293416 then temp looks like this temp
= (2293416 + 1)[2];
Now temp =(2293420)[2];, which denotes temp = "index value of 2 from the address 2293420(value =
2)";
✉ Answer
#include<stdio.h>
int main()
a[0][1][2] = 5;
printf("%d",*(*(*(a+0)+1)+2));
return 0;
A. printf("%d",*(((a+0)+1)+2));
B. printf("%d",*(*(*(a+0)+1)+2));
C. printf("%d",***((a+0)+1)+2);
Option: B
Explanation
Simply, this is a format for naviting to a value using the address of a first element in an array.
https://t.me/campusdrive
✉ Answer
#include<stdio.h>
void fun(char**);
int main()
fun(arr);
return 0;
char *t;
t = (p += sizeof(int))[-1];
printf("%s\n", t);
A. mat
B. fat
C. hat
D. cat
Option: C
Explanation
fun(arr) returns the address of first element in an array arr Let we start from the function void fun().
https://t.me/campusdrive
ie ) t = (p = p + sizeof(int)) [-1];
t = (p = p + 4) [-1];
t = (p = 2293416 + 4)[-1];
t = (p = 2293432)[-1]
t = "hat";
✉ Answer
#include<stdio.h>
int main()
return 0;
A. 0
B. Compilation error
C. 1
D. 4
x
https://t.me/campusdrive
Option: C
Explanation
printf("%d", (&arr+1 - &arr)); let us consider the address of an array arr starts from 2293420
printf("%d", 0 + 1);
printf("%d", 1);
Thus 1 is outputted.
✉ Answer
#include<stdio.h>
void fun(int[][3]);
int main(void)
fun(arr);
printf("%d\n", arr[2][1]);
return 0;
++b;
b[1][1] = 15;
A. 15
https://t.me/campusdrive
B. 9
C. 8
D. 7
Option: A
Explanation
fun(arr) returns the address of first element in an array to the function void fun();
when it passes through the function void fun(int b[][3]), its value is pre-incremented ++b
As it is a multi dimensional array ++b will not skip the address next to the last value in an array arr[][3]
instead it skip the address next to first part only i.e) now b[][3] array starts with the address 2293432
(i.e) starts from the value 4 but index from 0, Clearly b[0][0] = 4
✉ Answer
8. What will be the output of the C program by considering 'b' as a User input?
#include<stdio.h>
int main(){
i = j = k = 00;
for(i = 0;i<rows;i++)
for(j = 0;j<colums;j++)
if(a[k][j]<k)
https://t.me/campusdrive
k = a[i][j];
printf("%d\n", k);
return 0;
A. 00
B. No output
C. 0
D. 7
Option: C
Explanation
Initially we set i = 0, j = 0, k = 0. zero be never greater than any integer values in an array a[3][4], thus if
condition fails. and 0 is outputted.
✉ Answer
#include<stdio.h>
int main()
for(j = 0;j<5;j++)
++ptr;
}
https://t.me/campusdrive
A. 2 2 2 2 2
B. 1 1 1 1 1
C. 1 2 3 4 5
Option: B
Explanation
Initially array arr is assigned to a pointer variable ptr. In the for loop, ptr is incremented and not arr. So
the value 1 1 1 1 1 will be printed. as we use integer type decimal values are all exempted.
✉ Answer
#include<stdio.h>
int main()
int *b = arr;
int *c = arr + 1;
printf("%d", c - b);
return 0;
A. 0
B. Runtime Error
C. 25
D. Some address
https://t.me/campusdrive
Option: C
Explanation
Now *b = 2292932; *c = arr + 1; i.e) *c contains the address which is located next to the last value
address in an arr[5][5][5], which is the address location next to that 25th value in an array arr[5][5][5].
Now *c = 2293032;
printf("%d", 100/ sizeof(int)); as it is an integer type values we have to divide it by sizeof(int) to display
value not the address.
printf("%d", 25);
thus 25.
✉ Answer
#include<stdio.h>
int main()
int i = 0;
printf("Hello");
for(i = 0;i<4;i++){
https://t.me/campusdrive
printf("%c", s[i]);
return 0;
A. Hello
B. Compilation error
C. Hell
Option: C
Explanation
i.e) Hello\b\t\r\n.
i.e) Hell\t\r\n.
i.e) Hell\n.
✉ Answer
#include<stdio.h>
int main()
int **ptr = p;
++*ptr;
return 0;
A. 0 1 1
B. 0 0 1
C. 0 1 2
D. 1 1 2
Option: A
Explanation
*p[] is a pointer array variable which holds the all 5 addressess of a value in static integer array a[].
Our assumption
**ptr = p;
**ptr = 0;
**ptr = p;
*(value of first element in an array p[]) == *(address of first element in an array a[])
*(address of first element in an array a[]) == value of first element in an array a[];
that is 0
++(value of first element in an array p[]) == ++(address of first element in a[] which is address of 0)
++(address of first element in a[] which is address of 0) == address of second element in a[] which is
address of 1
Thus 0 1 1 is outputted.
✉ Answer
#include<stdio.h>
int main()
int i = 0;
https://t.me/campusdrive
for(i = 0;i<4;i++)
printf("%c", s[i]);
return 0;
A. \0 \0 \0
B. \0 \0 \0 \0
C. No output
Option: C
Explanation
✉ Answer
#include<stdio.h>
int main()
p = &s[3];
str = p;
https://t.me/campusdrive
str1 = s;
return 0;
A. 76
B. 77
C. 78
D. 79
Option: B
Explanation
p = &s[3].
str = p;
str1 = s;
Thus 77 is outputted.
✉ Answer
https://t.me/campusdrive
#include<stdio.h>
int main()
int i = 0;
printf("Hello");
for(i = 0;i<4;i++)
printf("%c", s[i]);
return 0;
A. Hello
B. Hell
C. No output
D. Compilation error
Option: C
Explanation
i.e) Hello\b\r\t\n.
i.e) Hell\r\t\n.
i.e) \t\n.
https://t.me/campusdrive
i.e) \n.
✉ Answer
#include<stdio.h>
int main()
printf("%d", arr[3]);
return 0;
A. 3
B. 4
D. Compilation error
Option: C
Explanation
Here the size of an array is 2, but the value inside array is exceed 2. Thus it prints garbage value for index
more than 1
✉ Answer
#include<stdio.h>
int main()
https://t.me/campusdrive
int a, b, c;
a = ++arr[1];
b = arr[1]++;
c = arr[a++];
printf("%d--%d--%d", a, b, c);
return 0;
A. 4--3--25
B. 3--3--25
C. 4--4--25
D. 3--4--25
Option: A
Explanation
here, a = ++arr[1];
i.e) a = 3 //arr[2];
b = arr[1]++;
i.e) b = 3 //arr[2];
c = arr[a++];
i.e) c = 25 //arr[4];
printf("%d--%d--%d",a, b, c);
https://t.me/campusdrive
printf("%d--%d--%d",4, 3, 25);
✉ Answer
#include<stdio.h>
int main()
ptr = &arr;
ptr1 = *ptr + 3;
A. 1--11
B. 1-7
C. 1--4
D. 1--some address
Option: C
Explanation
i.e) ptr1 = 1 + 3;
i.e) ptr1 = 4;
printf("%d--%d", 1, 4);
✉ Answer
#include<stdio.h>
int main()
int arr[5] = { 1, 3, 5, 7, 11 };
int *ptr;
ptr = &arr;
A. 1
B. 2
C. 3
D. Runtime error
Option: B
Explanation
printf("%d", 1 + 1);
Thus 2 is outputted.
✉ Answer
#include<stdio.h>
int main()
char ***p;
p = ptr;
**++p;
printf("%s",*--*++p + 2);
A. Nothing prints
B. ke
C. ike
D. Compilation error
Option: C
Explanation
here, p = ptr;
https://t.me/campusdrive
**++p;
*(address of car);
printf("%s",*--*++p + 2);
*--*++p;
*--(address of bus)
*(address of bike)
Thus ke is outputted.
✉ Answer
#include<stdio.h>
https://t.me/campusdrive
int main()
printf("%d", arr[1]);
return 0;
A. 1
B. 2
C. Compilation error
D. Runtime error
Option: C
Explanation
✉ Answer
#include<stdio.h>
int main()
printf("%c", arr[2]);
return 0;
A. c
https://t.me/campusdrive
B. b
C. Compilation error
D. Runtime error
Option: C
Explanation
✉ Answer
#include<stdio.h>
int main()
printf("%d", 0[arr]);
return 0;
A. Compilation error
C. 2
D. 0
Option: C
Explanation
https://t.me/campusdrive
✉ Answer
#include<stdio.h>
int main()
int *ptr;
ptr = &arr;
array(&ptr);
return 0;
printf("%d", **p);
B. 3
C. address of ptr
D. Runtime error
x
https://t.me/campusdrive
Option: B
Explanation
array(&ptr);
printf("%d", **p);
Thus 3 is outputted.
✉ Answer
#include<stdio.h>
int main()
int arr[3], i = 0;
while(i < 3)
arr[i] = ++i;
}
https://t.me/campusdrive
printf("%d--", arr[i]);
return 0;
A. Compilation error
B. 1--2--3--
C. Garbage value--1--2--
Option: C
Explanation
Simply arr[0] is left while filling the numbers in array using while loop.
arr[1] = 1;
arr[2] = 2;
✉ Answer