Member-only story

C++ Arrays Practice Questions

Marika Lam
2 min readMay 15, 2022

--

1. What is an array?

An array is a series of elements of the same type placed in contigous memory locations that can be referenced by adding an index to a unique identifier.

2. How do you initialize a c++ array?

type name [elements]

type: a valid type such as int or float

name: valid identifier

elements: always enclosed in square brackets [ ] and specifies the length of the array in terms of the number of elements

int foo [5];

This means the foo array has five elements of type int

3. What type of expression is in the square brackets?

It is a constant expression, since arrays are blocks of static memory whose size must be determined at compile time, before the program runs.

4. How do you initialize specific values to the array when it is declared?

int foo [5] = { 16, 2, 77, 40, 12071 };

The number of values between the braces { } contains exactly 5 values. If declared with less, the remaining elements are set to their default values. For fundamental times, the default values are zeroes.

5. Can the initalizer have no values?

--

--

No responses yet