Pointers and Arrays in C: Fundamental Quiz Quiz

Assess your knowledge of pointers and arrays in C with this quiz that covers basic pointer usage, array manipulation, pointer arithmetic, and memory access concepts. Whether you're a beginner or brushing up, these questions are crafted to reinforce essential C programming foundations vital for beginners.

  1. Identifying Array Declaration

    Which of the following correctly declares an array of 5 integers in C?

    1. int arr;
    2. array int arr[5];
    3. int arr[5];
    4. int[5] arr;

    Explanation: The correct way to declare an integer array of 5 elements in C is 'int arr[5];'. The option 'int arr;' declares a single integer, not an array. 'int[5] arr;' is incorrect syntax in C. 'array int arr[5];' uses an invalid keyword, as 'array' is not used in C for declarations.

  2. Pointer Declaration Syntax

    Which syntax correctly declares a pointer to an integer in C?

    1. int *ptr;
    2. int ptr*;
    3. intu0026 ptr;
    4. pointer int ptr;

    Explanation: 'int *ptr;' declares a pointer to an integer by placing the asterisk before the pointer variable name. 'pointer int ptr;' is not valid syntax in C. 'int ptr*;' places the asterisk after the variable name, which is incorrect. 'intu0026 ptr;' is not valid in C; the ampersand indicates a reference in languages like C++, not C.

  3. Accessing Array Elements with Pointers

    What will '*(arr + 2)' return if 'arr' is an integer array containing {4, 8, 15, 16}?

    1. 4
    2. 16
    3. 8
    4. 15

    Explanation: '*(arr + 2)' accesses the third element (index 2) of the array, which is 15. '8' corresponds to arr[1], not arr[2]. '4' is the first element, arr[0], and '16' is the last element, arr[3]. Pointer arithmetic moves in multiples according to the size of the data type.

  4. Pointer Initialization

    What is the value stored in a pointer variable 'p' immediately after the statement 'int *p = NULL;'?

    1. NULL
    2. 0
    3. Uninitialized value
    4. The memory address of an integer

    Explanation: Assigning 'NULL' to a pointer sets it to a value representing no valid memory address. The answer '0' is close, as NULL often evaluates to 0, but using 'NULL' is more explicit and proper in pointer contexts. 'Uninitialized value' is incorrect because 'p' is set to NULL. 'The memory address of an integer' is incorrect since no address is assigned.

  5. Pointer and Array Relationship

    Which expression gives the address of the first element in an array 'nums'?

    1. nums
    2. *nums
    3. u0026nums[0]
    4. nums[1]

    Explanation: In C, the name of the array 'nums' is equivalent to the address of its first element. 'u0026nums[0]' also gives the address of the first element, so it is equally correct, but 'nums' matches the standard definition. 'nums[1]' gives the value at the second element, not an address, and '*nums' dereferences the address, yielding the first value.

  6. Changing Array Elements Using Pointers

    If 'arr' is an integer array and 'ptr = arr;', what does 'ptr[2] = 7;' do?

    1. Updates the third element of 'arr' to 7
    2. Updates the address stored in 'ptr[2]' to 7
    3. Assigns 7 to the pointer variable 'ptr'
    4. Changes all elements of 'arr' to 7

    Explanation: Since 'ptr' points to 'arr', writing 'ptr[2] = 7;' changes 'arr[2]' to 7. Assigning 7 to 'ptr' itself would require 'ptr = 7;', not 'ptr[2] = 7;'. Changing all elements to 7 would require a loop. Setting the address in 'ptr[2]' to 7 would only apply to arrays of pointers, not integers.

  7. Array Indexing and Pointer Arithmetic

    Given 'int values[3] = {10, 20, 30};', what will the expression '*(values + 1)' return?

    1. values
    2. 20
    3. 10
    4. 30

    Explanation: '*(values + 1)' evaluates to the second element of the array, which is 20. '10' is the first element, accessed with '*(values + 0)'. '30' is the third element, '*(values + 2)'. 'values' itself is a pointer to the first element, not a value.

  8. Understanding sizeof Operator

    If 'int arr[10];' and 'int *ptr = arr;', what does 'sizeof(arr) / sizeof(arr[0])' compute to?

    1. 10
    2. 4
    3. 1
    4. Undefined

    Explanation: 'sizeof(arr)' gives the total bytes occupied by the array, and 'sizeof(arr[0])' gives the bytes for one integer. The division computes the number of elements, which is 10 in this case. '1' would only be correct for a single-element array. '4' is often the size of one int, not the count. 'Undefined' is incorrect, as this is a common and defined pattern.

  9. Pointer Dereferencing

    Given 'int num = 5; int *p = u0026num;', what does '*p' return?

    1. u0026num
    2. The value of 'p'
    3. 5
    4. An error

    Explanation: '*p' dereferences the pointer, accessing the value stored where 'p' points, which is 5. 'The value of p' would give the address, not the value. 'u0026num' gives the address of num, while 'An error' is incorrect if the syntax is valid.

  10. Arrays and Function Parameters

    What is passed to a function when an array is used as an argument in C, like 'function(arr);'?

    1. A copy of the first element
    2. The address of the first element
    3. Only the last element
    4. The entire array

    Explanation: In C, using an array as a function argument passes the address of its first element, allowing the function to manipulate the original array. 'The entire array' would require passing by value, which C does not do for arrays. 'A copy of the first element' is incorrect, as the whole array can be manipulated. 'Only the last element' is not relevant to how arrays are passed.