Ch.8 Flashcards

1
Q

What is the value of numbers.capacity( ) after the following code?

vector < float > numbers;
numbers.reserve(100)

A

0

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

When a vector is assigned to another vector

A

all the values in the vector are copied.

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

What is the value of str after the following code?
string str;

A

the empty string

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

Which is the proper way to determine how many characters are in the string variable named str?

A

str.length( )

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

What is the difference between strcmp and strncmp?

A

They both compare, one expects an integer for the number of characters to compare.

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

What is the value of numbers.size( ) after the following code?

vector < float > numbers;
numbers.reserve(100)

A

0

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

Which of the following will print out the value in str?
char str[30];
cin >> str;

A

cout << str;

and

int i = 0;
while(i < 30 && str[i] != ‘\0’)
cout << str[i];

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

How can you assign the value “toaster” to a c-string name str of size 10?

A

strcpy(str,”toaster”);

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

strcmp(first, second) returns

A

< 0 if first < second, 0 if first == second, positive otherwise.

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

What is wrong with the following attempted c-string declaration and initialization?
char str1[5] = {‘a’, ‘b’, ‘c’};

A

The values do not constitute a c-string.

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

Which of the following returns the fourth character in the string variable named str and checks if there is a fourth character in the string?

A

str.at(3);

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

What is wrong with the following code fragment?
char str1[10] = “Mark”, str2[15] = “What’s my name”;
strcpy(str1,str2);

A

str1 does not have enough room.

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

When the extraction operator is used to read data into a string,

A

it skips all white spaces.

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

Which of the following declarations correctly creates a c-string that can hold the value “phonebook”?

A) char s1[9]; B) char s1;
C) char s1 = 10; D) char s1[10];

A

char s1[10];

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

The base type for a vector can be

A

any data type.

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

Which assignment statements will copy the value “ toaster” into a string variable (str1)?

A

str1 = “toaster”;

17
Q

What is the value of numbers.size( ) after the following code?

vector < float > numbers(100);

A

100

18
Q

What is the proper way to declare a vector of strings named names?

A

vector<string> names;</string>

19
Q

To add an element to a vector of integers named numbers at the next available position in the vector, you would use ________.

A

numbers.push_back(newValue);

20
Q

Which of the following would correctly read an entire line from an input file stream named fin into a string variable named line?

A

getline(fin, line);

21
Q

The character ‘\0’ is called the ________ character

A

null

22
Q

The function used to “put two c-strings together into one” is called ________.

A

strcat

23
Q

All c-strings must be terminated by ________.

A

‘\0’

24
Q

What is the code to print out the third character in a string variable named str?

A

cout << str[2];

25
Q

Which string function returns the first occurrence of str1 in a string named str?

A

find

26
Q

What is the c-string function to determine the number of characters in a c-string?

A

strlen

27
Q

The c-string to number conversion functions are in the ________ library

A

cstdlib

28
Q

What is the difference between strcat and strncat?

A

strncat will concatenate at most n letters (where n has an appropriate value).

29
Q

The ________ class lets you treat string values and variables like other pre-defined data types (such as int).

A

string

30
Q

To use the functions for manipulating and comparing c-strings, you must include ________.

A

<cstring></cstring>

31
Q

To compare two c-strings you use the ________ function

A

strcmp or strncmp

32
Q

Using the == operator on a string variable results in the same value as using strcmp on two c-strings.

A

False

33
Q

Vectors and arrays are the same data type.

A

False

34
Q

The following declares a c-string variable that will hold 10 letters.
char str[10];

A

False

35
Q

Using the [i] on a string variable does not check for illegal values of i.

A

True

36
Q

A string variable and a c-string are the same data type.

A

False

37
Q

The following code declares a vector of integers named numbers that reserves space for 100 integers.
vector < int > numbers(100);

A

True