Deck 12: More on C-Strings and the String Class

Full screen (f)
exit full mode
Question
The string class member function ________ returns the C-string value of the string object.

A) c_string(string s)
B) c_str(string s)
C) c_str()
D) c_string()
E) None of the above
Use Space or
up arrow
down arrow
to flip the card.
Question
It is possible to implement a string class by using a dynamically allocated array of characters.
Question
To declare a C-string, you should use the type expression string *.
Question
A library function that looks for the occurrence of one string inside another is

A)the stand-alone function strcmp(char *s, char *t).
B) the string class member function find(string s, int pos).
C) strfind.
D) strsearch.
E) None of the above
Question
The string class overloads the += operator to perform

A) conversion to integers followed by addition.
B) concatenation of two strings.
C) calculation of the sum of the integer ASCII codes that comprise the characters in the string.
D) calculation of the sum of the lengths of two strings.
E) None of the above
Question
The expression strcmp("ab", "cde") returns

A) the boolean value false.
B) the boolean value True.
C) a negative integer.
D) a nonnegative integer.
E) None of the above
Question
The ________ function accepts a C-string as an argument and returns the length of the string (not including the null terminator).

A) numchar
B) strlength
C) strlen
D) countstring
E) None of the above
Question
The string class append member function str.append(s)tacks a string s to the end of str.
Question
Assume that str1 and str2 are variables of type C-string. The proper expression to use when comparing them for equality is

A) str1 = str2.
B) str1 == str2.
C) strcmp(str1, str2) == 0.
D) str1.compare(str2).
E) None of the above
Question
When using the strcat function, you must be careful not to overflow the bounds of the array allocated for the target string.
Question
An array of characters ending with the null terminator is called

A) a C-string.
B) a C++ String.
C) a string literal.
D) a string class object.
E) None of the above
Question
A good way to convert a value of type double to a string is

A) use the ostringstream class.
B) use the strtodouble function.
C) use the doubletoa function.
D) use the doublestringstream class.
E) None of the above
Question
Which statements convert the string "10" to the integer value 10?

A) istringstream istr("10"); int x; istr >> x;
B) ostringstream ostr("10"); int x; ostr >> x;
C) int x = stoi("10");
D) int x = str("10"):
E) None of the above
Question
The C++ compiler performs strict array bounds checking whenever an array of characters is being accessed.
Question
The ________ function converts an integer to a string and returns the string value.

A) string stoint(int i)
B) string strtoint(int i)
C) string strint(int i)
D) string to_string(int i)
E) None of the above
Question
Assume that str1 and str2 are objects of the string class. The proper expression to use when comparing them for equality is

A) str1 = str2.
B) str1 == str2.
C) strcmp(str1, str2).
D) str1.compare(str2).
E) None of the above
Question
By being able to pass arrays as arguments, you can write your own functions for processing C-strings.
Question
The null terminator stands for the ASCII code

A) 57.
B) 100.
C) 1000.
D) 0.
E) None of the above
Question
The strcpy function's arguments are

A) two string class objects.
B) two pointers to char.
C) three pointers.
D) three arrays and one pointer.
E) None of the above
Question
The string class member function ________ will return the number of characters in the string object.

A) length()
B) length(string s)
C) capacity()
D) capacity(string s)
E) None of the above
Question
To use the strlen function in a program, you must #include

A) .
B) .
C) .
D) .
E) None of the above
Question
The type of the literal string "Hello" is best described as

A) *string.
B) string *.
C) char *.
D) *char[].
E) None of the above
Question
To account for the null terminator stored at the end of each C-string, the strlen function returns the number of characters in its argument, plus one.
Question
The >> operator can be used to convert a value of type double to an equivalent string object stored in memory.
Question
The ________ function concatenates the contents of one C-string with another C-string.

A) strcopy
B) strappend
C) strcat
D) stradd
E) None of the above
Question
If str is a string class object, the member function str.length() returns

A) the number of characters that make up the str string object.
B) the same value as the member function str.size().
C) the number of integer variables that make up the str object.
D) Both A and B are correct.
E) Both A and C are correct.
Question
To access the last character of a string class object str you can use the expression

A) str[last].
B) str.last().
C) str[str.length()].
D) str[str.length()-1].
E) None of the above
Question
There exist C++ stream classes that write and read in-memory strings.
Question
A C-string can be assigned to a variable whose type is the string class.
Question
To declare an array that will store students' last names of up to 25 characters in length, which is an appropriate statement?

