Arrays in C: Fundamentals u0026 Applications Quiz Quiz

Challenge your understanding of arrays in C programming with this quiz focused on array declaration, initialization, memory layout, and common operations. Explore key concepts and practical scenarios relevant for students and developers working with one-dimensional and multidimensional arrays in C.

  1. Identifying Valid Array Declarations

    Which of the following lines correctly declares an integer array of size 5 in C?

    1. int arr[5];
    2. int arr(5);
    3. int arr{5};
    4. int arr = [5];

    Explanation: The correct syntax to declare an integer array of size 5 in C is 'int arr[5];' using square brackets. The option 'int arr(5);' incorrectly uses parentheses, which is not valid for arrays. 'int arr = [5];' is not recognized syntax in C. 'int arr{5};' employs curly braces, which is an initialization style not supported here. Only the first option follows standard array syntax in C.

  2. Array Indexing and Memory

    What value does the first index of array 'int data[4] = {3, 7, 2, 9};' hold in C?

    1. 3
    2. 9
    3. 2
    4. 7

    Explanation: In C, array indices start from zero, so 'data[0]' represents the first element of the array, which is 3. The other options correspond to later elements: 'data[1]' is 7, 'data[2]' is 2, and 'data[3]' is 9. Remember that using indices greater than 0 would not access the first element in C arrays.

  3. Default Values in Local Arrays

    If a local integer array is declared as 'int nums[3];' in a C function without initialization, what are the values of its elements?

    1. Zeros
    2. Garbage values
    3. Ones
    4. Nulls

    Explanation: Local variables in C, including arrays, are not automatically initialized, so they contain garbage values (indeterminate data). The option 'Zeros' would be correct for static or global arrays, but not for local ones. 'Ones' and 'Nulls' are incorrect, as C does not assign these default values to uninitialized local arrays. Always initialize local arrays to avoid undefined behavior.

  4. Passing Arrays to Functions

    When an array is passed to a function in C, which statement accurately describes what happens?

    1. The function receives a pointer to the first element
    2. Only the array size is passed
    3. A reference to the whole array is passed
    4. The entire array is copied into the function

    Explanation: Passing an array to a function in C results in the function receiving a pointer to the first element of the array, not a full copy or a true reference. The entire array is not copied into the function, which helps avoid performance issues. Only passing the size is also incorrect, though the array size can be passed separately if needed. C does not support passing arrays by reference directly as a single entity.

  5. Identifying Valid Multidimensional Array Access

    Given 'int grid[2][3] = {{1,2,3},{4,5,6}};', what is the value of 'grid[1][2]'?

    1. 3
    2. 6
    3. 2
    4. 5

    Explanation: For the two-dimensional array 'grid', 'grid[1][2]' refers to row 1 and column 2, which holds the value 6. 'grid[1][1]' would correspond to 5, and 'grid[0][2]' would be 3, while 'grid[0][1]' is 2. Multidimensional arrays in C use zero-based indexing for both dimensions, so careful attention to indices is required.