C++ Primer - Chapter 3 (Strings, Vectors & Arrays) Flashcards

1
Q

What is an array?

A

A container of unnamed objects of a single type that we access by position.

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

Can arrays be doubled in size?

A

No. Arrays cannot be resized at all; they have fixed size.

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

True or false? The dimension of an array is not a part of its type.

A

False. The dimension of an array is part of its type (and must be known at compile-time).

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

What is the issue with the following code?
int sz=4;
int arr[sz]={0,1,2,3};

A
int sz=4;
int arr[sz]={0,1,2,3};
// Error; the dimension of an array must be a constant expression so that it can be known at compile-time.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

If the size of the container that is required is unknown to the user, which is better to use? An array or a vector?

A

A vector, as it can be resized after it has been created.

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

Are there any issues with the following code?
int *parray[10]={};
int &rarray[10]={};

A
int *parray[10]={};
int &rarray[10]={};
// This is an error; an array can only hold objects. Thus, an array cannot hold a reference.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Fill in the blank:

Like pointers and references, arrays are a ________.

A

Like pointers and references, arrays are a compound type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
Describe the following code. Are there any errors?
const unsigned sz = 3;
int ia1[sz] = {0,1,2}; 
int a2[] = {0, 1, 2};
int a3[5] = {0, 1, 2}; 
string a4[3] = {"hi", "bye"};
int a5[2] = {0,1,2};
A
const unsigned sz = 3;
int ia1[sz] = {0,1,2}; 
// ia1 is an array of three ints with values 0, 1, 2.
int a2[] = {0, 1, 2}; 
// a2 is an array of dimension 3.
int a3[5] = {0, 1, 2}; 
// This is equivalent to writing a3[] = {0, 1, 2, 0, 0}.
string a4[3] = {"hi", "bye"}; 
// This is the same as a4[] = {"hi", "bye", ""}.
int a5[2] = {0,1,2}; 
// This is an error; too many initializers.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the difference between the two arrays below?
char a1[] = {’C’, ’+’, ’+’};
char a2[] = “C++”;

A
char a1[] = {’C’, ’+’, ’+’}; 
// This contains no null character for termination.
char a2[] = "C++"; 
// A null terminator is added automatically because a2 is created from a string.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Is it possible to copy or assign an array?

A

No. We cannot initialise an array as a copy of another array and one array cannot be assigned to another.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
What does the following code do?
int arr[10];
int *ptrs[10]; 
int &refs[10] = /* ... */; 
int (*Parray)[10] = &arr;
int (&arrRef)[10] = arr;
A
int arr[10];
int *ptrs[10]; 
// ptrs is an array of ten pointers to int.
int &refs[10] = /* ... */; 
// Error: there are no arrays of references.
int (*Parray)[10] = &arr; 
// Parray points to an array of ten ints.
int (&arrRef)[10] = arr; 
// arrRef refers to an array of ten ints.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is arry in the following code?

int *(&arry)[10] = ptrs;

