Final Exam Flashcards
(81 cards)
What is the value of numbers.size() after the following code?
vector < float > numbers;
numbers.reserve(100);
0
To declare a c-string and initialize it to the value of “phonebook”,
char s1[10]=”phonebook”;
Which assignment statements will copy the value “ toaster” into a string variable (str1)?
str1 = “toaster”;
Given the following code, what is the correct statement to insert the string str2 into str1, directly after the ‘d’?
string str1 = “abcdefg”;
string str2 = “ABCDE”;
str1.insert(4,str2);
The following code declares a vector of integers named numbers that reserves space for 100 integers.
vector < int > numbers(100);
TRUE/FALSE
true
Which of the following declarations correctly creates a c-string that can hold the value “phonebook”
char s1=10;
char s1[10];
char s1[10];
A vector v will automatically increase the allocated size when more than v.size( ) elements are inserted with v.push_back( newElement).
TRUE/FALSE
true
What is a correct statement about vector member functions studied in this chapter?
size( ) tells how many base type objects have been inserted into the vector
Using the == operator on a string variable results in the same value as using strcmp on two c-strings.
TRUE/FALSE
False
If the capacity of a vector named names is 20 and the size of names is 19, which of the following statements are legal?
names.push_back(“myName”);
names[18]=”myName”;
All of these
None of These
None of these
The base type for a vector can be
Any data type
Using the resize member function alone, you can increase the capacity of an STL vector.
TRUE/FALSE
true
Which of the following would correctly read an entire line from an input file stream named fin into a string variable named line?
getline(fin,line);
fin. getline(line);
fin. getline(line,’\n’)
fin. getline(line,80);
getline(fin, line);
If we use an out of range index with a vector, there will be an error message from the compiler.
TRUE/FALSE
False
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?
str(3);
str.at(3);
str[3];
all of these
str.at(3);
Write how to print out the value in str?
char str[30];
cin»_space; str;
for(int i=0;i<30;i++)
cout «_space;str[i];
What is the value of numbers.capacity() after the following code?
vector < float > numbers;
numbers.reserve(100);
100
Vectors can have any type as the base type.
TRUE/FALSE
True
Given the following declarations, which of the following is legal syntax?
string str=”your name”;
char c_string[20]=”my name”;
-------------- str = c_string; c_string = str; strcpy(c_string, str.c_str()); strcpy(c_string,str);
str = c_string;
and
strcpy(c_string,str.c_str());
Vector assignment is well behaved.
TRUE / FALSE
True
strcmp(first, second) returns
<0 if first < second
0 if first == second
positive otherwise.
Which is the proper way to determine how many characters are in the string variable named str?
str.length()
You can explicitly use the vector member function resize to increase the capacity of a vector.
TRUE / FALSE
True
If you want to read into a c-string, you must ensure that the user does not enter more characters than
The size of the c-string -1