C Array Quiz - MCQ Questions and Answers

1. What is an array in C?

a) A collection of values of different data types
b) A collection of functions
c) A collection of values of the same data type
d) A special type of variable

Answer:

c) A collection of values of the same data type

Explanation:

In C, an array is a data structure that can store a fixed-size sequential collection of elements of the same type.

2. How is an array declared in C?

a) dataType arrayName[size];
b) array dataType[size];
c) dataType[size] arrayName;
d) arrayName[size] dataType;

Answer:

a) dataType arrayName[size];

Explanation:

An array is declared in C by specifying the data type of its elements, followed by the array name and its size in square brackets.

3. Which of the following array declarations is correct in C?

a) int numbers[5] = {1, 2, 3, 4, 5};
b) float rates[] = {2.5, 3.0, 4.5, 5.5};
c) char name[10] = "John";
d) All of the above

Answer:

d) All of the above

Explanation:

All these declarations are correct ways to declare arrays in C.

4. How do you access the third element in an array named 'data' in C?

a) data[2]
b) data[3]
c) data(2)
d) data(3)

Answer:

a) data[2]

Explanation:

Array indices in C start from 0, so the third element is accessed using data[2].

5. What is the default value of elements in an uninitialized array in C?

a) Null
b) 0
c) Garbage values
d) It depends on the compiler

Answer:

c) Garbage values

Explanation:

Uninitialized array elements in C contain garbage values (i.e., whatever value is already at the memory location).

6. What is the size of an array 'arr' declared as int arr[10] in C?

a) 10 bytes
b) 20 bytes
c) 40 bytes
d) Depends on the system

Answer:

d) Depends on the system

Explanation:

The size of the array depends on the size of an int on the system. Typically, if an int is 4 bytes, the array size would be 4 bytes * 10 = 40 bytes.

7. How can the number of elements in an array be determined in C?

a) Using the sizeof operator
b) Using the length() function
c) By manually counting the elements
d) Using the size() function

Answer:

a) Using the sizeof operator

Explanation:

The number of elements in an array can be determined by dividing the total size of the array (obtained using sizeof) by the size of an element.

8. What is the output of the following C code?

   int arr[] = {1, 2, 3, 4, 5};
   printf("%d", arr[3]);
a) 3
b) 4
c) 5
d) Error

Answer:

b) 4

Explanation:

Array indices start at 0, so arr[3] refers to the fourth element, which is 4.

9. Which of the following is a correct way to initialize an array in C?

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

Answer:

d) a) and c)

Explanation:

Both a) and c) are correct. In a), all elements are initialized to 0. In c), the array size is determined by the number of elements in the initializer.

10. Can the size of an array be changed at runtime in C?

a) Yes, always
b) No, never
c) Only for dynamic arrays
d) Only if it is a global array

Answer:

b) No, never

Explanation:

In C, the size of an array is fixed at compile-time and cannot be changed at runtime.

11. What is a multidimensional array in C?

a) An array with elements of different types
b) An array with functions as elements
c) An array of arrays
d) An array with more than 100 elements

Answer:

c) An array of arrays

Explanation:

A multidimensional array in C is an array whose elements are themselves arrays.

12. How do you initialize a two-dimensional array in C?

a) Using nested loops
b) Using multiple one-dimensional arrays
c) Using a single initializer list with commas
d) Using nested initializer lists

Answer:

d) Using nested initializer lists

Explanation:

A two-dimensional array is initialized using nested initializer lists, each representing a row of the array.

13. What does array decay refer to in C?

a) Conversion of an array into a single value
b) Loss of array type information and conversion to a pointer
c) Physical degradation of an array over time
d) Decrease in array size at runtime

Answer:

b) Loss of array type information and conversion to a pointer

Explanation:

Array decay is the process where an array loses its type and size information and becomes a pointer to its first element.

14. What is the output of the following C code?

int arr[3] = {0};
   printf("%d %d %d", arr[0], arr[1], arr[2]);
a) 0 0 0
b) Garbage values
c) Error
d) 0 followed by garbage values

Answer:

a) 0 0 0

Explanation:

The array is initialized with all elements set to 0, so the output will be 0 0 0.

15. Which of the following statements about arrays in C is correct?

a) Arrays can store elements of different data types
b) Array indices start at 1
c) The size of an array must be a constant expression
d) Elements in an array are accessed using parentheses

Answer:

c) The size of an array must be a constant expression

Explanation:

In C, the size of an array must be known at compile time and must be a constant expression.

16. What is a pointer to an array in C?

a) A variable that points to the first element of an array
b) A special type of array
c) A function that returns an array
d) An array of pointers

Answer:

a) A variable that points to the first element of an array

Explanation:

A pointer to an array in C is a variable that stores the address of the first element of the array.

17. How do you pass an array to a function in C?

a) By specifying the array elements individually
b) By passing the array name
c) By passing the first element of the array
d) By creating a copy of the array

Answer:

b) By passing the array name

Explanation:

In C, an array is passed to a function by passing the array name, which is a pointer to the first element of the array.

18. Can an array contain a pointer in C?

a) Yes
b) No
c) Only in special cases
d) Only if it is a void pointer

Answer:

a) Yes

Explanation:

An array in C can contain pointers as its elements.

19. What is the relationship between arrays and pointers in C?

a) They are completely unrelated
b) They are the same thing
c) Arrays can be treated as pointers
d) Pointers can only point to single values, not arrays

Answer:

c) Arrays can be treated as pointers

Explanation:

In C, arrays and pointers are closely related. An array name can be treated as a pointer to the first element of the array.

20. What is the output of the following C code?


   int arr[] = {1, 2, 3};
   printf("%d", *(arr + 1));
a) 1
b) 2
c) 3
d) Error

Answer:

b) 2

Explanation:

The expression *(arr + 1) is equivalent to arr[1], which accesses the second element of the array, giving the output 2.

Comments