A
int *(&arry)[10] = ptrs;
// arry is a reference to an array of 10 pointers to ints.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
Assuming txt_size is a function that takes no arguments and returns an int value, which of the following definitions are illegal? Explain why.
unsigned buf_size = 1024;
(a) int ia[buf_size]; 
(b) int ia[4 * 7 - 14];
(c) int ia[txt_size()]; 
(d) char st[3] = "fun";
A
unsigned buf_size = 1024;
(a) int ia[buf_size]; 
// This is an error; buf_size is not a constant expression.
(b) int ia[4 * 7 - 14];
// This is okay; 4 * 7 - 14 is a constant expression.
(c) int ia[txt_size()]; 
// This is okay if txt_size() is constexpr.
(d) char st[3] = "fun";
// This is an error; this should be an array of size 4 as string literals end with a (hidden) null character.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
What are the values in the following arrays?
std::string sa[10];
int ia[10];
int main() {
std::string sa2[10];
int ia2[10];
}
A
std::string sa[10];
// Default initialised to an array of empty strings (as it is outside a function).
int ia[10];
// Default initialised to an array of zeros (as it is outside a function).
int main() {
std::string sa2[10];
// Array of 10 strings of empty strings.
int ia2[10];
// Array of 10 ints with uninitialised entries.
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

List up to five of the drawbacks of using an array instead of a vector.

A
  1. The size of the array is fixed and must be known when defined.
  2. A character array must leave space for null character at the end.
  3. Arrays can not be copied or assigned as a whole.
  4. Array can not be constructed with the same values like the constructor of vector when defined.
  5. The size of array must be calculated (sometimes it cannot be calculated at all) instead of calling a size() method.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the problem with the following code?

auto ia1[3] = {0,1,2};

A
auto ia1[3] = {0,1,2};
// Error; we cannot use auto to deduce the type from a list of initializers.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What are two ways to loop over the entries of an array?

A
  1. With a regular for-loop (e.g. using the known size), or;

2. With a range-for (e.g. with the type auto deduced).

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

Identify the indexing errors in the following code:
constexpr size_t array_size = 10;
int ia[array_size];
for (size_t ix = 1; ix <= array_size; ++ix)
ia[ix] = ix;

A
constexpr size_t array_size = 10;
int ia[array_size];
for (size_t ix = 1; ix <= array_size; ++ix)
// Error; accesses the off-the-end element of the array. It is an error to access this entry.
ia[ix] = ix;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q
What type does ia2 have in the following code?
int ia[] = {0,1,2,3,4,5,6,7,8,9}; 
auto ia2(ia);
A
int ia[] = {0,1,2,3,4,5,6,7,8,9}; 
auto ia2(ia); 
// ia2 is an int* that points to the first element in ia.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Is there any difference in the initialisation of p and p2 below?
string nums[] = {“one”, “two”, “three”};
string *p = &nums[0];
string *p2 = nums;

A

No. There is no difference in the initialisation of p and p2. As nums is an array the compiler will automatically pass a pointer to the first element of nums.

string nums[] = {“one”, “two”, “three”};
string *p = &nums[0];
string *p2 = nums;

21
Q

Fill in the blank:

In most expressions, when we use an object of array type, we are really using a ________ to the ________ in that array.

A

In most expressions, when we use an object of array type, we are really using a pointer to the first element in that array.

22
Q
What is the type of ia2 and ia3 in the following code?
int ia[] = {0,1,2,3,4,5,6,7,8,9};
auto ia2(ia);
decltype(ia) ia3 = {0,1,2,3,4,5,6,7,8,9};
A
int ia[] = {0,1,2,3,4,5,6,7,8,9};
auto ia2(ia);
// ia2 is an int* that points to the first entry of ia.
decltype(ia) ia3 = {0,1,2,3,4,5,6,7,8,9};
// ia3 is an array, not a pointer like a2.
23
Q

Why is it possible to loop over the entries of scores with the following code and not overrun?
unsigned scores[11] = {};
for (auto i : scores)
cout &laquo_space;i &laquo_space;” “;

A

The dimension of an array is part of its type so the compiler knows how many elements are in scores.

unsigned scores[11] = {};
for (auto i : scores)
cout &laquo_space;i &laquo_space;” “;

24
Q

How can we change p so that it points to the second element of arr without accessing arr directly?
int arr[] = {0,1,2,3,4,5,6,7,8,9};
int *p = arr;

A

We can increment the position of p as shown below:
int arr[] = {0,1,2,3,4,5,6,7,8,9};
int *p = arr;
p++;

25
Q

(C++11:) What is a safe way to obtain pointers to the first and off-the-end element of an array? What library does it require?

A

The first and off-the-end element of an array can be accessed with the begin() and end() functions, respectively, in the iterator header.

26
Q

When we add or subtract an integral value from a pointer, what is the result?

A

The result is another pointer, which points to the element the given number ahead of the original pointer.

27
Q

How can the length of an array be determined using a pointer to the first and off-the-end element of the array?

A

With the following code:

auto n = end(arr) - begin(arr);

28
Q

What type does n have in the following code?

auto n = end(arr) - begin(arr);

A
auto n = end(arr) - begin(arr);
// n has type ptrdiff_t which is defined in the cstddef header.
29
Q

Is there a problem with the following code?
int i = 0, sz = 42;
int *p = &i, *e = &sz;
while (p < e)

A
int i = 0, sz = 42;
int *p = &amp;i, *e = &amp;sz;
while (p < e)
// Undefined: p and e are unrelated; therefore their comparison is meaningless!
30
Q

What is the difference between the latter two lines of the following code?
int ia[] = {0,2,4,6,8};
int last = *(ia + 4);
last = *ia + 4;

A
int ia[] = {0,2,4,6,8}; 
int last = *(ia + 4); 
// This initializes last to 8, i.e. the value of ia[4].
last = *ia + 4;
// This sets last to 4, equivalent to ia[0] + 4.
31
Q

Is it possible to use the subscript operator on an array?

A

Yes, as long as that pointer points to an element in an array.

32
Q
Will the following code work? If not, why not?
int ia={0,1,2,3,4,5};
int *p = &amp;ia[2];
int j = p[1]; 
int k = p[-2];
A
The code will work fine.
int ia={0,1,2,3,4,5};
int *p = &amp;ia[2]; 
int j = p[1]; 
// j is set to the value of p[1], which is equivalent to *(p + 1), i.e. ia[3].
int k = p[-2]; 
// This is legal; p[-2] is the same element as ia[0]. Negative indices can be used inside the subscript operator when accessing (valid) entries of the given array.
33
Q

What type does the argument of the subscript operator of an array have?

A

A signed integer type, as negative indices may be used to access entries of an array.

34
Q

Given that p1 and p2 point to elements in the same array, what does the following code do? Are there values of p1 or p2 that make this code illegal?
p1 += p2 - p1;

A
p1 += p2 - p1;
// p2-p1 computes the difference in the positions of p2 and p1 and thus shifts the position of p1 to the position of p2.
// This is legal as long as p1 and p2 point to entries of the same array (as stated).
35
Q

Fill in the blanks:

Character string literals are an instance of a more general construct that C++ inherits from C: _________________.

A

Character string literals are an instance of a more general construct that C++ inherits from C: C-style character strings.

36
Q

Fill in the blanks:
______________ are not a type. Instead, they are a convention for how to represent and use character strings. Strings that follow this convention are stored in ______________ and are ____ terminated.

A

C-style strings are not a type. Instead, they are a convention for how to represent and use character strings. Strings that follow this convention are stored in character arrays and are null terminated.

37
Q

Is there a problem with the following code?
char ca[] = {’C’, ’+’, ’+’};
std::cout &laquo_space;strlen(ca) &laquo_space;std::endl;

A
char ca[] = {’C’, ’+’, ’+’}; 
// ca is not null terminated.
cout << strlen(ca) << endl; 
// This is an error; ca isn’t null terminated. The code will continue searching memory until a null character is found.
38
Q

The header cstring allows the contents of two character arrays to be concatenated using the “strcat” function. What must we provide? What is a common issue with the use of this function?

A

The strcat function requires a character array large enough to store the result of the concatenation. A common issue with this is not allocating enough space for the output string.

39
Q
What does the following program do?
const char ca[] = {’h’, ’e’, ’l’, ’l’, ’o’};
const char *cp = ca;
while (*cp) {
cout << *cp << endl;
\++cp;
}
A

const char ca[] = {’h’, ’e’, ’l’, ’l’, ’o’};
const char cp = ca;
while (
cp) {
cout &laquo_space;*cp &laquo_space;endl;
++cp;
}
// Since there is no null character at the end, the program will print all the characters in ca and continue to print whatever is in memory until it finds a null character.

40
Q

Why would adding two pointers be meaningless?

A

Pointers contain addresses. Adding two addresses makes no sense because you have no idea what you would point to.

41
Q

How can a vector be initialised from the following array?

int arr[] = {0, 1, 2, 3, 4, 5};

A

With the following code:
int arr[] = {0, 1, 2, 3, 4, 5};
std::vector ivec(begin(arr), end(arr));
// ivec will copy all of the entries of arr and ignores the off-the-end element pointed to by end(arr).

42
Q

Suppose an array arr of length 10 is given. How can entries 3 to 8 be copied directly into a vector?

A

Entries 3 to 8 of arr can be copied directly into a vector with the following code:
std::vector vec(arr+3, arr+9);
(Note the position of the “end”.)

43
Q

What does the following typedef do?

typedef int int_array[4];

A
typedef int int_array[4];
// Creates an alias called "int_array" for an array of 4 ints.
44
Q

What is a C-style string?

A

A null-terminated character array. String literals are C-style strings.

45
Q

What is a buffer overflow?

A

A programming bug that results when we use an index that is out-of-range for a container, such as a string, vector, or an array.

46
Q

What is copy initialisation?

A

Form of initialisation that uses an =. The newly created object is a copy of the given initialiser.

47
Q

What is direct initialisation?

A

A form of initialisation that does not include an =.

48
Q

What is value initialisation?

A

Initialisation in which built-in types are initialised to zero and class types are initialised by the class’s default constructor.

49
Q

When can objects of a class be value initialised?

A

When the class has a default constructor.