The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

C Interview Array Questions

In most of the MNC interview questions such as in ZOHO interview question, IVTL Infoview interview questions, Amazon interview questions, GOOGLE interview questions, Infosys interview questions and even in Voonik interview questions, We come across several Tricky C Questions about which 2:5 of the questions are from Array in c. Solving that kind of tricky C questions is not an easy task for all C programmers. We need more practices to solve it with ease. So we provide 25+ interesting C questions in Array to make your MNC interview very easy.

C Array Questions

6. What will be the output of the C program?

#include<stdio.h>
int main()
{
	int arr[5][5][5] = {0};
	printf("%d", ( &arr+1 - &arr ));
	return 0;
}

A. 0

B. Compilation error

C. 1

D. 4

x

 

Option: C

Explanation

printf("%d", (&arr+1 - &arr)); let us consider the address of an array arr starts from 2293420
then, printf("%d", (2293420 +1 - 2293420);
printf("%d", 0 + 1);
printf("%d", 1);
Thus 1 is outputted.

Answer


7. What will be the output of the C program, if input is 6?

#include<stdio.h>
void fun(int[][3]);
int main(void)
{
	int arr[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
	fun(arr);
	printf("%d\n", arr[2][1]);
	return 0;
}
void fun(int b[][3])
{
	++b;
	b[1][1] = 15;
}

A. 15

B. 9

C. 8

D. 7

x

 

Option: A

Explanation

This question from 2braces.com is more tricky.
fun(arr) returns the address of first element in an array to the function void fun();
Let us consider the address of the values in arr[3][3] is 2293420.
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
Now b[1][1] = 15 will affect the value 8 in arr of array.
Thus arr[2][1] outputted 15.

Answer


8. What will be the output of the C program by considering 'b' as a User input?

#include<stdio.h>
int main(){
	int rows = 3, colums = 4, i, j, k;
	int a[3][4] = {1, 2, 3, 5, 7};
	i = j = k = 00;
	for(i = 0;i<rows;i++)
	for(j = 0;j<colums;j++)
	if(a[k][j]<k)
	k = a[i][j];
	printf("%d\n", k);
	return 0;
}

A. 00

B. No output

C. 0

D. 7

x

 

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


9. What will be the code to print 5 contains in a[4][1][0]?

#include<stdio.h>
int main()
{
	int arr[ ]={1.2, 2.4, 3.6, 4.8, 5};
	int j, *ptr = arr;
	for(j = 0;j<5;j++)
	{
	printf("%d ", *arr);
	++ptr;
	}
}

A. 2 2 2 2 2

B. 1 1 1 1 1

C. 1 2 3 4 5

D. None of the above

x

 

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


10. What will be the output of the C program?

#include<stdio.h>
int main()
{
	int arr[5][5][5] = {0};
	int *b = arr;
	int *c = arr + 1;
	printf("%d", c - b);
	return 0;
}

A. 0

B. Runtime Error

C. 25

D. Some address

x

 

Option: C

Explanation

Clearly array arr[5][5][5] can hold upto 25 integer values.
let us consider the address of first element in an array arr[5][5][5] is 2292932
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;
here, printf("%d", c-b);
printf("%d", 2292932 - 2293032);
printf("%d", 100); this is not yet over
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


Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report