A) char lastName[25];
B) string lastName[25];
C) char lastName[26];
D) string lastName[24];
E) None of the above
Question
After num = stoi("1000"); executes, what value is stored in the variable num?

A) 1000
B) 999 (1000 minus 1 for the null terminator)
C) "1000"
D) "thousand"
E) None of the above
Question
A good way to convert a string such a "572" to an equivalent integer such as 572

A) is to use the C-library function itoa.
B) is to use an ostringstream object and the insertion operator <<.
C) is to use an istringstream object and the extraction operator >>.
D) is to use an ostringstream object and the extraction operator >>.
E) None of the above
Question
The ________ library function reverses the order of a character array.

A) reverstr
B) strrev
C) reversit
D) backward
E) None of the above
Question
To use the ostringstream class, you must include the memstringstream header file.
Question
A C-string is a sequence of characters stored in consecutive memory, terminated by a

A) period.
B) space.
C) null character.
D) semicolon.
E) None of the above
Question
The code int x = 0; while (s[x] != 0) { x++;}

A) sets the variable x to point to the last character in the C-string s.
B) computes the length of the C-string s into the variable x.
C) sets the variable x to the ASCII code of the last character in the C-string s.
D) sets the value of the variable x to 0.
E) None of the above
Question
The proper expression to compare the first characters of two string objects str1 and str2 is

A) str1[0] = str2[0].
B) str1[0] == str2[0].
C) strcmp(str1[0], str2[0]).
D) 'str1[0]' == 'str2[0]'.
E) None of the above
Question
The expression str1 < str 2 is True when

A) the string object str1 precedes the string str 2 in alphabetic order.
B) the string str1 converted to numeric form is less than the string str2 converted to numeric form.
C) the length of the string str1 is less than the length of the string str2.
D) the C-string "str1" precedes the C-string "str2" in alphabetic order.
E) None of the above
Question
Although C++ provides ample library functions to handle numeric values, we must write all of our own functions to manipulate character values.
Question
The following statement declares a string object and initializes it to "Once upon a time".
string theString("Once upon a time");
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/40
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 12: More on C-Strings and the String Class
1
The string class member function ________ returns the C-string value of the string object.

A) c_string(string s)
B) c_str(string s)
C) c_str()
D) c_string()
E) None of the above
C
2
It is possible to implement a string class by using a dynamically allocated array of characters.
True
3
To declare a C-string, you should use the type expression string *.
False
4
A library function that looks for the occurrence of one string inside another is

A)the stand-alone function strcmp(char *s, char *t).
B) the string class member function find(string s, int pos).
C) strfind.
D) strsearch.
E) None of the above
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
5
The string class overloads the += operator to perform

A) conversion to integers followed by addition.
B) concatenation of two strings.
C) calculation of the sum of the integer ASCII codes that comprise the characters in the string.
D) calculation of the sum of the lengths of two strings.
E) None of the above
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
6
The expression strcmp("ab", "cde") returns

A) the boolean value false.
B) the boolean value True.
C) a negative integer.
D) a nonnegative integer.
E) None of the above
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
7
The ________ function accepts a C-string as an argument and returns the length of the string (not including the null terminator).

A) numchar
B) strlength
C) strlen
D) countstring
E) None of the above
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
8
The string class append member function str.append(s)tacks a string s to the end of str.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
9
Assume that str1 and str2 are variables of type C-string. The proper expression to use when comparing them for equality is

A) str1 = str2.
B) str1 == str2.
C) strcmp(str1, str2) == 0.
D) str1.compare(str2).
E) None of the above
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
10
When using the strcat function, you must be careful not to overflow the bounds of the array allocated for the target string.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
11
An array of characters ending with the null terminator is called

A) a C-string.
B) a C++ String.
C) a string literal.
D) a string class object.
E) None of the above
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
12
A good way to convert a value of type double to a string is

A) use the ostringstream class.
B) use the strtodouble function.
C) use the doubletoa function.
D) use the doublestringstream class.
E) None of the above
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
13
Which statements convert the string "10" to the integer value 10?

A) istringstream istr("10"); int x; istr >> x;
B) ostringstream ostr("10"); int x; ostr >> x;
C) int x = stoi("10");
D) int x = str("10"):
E) None of the above
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
14
The C++ compiler performs strict array bounds checking whenever an array of characters is being accessed.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
15
The ________ function converts an integer to a string and returns the string value.

