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

C Array Interview 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

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

#include<stdio.h>
int main(void)
{
	int arr[5] = { 1, 2, 3, 5, 7 };
	int *ptr = (&arr + 1);
	printf("%d %d\n", *(arr + 1), *(ptr - 1));
	return 0;
}

A. 2 5

B. 3 5

C. 2 7

D. 3 7

x

 

Option: C

Explanation

let's go from line 5...
*ptr = (address of first value in arr[] array + 1)
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();
printf("%d %d\n", *(2293420 + 1), *(2293436 -1));
printf("%d %d\n", *(2293424), *(2293432));
printf("%d %d\n", 2, 7);
thus 2 7

Answer


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

#include<stdio.h>
int main()
{
	int a[][3] = {0, 1, 2, 3, 4, 5};
	int (*ptr)[3] = a;
	printf("%d %d ", (*ptr)[0], (*ptr)[1]);
	++ptr;
	printf("%d %d\n", (*ptr)[0], (*ptr)[1]);
	return 0;
}

A. 0 1 3 4

B. 0 1 0 1

C. 0 1 2 3

D. 0 1 1 2

x

 

Option: A

Explanation

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;
	char arr[10] = {1, 2, 3, 4, 5, 6, 9, 8};
	temp = (arr + 1)[2];
	printf("%d\n", temp);
	return 0;
}

A. 2

B. 3

C. 4

D. 5

x

 

Option: C

Explanation

Here, temp = (arr + 1)[2];
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)";
Now temp = 4;(address = 2293428)
Thus the program outputted 4.

Answer


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

#include<stdio.h>
int main()
{
	int a[1][2][3] = {0};
	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);

D. None of the above

x

 

Option: B

Explanation

Simply, this is a format for naviting to a value using the address of a first element in an array.

Answer


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

#include<stdio.h>
void fun(char**);
int main()
{
	char *arr[] = { "bat", "cat", "fat", "hat", "mat", "pat" };
	fun(arr);
	return 0;
}
void fun(char **p)
{
	char *t;
	t = (p += sizeof(int))[-1];
	printf("%s\n", t);
}

A. mat

B. fat

C. hat

D. cat

x

 

Option: C

Explanation

fun(arr) returns the address of first element in an array arr Let we start from the function void fun().
*t is a pointer variable which holds t = (p += sizeof(int))[-1];
ie ) t = (p = p + sizeof(int)) [-1];
t = (p = p + 4) [-1];
t = (p = address of bat + 4)[-1];
let us consider a address of bat is 2293416,
t = (p = 2293416 + 4)[-1];
t = (p = 2293432)[-1]
t = ("mat")[-1]; // index from "mat"
t = "hat";
thus hat is outputted.

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