Search Placementyogi.com
 

Arrays In C Objective Questions and Answers

  :: Topics ::

1

What is the output of the following program?

#include<stdio.h>
void main()
{
	int arr[5]={1,2,3,4,5,6};
	printf("size=%d ", sizeof(arr));
	printf("arr[5]=%d",arr[5]);
}
A.

5 GarbageValue

B.

20 GarbageValue

C.

6 GarbageValue

D.

24 6

E.

6 6

See Answer & Explanation Lets Discuss
Correct answer is : B
Explanation

The statement arr[5] fixes the size of the array to 5. Any attempt to assign a value outside this limit is discarded.

Here , sizeof(arr) pritns the size as 20 bytes as each int occupies 4 bytes and 5 *4=20 bytes.

The arr[5] prints a Garbage Value as it is trying to access the memory location outside the array size.

 
2

What is the output of the following program?

#include<stdio.h>
void main()
{
        int arr[]={1.999999,2.999999};
        printf("%d %d %d %d ",*(arr),*(arr+0), 1[arr],0[arr+1]);
}
A.

1.9999990 1.999990 2.9000000 2.900000

B.

1 1 2 2

C.

GarbageValue GarbageValue 2 2

D.

Compilation Error

E.

None of the Above

See Answer & Explanation Lets Discuss
Correct answer is : B
Explanation

 

If we assign a float value to an array of integers, the float values are converted to an integer by discarding the decimal and anything that follows the decimal.

*arr is the value at "arr" i.e. the base address of array =1

*(arr+0) is the value at the address arr+0 i.e. at the 0th index of arr =1

1[arr] is same as arr[1] =2

0[arr+1] is same as arr[0+1] = arr[1] =2

 

 

 
3

What is the output of the following program?

#include<stdio.h>
void main()
{
	int[] a={100,200,300};
	printf("%d",a[2]);
}
A.

200

B.

300

C.

Compilation Error

D.

None of the Above

See Answer & Explanation Lets Discuss
Correct answer is : C
Explanation

int[] a; // this is not a valid way of declaring an array.

int a[]; // this is valid

 
4

What is the output of the following program if the base address of the array is 1000?

#include<stdio.h>
void main()
{
	int arr[]={10,20,30,40};
	printf("%u %u %u",arr,&arr[0],&arr);
}
A.

1000 1000 1000

B.

1000 1000 2000

C.

1000 1100 1200

D.

None of the Above

See Answer & Explanation Lets Discuss
Correct answer is : A
Explanation

Important points to remember:

1. The name of an array refers to the base address of the array.

2. The address of the first element of the array is same as the base address of the array.

3. The array name is not a pointer but a variable of type array and it's address is same as the base address of the array.

Here,

"arr" is a variable whose value is equal to 1000 i.e. the base address of the array,i.e. arr=1000

The address of the first element of the array is also 1000, i.e. &arr[0] =1000

The address of "arr" is also 1000 i.e. &arr = 1000.

Hence, the output is 1000 1000 1000

 
5

What is the output of the following program if the base address of arr is 1000?

#include<stdio.h>
void main()
{
	int arr[]={1,2,3,4,5};
	int *p,(*q)[];
	p=arr;
	q=arr;
	printf("arr=%u &arr=%u p=%u &p=%u q=%u &q=%u",arr,&arr,p,&p,q,&q);
}
A.

arr=1000 &arr=1000 p=1000 &p=1200 q=1000 &q=1300

B.

arr=1000 &arr=1000 p=1000 &p=1000 q=1000 &q=1000

C.

arr=1000 &arr=1100 p=1000 &p=1200 q=1000 &q=1300

D.

Error: q can not refer to an array.

See Answer & Explanation Lets Discuss
Correct answer is : A
Explanation

 

arr contains the base address of the array.

arr is not a pointer but a variable of type array and it's address is same as the base address of the array.

This might be hard to digest and you might ask: How can the value and the address of a variable be same?

Well in this special case it is. Just assume that the variable arr contains it's own address.

It is this property of an array which differentiates it from a pointer.

 

p is a pointer and contains the base address of the array.

p is itself stored at a different location and hence &p prints a different value.

 

q is a pointer to an array of integers and hence contains the address of the array "arr".

q is itself stored at a different location and hence &q prints a different value.

 

 
 

Banner to LogicGuns.com

Hide Page Information

C Arrays Objective Questions and Answers for Written Round Interview

Here you can find C Arrays Objective Questions with answers and explanation. We have a vast collection of Objective Questions on Arrays in C that will help you a lot!

Why learn C Arrays?

Arrays are an integral part of C and many Objective Questions in the written round are asked from this topic.In this section you can learn and practice C Arrays (Objective Questions with Answers) to improve your skills in order to face the interview and clear campus and off-campus selection of TCS, Infosys, Wipro etc.

Where can I get C Arrays Objective Questions and Answers with Explanation?

PlacementYogi provides you lots of fully solved C Arrays Objective Questions and answers with explanation. Fully solved examples with detailed answer description, explanation are given and it would be easy to understand.Though we do not have pdf file for C Arrays, We have quiz Objective Questions with answers to help you test yourself..

\n

Where can I get C Arrays Interview Objective Questions and Answers (objective type, multiple choice)?

Here you can find objective type C Arrays Objective Questions and answers for interview and entrance examination.Multiple Answers and true or false type Objective Questions are also provided.

How to solve C Arrays problems?

You can easily solve all kind of Objective Questions based on C Arrays by practicing the exercises (including shortcut Arrays to solve problems) given below.