Pointers - C Questions

Pointers Questions - 1 Pointers Questions - 2 Pointers Questions - 3 Pointers Questions - 4 Pointers Questions - 5 Pointers Questions - 6 Pointers Questions - 7 Pointers Questions - 8

Data Types - C Questions

Data Types Questions - 1 Data Types Questions - 2 Data Types Questions - 3 Data Types Questions - 4 Data Types Questions - 5

Operators - C Questions

Operators Questions - 1 Operators Questions - 2 Operators Questions - 3 Operators Questions - 4 Operators Questions - 5

Structures - C Questions

Structures Questions - 1 Structures Questions - 2 Structures Questions - 3 Structures Questions - 4 Structures Questions - 5

Files - C Questions

Files Questions - 1 Files Questions - 2 Files Questions - 3 Files Questions - 4 Files Questions - 5

printf - C Questions

printf Questions - 1 printf Questions - 2 printf Questions - 3 printf Questions - 4 printf Questions - 5

Variables - C Questions

Variables Questions - 1 Variables Questions - 2 Variables Questions - 3 Variables Questions - 4 Variables Questions - 5

Preprocessor Macros

Preprocessor Macros - 1 Preprocessor Macros - 2 Preprocessor Macros - 3 Preprocessor Macros - 4 Preprocessor Macros - 5

While Loop - C Questions

While Loop Questions - 1 While Loop Questions - 2 While Loop Questions - 3 While Loop Questions - 4 While Loop Questions - 5

for Loop - C Questions

for Loop Questions - 1 for Loop Questions - 2 for Loop Questions - 3 for Loop Questions - 4 for Loop Questions - 5

if else - C Questions

if else Questions - 1 if else Questions - 2 if else Questions - 3 if else Questions - 4 if else Questions - 5

Switch case - C Questions

Switch case Questions - 1 Switch case Questions - 2 Switch case Questions - 3 Switch case Questions - 4 Switch case Questions - 5

Array - C Questions

Array Questions - 1 Array Questions - 2 Array Questions - 3 Array Questions - 4 Array Questions - 5

Functions - C Questions

Functions Questions - 1 Functions Questions - 2 Functions Questions - 3 Functions Questions - 4 Functions Questions - 5

Memory Allocation

Memory Allocation - 1 Memory Allocation - 2
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

We to update you