Chapter 4: Arrays, Dynamic Arrays, and Vectors Flashcards

1
Q

Given the following code, what are the legal indexes for the array named “arr”.

int arr[4] = {2, 4, 6, 8, 10 };

A

0, 1, 2, 3, 4

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

Which of the following correctly initializes an array of int type named “arr” to contain four elements: 1, 2, 3, and 4.

I: int arr[4] = { 1, 2, 3, 4 };
II: int arr[4]; arr[1]=1; arr[2]=2; arr[3]=3; arr[4]=4;
III: int arr[4]; for (int i=0; i<4; i++) { arr[i]=i; }
IV: int arr[4]; int i=0 while(i<4) { arr[i]=i+1; i++; }

A

I and IV

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

Given the following code, what is the maximum number of elements to be populated to the “arr” array?

for (int i=0; i<n; i++)
{
arr[i] = i+1;
}

A

n

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

Given the following 2D array, the value of x[2][2] is __.

int x[3][4] = {{0,1,2,3}, {4,5,6,7}, {8,9,10,11}};

A

10

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

Which can find the size of an array named “arr” and then assign the value to a variable named “size”?

A

int size = sizeof(arr) / sizeof(arr[0])

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

Given the following code, which can deallocate the memory of “arr”?

int *arr = new int[5];

A

delete [] arr;

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

Given the following code, which can create an array of int type which can have up to 5 elements?

int size = 5;

A

int *a = new int[size];

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

Which is the advantage(s) of Vectors compared to arrays in C++?

I. Vectors do not have to declare size
II. Vectors can report their size and capacity
III. Vectors can grow in size
IV. Vectors do not take memory space

A

I, II, and III

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

Which declares a vector named “v” of 5 integers in C++?

A

vector<int> v (5);</int>

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

Which statement is correct about the “push_back()” function?

A

It adds a new element at the end.

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

What is an array in C++?

A

A fixed-size collection of elements of the same data type.

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

What can declare a dynamic array of integers named data with an initial size of 10?

A

int* data = new int[10];

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

By using new int[n] to create a dynamic array , what is the valid range of indices (indexes) you can use to access elements without causing undefined behavior?

A

0 to n-1

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

How is an array index typically defined?

A

A non-negative integer starting from 0.

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

What happens if you try to access an array element with an index that is out of bounds?

A

Undefined behavior, which can lead to crashes or incorrect results.

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

How do you declare an array of 10 integers named “numbers” in C++?

A

int numbers[10];

17
Q

Which can deallocate the memory allocated to a dynamic array named “arr” in C++?

A

delete[] arr;

18
Q

What is a dynamic array in C++?

A

A data structure that can change its size

19
Q

Which can return the number of elements in a C++ array named “arr”?

A

sizeof(arr) / sizeof(arr[0])

20
Q

Which header file must you include to use the std::vector class?

A

<vector>
</vector>