Part 1
- What is an array?
An array is an aggregate data type that can be used to create and access many variables of the same data type through a single identifier. Each element of the array is assigned an index value in sequential order starting from 0 for the first element up to array length minus 1 for the last element.
- Which index would you use to access the 2nd variable in an array?
Index value 1
arrayName[1];
- What can you say about the size of an array?
The size of an array is computed by multiplying the array length with the size of one element.
If this question refers to the length of an array:
An array can have any length but in the case of a fixed array the array length must be known at compile-time and the array length cannot be changed, otherwise the program throws an error. In case of a dynamic array, the length can be determined at runtime and the array length can also be changed.
Part 2
- How can you initialize an array?
The most convenient way (the number of elements can also be omitted):
dataType arrayName[number of array elements]{valueElement1, valueElement2, …};
- How can you get the size of an array in bytes?
The “sizeof” operator can be used on arrays to return the size of the array in bytes.
sizeof(arrayName);
- How can you use the “sizeof” operator to calculate the number of elements in an array?
Dividing the size of the array by the size of one array element results into the total number of elements the array contains.
numberOfElements = sizeof(arrayName) / sizeof(arrayName[0]);
- What happens when you try to write outside the bounds of the array?
Writing outside the bounds of an array does not throw an error but it results into undefined behavior.