What is the time complexity of accessing an element at a specific index in an array?
Accessing an element at a specific index in an array takes constant time, O(1), because arrays allow direct access using the index.
Click to reveal answer
beginner
How do you update the value at index 3 in an integer array named arr in C?
You update the value by assigning a new value using the index: arr[3] = new_value;
Click to reveal answer
intermediate
What happens if you try to access or update an index outside the array bounds in C?
Accessing or updating an index outside the array bounds causes undefined behavior, which can lead to program crashes or incorrect results.
Click to reveal answer
intermediate
Explain why arrays provide fast access to elements compared to linked lists.
Arrays store elements in contiguous memory locations, so accessing an element by index is direct and fast (O(1)). Linked lists require traversal from the head to reach an element, which takes O(n) time.
Click to reveal answer
advanced
In C, how is the expression arr[i] interpreted internally?
The expression arr[i] is interpreted as *(arr + i), which means accessing the memory location at the base address of arr plus i times the size of the element.
Click to reveal answer
What is the result of accessing arr[5] if arr has size 5?
AValid access to the last element
BAccesses the first element
CUndefined behavior (out of bounds)
DCompiler error
✗ Incorrect
Array indices in C go from 0 to size-1. Accessing index 5 in a size 5 array is out of bounds and causes undefined behavior.
Which of the following is the correct way to update the 2nd element of an array arr to 10?
Aarr[1] = 10;
Barr[2] = 10;
Carr(1) = 10;
Darr->1 = 10;
✗ Incorrect
Array indices start at 0, so the 2nd element is at index 1. The correct syntax is arr[1] = 10;
What is the time complexity to update an element at a given index in an array?
AO(1)
BO(n)
CO(log n)
DO(n^2)
✗ Incorrect
Updating an element at a specific index in an array is a direct operation and takes constant time O(1).
In C, what does the expression *(arr + i) mean?
APointer to arr plus i
BAccess the element at index i of arr
CAccess the first element of arr
DAdd i to the value of arr
✗ Incorrect
In C, arr[i] is equivalent to *(arr + i), which accesses the element at index i.
Which of these statements about arrays is FALSE?
AAccessing out-of-bounds index causes undefined behavior
BAccessing an element by index is O(1)
CArrays store elements in contiguous memory
DArrays can dynamically resize themselves automatically
✗ Incorrect
Arrays in C have fixed size and do not resize automatically. Dynamic resizing requires other data structures.
Describe how to access and update an element at a specific index in a C array.
Think about how you use arr[i] in C.
You got /5 concepts.
Explain what happens if you try to access or update an array element outside its valid index range in C.
Consider memory safety and C language behavior.
You got /4 concepts.
Practice
(1/5)
1. What does the expression arr[2] do in an array named arr?
easy
A. Accesses the element at index 0 of the array
B. Accesses the element at index 2 of the array
C. Updates the element at index 2 to zero
D. Deletes the element at index 2
Solution
Step 1: Understand array indexing
Arrays use zero-based indexing, so index 2 means the third element.
Step 2: Access element at index 2
The expression arr[2] retrieves the value stored at the third position in the array.
Final Answer:
Accesses the element at index 2 of the array -> Option B
Quick Check:
arr[2] means third element access [OK]
Hint: Remember arrays start at index 0, so arr[2] is third element [OK]
Common Mistakes:
Thinking index starts at 1 instead of 0
Confusing access with update or delete
Assuming arr[2] changes the array
2. Which of the following is the correct syntax to update the 4th element of an integer array arr to 10 in C?
easy
A. arr[3] = 10;
B. arr(4) = 10;
C. arr[4] == 10;
D. arr{3} = 10;
Solution
Step 1: Identify correct array indexing syntax
In C, arrays use square brackets with zero-based indexing, so the 4th element is at index 3.
Step 2: Use assignment operator correctly
To update, use single equals =, so arr[3] = 10; is correct.
Final Answer:
arr[3] = 10; -> Option A
Quick Check:
Use arr[index] = value; with zero-based index [OK]
Hint: Use square brackets and single = for update, zero-based index [OK]
Common Mistakes:
Using parentheses or braces instead of brackets
Using double equals (==) instead of assignment (=)
Using wrong index number for 4th element
3. What will be the output of the following C code?
Array size is 3, valid indices are 0, 1, 2. Index 3 is out of bounds.
Step 2: Understand consequences of out of bounds access
Accessing or assigning arr[3] causes undefined behavior or runtime error.
Final Answer:
Array index out of bounds error -> Option C
Quick Check:
Index 3 invalid for size 3 array [OK]
Hint: Array indices go from 0 to size-1 only [OK]
Common Mistakes:
Using index equal to array size
Ignoring zero-based indexing
Assuming no error for out of bounds
5. Given an integer array arr of size 6 initialized as {2, 4, 6, 8, 10, 12}, which code snippet correctly doubles the value at index 4 and then prints the entire array?
Index 4 is the 5th element. Doubling means multiply by 2. Both arr[4] = arr[4] * 2; and arr[4] *= 2; work, but check loop next.
Step 2: Print entire array correctly
Array size is 6, so loop must run from 0 to 5 (i<6). arr[4] = arr[4] * 2;
for(int i=0; i<6; i++) printf("%d ", arr[i]); uses correct loop range and prints all elements.