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?
Answer:
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?
Answer:
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:
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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]?
Answer:
Explanation:
Elements that aren't explicitly initialized are automatically initialized to 0.
8. Which function is used to determine the length of an array?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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};
Answer:
Explanation:
Array indices start from 0. Hence, arr[2] refers to the third element which is 30.
Comments
Post a Comment