A) string stoint(int i)
B) string strtoint(int i)
C) string strint(int i)
D) string to_string(int i)
E) None of the above
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
16
Assume that str1 and str2 are objects of the string class. The proper expression to use when comparing them for equality is

A) str1 = str2.
B) str1 == str2.
C) strcmp(str1, str2).
D) str1.compare(str2).
E) None of the above
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
17
By being able to pass arrays as arguments, you can write your own functions for processing C-strings.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
18
The null terminator stands for the ASCII code

A) 57.
B) 100.
C) 1000.
D) 0.
E) None of the above
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
19
The strcpy function's arguments are

A) two string class objects.
B) two pointers to char.
C) three pointers.
D) three arrays and one pointer.
E) None of the above
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
20
The string class member function ________ will return the number of characters in the string object.

A) length()
B) length(string s)
C) capacity()
D) capacity(string s)
E) None of the above
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
21
To use the strlen function in a program, you must #include

A) .
B) .
C) .
D) .
E) None of the above
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
22
The type of the literal string "Hello" is best described as

A) *string.
B) string *.
C) char *.
D) *char[].
E) None of the above
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
23
To account for the null terminator stored at the end of each C-string, the strlen function returns the number of characters in its argument, plus one.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
24
The >> operator can be used to convert a value of type double to an equivalent string object stored in memory.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
25
The ________ function concatenates the contents of one C-string with another C-string.

A) strcopy
B) strappend
C) strcat
D) stradd
E) None of the above
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
26
If str is a string class object, the member function str.length() returns

A) the number of characters that make up the str string object.
B) the same value as the member function str.size().
C) the number of integer variables that make up the str object.
D) Both A and B are correct.
E) Both A and C are correct.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
27
To access the last character of a string class object str you can use the expression

A) str[last].
B) str.last().
C) str[str.length()].
D) str[str.length()-1].
E) None of the above
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
28
There exist C++ stream classes that write and read in-memory strings.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
29
A C-string can be assigned to a variable whose type is the string class.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
30
To declare an array that will store students' last names of up to 25 characters in length, which is an appropriate statement?

A) char lastName[25];
B) string lastName[25];
C) char lastName[26];
D) string lastName[24];
E) None of the above
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
31
After num = stoi("1000"); executes, what value is stored in the variable num?

A) 1000
B) 999 (1000 minus 1 for the null terminator)
C) "1000"
D) "thousand"
E) None of the above
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
32
A good way to convert a string such a "572" to an equivalent integer such as 572

A) is to use the C-library function itoa.
B) is to use an ostringstream object and the insertion operator <<.
C) is to use an istringstream object and the extraction operator >>.
D) is to use an ostringstream object and the extraction operator >>.
E) None of the above
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
33
The ________ library function reverses the order of a character array.

A) reverstr
B) strrev
C) reversit
D) backward
E) None of the above
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
34
To use the ostringstream class, you must include the memstringstream header file.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
35
A C-string is a sequence of characters stored in consecutive memory, terminated by a

A) period.
B) space.
C) null character.
D) semicolon.
E) None of the above
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
36
The code int x = 0; while (s[x] != 0) { x++;}

A) sets the variable x to point to the last character in the C-string s.
B) computes the length of the C-string s into the variable x.
C) sets the variable x to the ASCII code of the last character in the C-string s.
D) sets the value of the variable x to 0.
E) None of the above
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
37
The proper expression to compare the first characters of two string objects str1 and str2 is

A) str1[0] = str2[0].
B) str1[0] == str2[0].
C) strcmp(str1[0], str2[0]).
D) 'str1[0]' == 'str2[0]'.
E) None of the above
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
38
The expression str1 < str 2 is True when

A) the string object str1 precedes the string str 2 in alphabetic order.
B) the string str1 converted to numeric form is less than the string str2 converted to numeric form.
C) the length of the string str1 is less than the length of the string str2.
D) the C-string "str1" precedes the C-string "str2" in alphabetic order.
E) None of the above
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
39
Although C++ provides ample library functions to handle numeric values, we must write all of our own functions to manipulate character values.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
40
The following statement declares a string object and initializes it to "Once upon a time".
string theString("Once upon a time");
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 40 flashcards in this deck.