C++ Deck 7 Flashcards

1
Q

What is a composite variable?

A

Variables that are capable of storing multiple values under the same name with some way to tell them apart.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the simplest form of composite data type?

A

An array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is an array?

A

Data structures that allow the storage of multiple elements of the same type in contiguous memory locations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does it mean that an array is indexed?

A

The elements contained in the array are numbered (starting at 0)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

True or False: Every element in an array must be the same data type.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the formula for calculating the memory address of an index in an array?

A

Address = starting address + (size of element type * index number)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Which of the following is NOT a property of an array:

a. Same Data Type
b. Continuous in Memory
c. Indexed (Beginning at Zero)
d. Variable size upon declaration

A

d. Variable size upon declaration

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the index range of an array?

A

0 to size-1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the syntax of declaring an array?

A

typeName variableName[size];

The size in the brackets must be an integer literal or a constant variable. The compiler uses the size to determine how much space to allocate (i.e. how many bytes)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

True or False: When initializing an array using set notation, it is permissible to exceed the number of values allocated. C++ will adjust the size.

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

A c-style string is implemented as an array of type char that ends with what?

A

The null character

char name[7] = “Connie”;
(note that it’s 6 characters long, but room has been left for the null character to be the 7th index. This is REQUIRED.)

It’s the equivalent of:

char name[7] = {‘C’, ‘o’, ‘n’,’n’,’i’,’e’,’\0’};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Is this a valid way to declare and initialize an array:

int list[] = {1, 3, 5, 7, 9};

A

Yes, C++ will make the array size just large enough to capture the initialized data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Which of these is legal initialization of an array:

int list[5] = {1, 2};

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

A

int nums[3] = {1, 2, 3, 4}; is illegal because the number of elements being initialized (4) is greater than the size (3).

int list[5] = {1,2}; has fewer elements initialized than the size, but the uninitialized elements are simply defaulted to 0: {1, 2, 0, 0, 0}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

True or False: You can initialize an array with a for loop, and this is a common way to do so.

A

True.

int numList[10];

int i;

for (i=0; i < 10; i++)
numList[i] = i*2;

The above loop initializes numList to {0, 2, 4, 6, 8, 10, 12, 14, 16, 18}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

In this array:

int numList[10];

How would one assign a value of 6 to the 8th element of the array?

A

numList[7] = 6;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

True or False: An out of bounds reference to an array is syntactically legal.

A

True.
However, you shouldn’t do this as it will result in run-time errors.

17
Q

True or False:

int list1 = list2;

This will copy the contents of the array list1 into array list2 if they are the same size.

A

False.
You must copy array contents element-by-element. This is called a DEEP COPY.
A very simple for loop can accomplish this:

for (int i = 0; i < 5; i++)
list1[i] = list2[i];

18
Q

Is it common to pass the array size explicitly as an additional parameter in a function?

A

Yes.
The reason is because arrays lose their size information when passed to functions, so having a size parameter in the function helps to make sure the array performs operations accordingly within the function.

You can also use a sentinel value, such as ‘\0’ for character arrays, to mark the end of the array.

19
Q

Does C++ allow direct return of an entire array from a function?

A

No.
You’ll have to use pointers to do so indirectly (covered later).

20
Q

True or False: It is possible to call functions with array values as parameters.

A

True.

21
Q

What is the formula for calculating the address of a particular row and column intersection in a 2D matrix array?

A

address = StartingAddress + ((row_number * number_of_columns) + column_number) * type_size

22
Q

If you try to print out an array using only its name, what will happen?

A

It will print out the memory address of the starting address of the array (i.e. 0 for a 1D array or 0,0 for a 2D array.)

23
Q
A