ScA: Q&A

1. What is a built-in array in C++?
A built-in array is a sequence of elements of the same data type stored in contiguous memory. All elements are accessed using an index that starts from 0.


2. How do you declare an array that holds 7 integers?

int week[7];

This creates 7 integer slots, where you can access elements using week[0] through week[6].


3. What happens if you access an element outside of an array’s bounds (e.g., week[7])?
This causes undefined behavior — it might crash the program or return garbage data. C++ does not prevent out-of-bounds access for built-in arrays.


4. What are the ways to initialize an array in C++?

int nums[3] = {1, 2, 3};      // With assignment operator
int nums2[3]{1, 2, 3};        // Brace-init (C++11+)
int nums3[]{1, 2, 3};         // No size, compiler infers size
int nums4[5] = {1, 2};        // Remaining values set to 0
int nums5[5] = {0};           // All values set to 0

5. How do you loop through an array and print its contents?

const int SIZE = 5;
int nums[SIZE] = {1, 2, 3, 4, 5};

for (int i = 0; i < SIZE; i++) {
    cout << nums[i] << " ";
}

This prints: 1 2 3 4 5


6. Can you use .size() on a built-in array? Why or why not?
No. Built-in arrays don’t have methods like .size(). You must track the size separately, often using a const int or #define.


7. What does it mean when an array is passed to a function?
It means that a pointer to the first element is passed, not the entire array. This is called array decay.


8. Why do you usually pass the size of the array along with it to a function?
Because once passed, the array becomes just a pointer, and the function doesn’t know how many elements are in it. You must explicitly pass the size.


9. Can you compare two arrays using ==? Why or why not?
No. == compares the memory addresses, not the values inside the arrays. To compare values, loop through each index manually.


10. How do you copy one array to another?

for (int i = 0; i < size; i++) {
    dest[i] = source[i];
}

You must copy each element individually using a loop, since = doesn’t work for whole arrays.


Would you like the next 10 Q&A? Just say YES and I’ll continue with questions 11–20.