C Array MCQ

Arrays are fundamental to programming, and in C, they allow us to store multiple values of the same data type in a single data structure. Ready to assess your understanding of C arrays? Let’s jump into this multiple-choice quiz.

Each question is followed by the correct answer and an explanation to help reinforce your knowledge.

1. How is an array with 5 integers declared in C?

A) int a(5);
B) int a[5];
C) int[5] a;
D) array a[5];

Answer:

B) int a[5];

Explanation:

Arrays in C are declared with the datatype followed by the array name and its size in square brackets.

2. What is the index of the first element in an array?

A) 0
B) 1
C) First
D) Start

Answer:

A) 0

Explanation:

Array indexing in C starts from 0, so the first element’s index is 0.

3. To access the third element of an array named ‘arr’, you would use:

A) arr[2]
B) arr[3]
C) arr(2)
D) arr(3)

Answer:

A) arr[2]

Explanation:

As indexing starts from 0, the third element is accessed using index 2.

4. What will be the size in bytes of an array int arr[20]; if an integer requires 4 bytes of memory?

A) 40
B) 80
C) 20
D) 5

Answer:

B) 80

Explanation:

For 20 integers each of 4 bytes, the array will be 20 * 4 = 80 bytes.

5. Which of the following is a valid declaration for a 2D array?

A) int a[][] = {{1,2}, {3,4}};
B) int a[2,2] = {{1,2}, {3,4}};
C) int a[2][2] = {{1,2}, {3,4}};
D) array a[2][2] = {{1,2}, {3,4}};

Answer:

C) int a[2][2] = {{1,2}, {3,4}};

Explanation:

In C, multi-dimensional arrays are declared by specifying each dimension’s size inside its respective brackets.

6. Which of the following is the correct way to declare a 3D array?

A) int a[][][] = {{{1},{2},{3}},{{4},{5},{6}}};
B) int a[3][3][3];
C) int[3][3][3] a;
D) Both A and B

Answer:

D) Both A and B

Explanation:

Both declarations are valid ways to declare a 3D array.

7. In an array declaration int arr[5] = {1, 2, 3};, what will be the value of arr[3]?

A) 1
B) 2
C) 3
D) 0

Answer:

D) 0

Explanation:

Elements that aren’t explicitly initialized are automatically initialized to 0.

8. Which function is used to determine the length of an array?

A) sizeof()
B) length()
C) len()
D) size()

Answer:

A) sizeof()

Explanation:

The sizeof() function can be used to determine the memory size occupied by the array. To get the number of elements, you’d divide the size of the entire array by the size of a single element.

9. Which statement about arrays in C is false?

a) Arrays can store multiple values of different data types.
b) The size of an array must be specified at the time of declaration.
c) The elements of an array are stored in contiguous memory locations.
d) The array index starts from 0.

Answer:

a) Arrays can store multiple values of different data types.

Explanation:

Arrays in C store multiple values of the same data type.

10. Which of the following correctly initializes an array of size 5 with all values set to 0?

A) int arr[5] = {0};
B) int arr[5];
C) int arr[5] = {};
D) int arr[] = {0,0,0,0,0};

Answer:

A) int arr[5] = {0};

Explanation:

When an array is initialized with fewer values than its declared size, the remaining elements get automatically initialized to 0.

11. How would you declare a two-dimensional integer array of 3 rows and 4 columns?

a) int arr[3, 4];
b) int arr[3][4];
c) int[3][4] arr;
d) int arr[ ][ ] = {3, 4};

Answer:

b) int arr[3][4];

Explanation:

A two-dimensional array in C is declared using the type name[row_size][column_size].

12. What will the value of arr[2] be in the following code segment?

int arr[] = {10, 20, 30, 40, 50};
a) 10
b) 20
c) 30
d) 40

Answer:

c) 30

Explanation:

Array indices start from 0. Hence, arr[2] refers to the third element which is 30.


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top