Recall & Review
beginner
What does the size of an array represent in C++?
The size of an array represents the total number of elements it can hold. For example,
int arr[5]; means the array can store 5 integers.Click to reveal answer
beginner
What happens if you try to access an array element outside its bounds in C++?
Accessing an element outside the array bounds leads to undefined behavior. This means the program might crash, show wrong data, or behave unpredictably.
Click to reveal answer
intermediate
How do you find the size of a statically declared array in C++?
You can find the number of elements by dividing the total size of the array by the size of one element:
int size = sizeof(arr) / sizeof(arr[0]);Click to reveal answer
beginner
Why is it important to stay within array bounds when accessing elements?
Staying within bounds prevents errors and crashes. It ensures you only access valid memory locations, keeping your program safe and stable.
Click to reveal answer
intermediate
What is the difference between array size and array bounds?
Array size is how many elements the array can hold. Array bounds are the valid index range, usually from 0 to size-1. Accessing outside bounds is unsafe.
Click to reveal answer
If you declare
int arr[10];, what is the highest valid index you can use?✗ Incorrect
Array indices start at 0, so the highest valid index is size - 1, which is 9.
What does
sizeof(arr) return for int arr[5]; if an int is 4 bytes?✗ Incorrect
sizeof(arr) returns total bytes. 5 elements × 4 bytes = 20 bytes.What is the result of accessing
arr[10] if int arr[10];?✗ Incorrect
Index 10 is outside bounds (0-9), so accessing it causes undefined behavior.
How can you safely find the number of elements in a static array?
✗ Incorrect
Only
sizeof(arr) / sizeof(arr[0]) works for static arrays in C++.Why should you avoid accessing array elements beyond their declared size?
✗ Incorrect
Accessing out-of-bounds elements can cause unpredictable program behavior or crashes.
Explain what array size and array bounds mean in C++ and why they matter.
Think about how many elements an array can hold and which indexes are safe to use.
You got /3 concepts.
Describe how to calculate the number of elements in a static array using sizeof in C++.
Remember sizeof(arr) gives total bytes, and sizeof(arr[0]) gives bytes per element.
You got /3 concepts.