Name:
Class:
Date:
.
Page 1
Indicate whether the statement is true or false.
1. By default, the getline function will continue to read characters from the keyboard until it encounters the ‘# character.
a.
True
b.
False
2. The first character in a string has a subscript of 1.
a.
True
b.
False
3. A string is equivalent to a one-dimensional array of characters.
a.
True
b.
False
4. When the getline function encounters the delimiter character in the input, the character is added to the string and the
string is assigned to the variable.
a.
True
b.
False
5. The code cin.ignore() reads and consumes a series of characters until the delimiter character is reached.
a.
True
b.
False
6. The substr function returns a subscript indicating where a substring starts in the specified string.
a.
True
b.
False
7. You can use the string class’s delete function to remove characters from a string variable.
a.
True
b.
False
8. An escape sequence is the combination of the backslash and the character that follows it.
a.
True
b.
False
9. The string data type is one of the fundamental C++ data types.
a.
True
b.
False
10. You can use the string class’s search function to search the contents of a string variable.
a.
True
b.
False
11. The replace function substitutes a sequence of characters in a string variable with another sequence of characters.
a.
True
b.
False
Name:
Class:
Date:
.
Page 2
12. For a program to use the string class, it must contain the #include <string> directive.
a.
True
b.
False
Indicate the answer choice that best completes the statement or answers the question.
13. What function can be used to get string input from the keyboard?
a.
extraction
b.
getline
c.
ignore
d.
substr
14. Which string class function can be used to replace a sequence of characters in a string variable with another sequence
of characters.
a.
swap
b.
replace
c.
substr
d.
find
15. What is assigned to newStr after the following code is executed?
string orgStr = “Hello there!”;
string newStr = orgStr.substr(1, orgStr.length()1);
a.
Hello there
b.
ello there
c.
Hello there!
d.
ello there!
16. In order to use the string data type in a program, what code must be in your program?
a.
a #include <iostream> directive
b.
a using <iostream> statement
c.
a #include <string> directive
d.
a using <string> statement
17. What will the value of newStr be after the following code is executed?
string newStr = “13 Mockingbird Lane”;
newStr.insert(1, “31”);
a.
3113 Mockingbird Lane
b.
31
c.
1313 Mockingbird Lane
d.
131
18. Which of the following statements gets a string from the keyboard until the newline character is encountered and
assigns the string to a variable named myStr?
a.
getline(cin, myStr, ‘/n’);
b.
getline(myStr, cin);
c.
getline(myStr, cin, ‘\n’);
d.
getline(cin, myStr);
19. To remove one or more characters located anywhere in a string variable, which string class function would be used?
a.
remove
b.
delete
c.
truncate
d.
erase
20. Which of the following is the escape sequence that represents the newline character?
a.
‘\endl’
b.
‘/t’
c.
‘\n’
d.
‘\t’
Name:
Class:
Date:
.
Page 3
21. What is the name of the operator that can be used to get string input from the keyboard?
a.
insertion
b.
extraction
c.
modulus
d.
ternary
22. Which statement gets a string from the keyboard and stores it into the answer variable?
a.
cin >> answer;
b.
cin << answer;
c.
get answer;
d.
answer >> cin;
23. To duplicate a single character a specified number of times and then assign the resulting string to a string variable, you
would use which of the following functions?
a.
dup
b.
duplication
c.
replicate
d.
assign
24. What value will be assigned to resultStr when the following code is executed?
string newStr = “Hello there.”;
resultStr = newStr.substr(1, 6);
a.
Hello
b.
Hello t
c.
ello t
d.
ello th
25. Which statement would be used to declare and initialize a string variable named zipCode?
a.
string zipCode = “”;
b.
zipCode = “”;
c.
init zipCode = “”;
d.
declare zipCode = “”;
26. Which statement reads and consumes up to four characters until the tab character is encountered?
a.
cin.consume(4, ‘/t’);
b.
cin.discard(4, ‘\t’);
c.
cin.ignore(4, ‘\t’);
d.
cin.discard(‘\t’, 4);
27. Which of the following terms best describe the process of connecting (or linking) strings together?
a.
string meshing
b.
string constructing
c.
string linkage
d.
string concatenation
28. The string class provides which function to determine the number of characters contained in a string variable?
a.
numchars
b.
length
c.
total
d.
chars
29. What value will be returned by the myStr.length() statement in the code below?
string myStr = “Hello there”;
int strlen = myStr.length();
a.
11
b.
10
c.
13
d.
12
30. What is the value of myStr when the following code is executed?
string myStr = ABCdefghiJK”;
Name:
Class:
Date:
.
Page 4
myStr.replace(4, 5, “12345”);
a.
ABC12345iJK
b.
ABCd12345JK
c.
ABC1234hijk
d.
ABCd1234hiJK
31. The getline function will continue to read the characters entered at the keyboard until it encounters which character?
a.
delimiter
b.
delineator
c.
pause
d.
initializer
32. What function allows you to access a series of characters contained in a string variable?
a.
remove
b.
getline
c.
substr
d.
partial
33. Which statement will place 5 dollar sign ($) characters in the string dollarStr?
a.
dollarStr = (‘$’, 5);
b.
dollarStr.set(5, ‘$’);
c.
dollarStr.assign(5, ‘$’);
d.
dollarStr = string(‘$’, 5);
34. What character is used as the concatenation operator in C++?
a.
&
b.
&&
c.
||
d.
+
35. When using the ignore function, if you omit the numberOfCharacters argument, what is the default number of
characters to consume?
a.
0
b.
1
c.
up to the delimiter
d.
all of them
36. The functions contained within the string class are collectively known as what type of functions?
a.
member
b.
populated
c.
value-returning
d.
void
37. Which of the following is NOT one of the fundamental data types in C++.
a.
int
b.
char
c.
float
d.
string
38. Which string class function can be used to search the contents of a string variable to determine whether it contains a
specific sequence of characters?
a.
locate
b.
search
c.
examine
d.
find
39. Write the statement to declare and initialize a string varable called person to contain your full name.
40. Given the declarations below, using the find function, write the statements to search the number string variable to
determine if the sequence 456” exists. The location should be placed in the position variable. What will be the value of
the position variable when the statement is executed?
int position = 0;
string number = “1234567890”;
Name:
Class:
Date:
.
Page 5
41. Write the statement to determine the length of the name string defined below and place the value into the number
variable.
int number = 0;
string name = “Hello”;
42. Given the declarations below, using the substr function, write the statements to assign “First” to the name1 variable
and “Last” to the name2 variable.
string name = “First Last”; // note the space between First and Last
string name1 = “”;
string name2 = “”;
43. Given the declarations below, using the erase function, write the statements to remove all of the characters from the
name string, except for “Last”.
string name = “First Last”; // note the space between First and Last
44. Given the following, write the statement to store the characters entered by the user, up until the newline character, in
the name variable; and consume the newline character.
string name = “”;
cout << “Enter your name: “;
45. Write the statement to read and consume characters stored in the cin object until either 25 characters are consumed or
the newline character is consumed; whichever occurs first.
46. Given the declaration below, using the assign function, write the statements to assign 20 asterisks (‘*’) to the line
variable.
string line = “”;
47. Write the statement to read and consume one character stored in the cin object.
48. Given the declarations below, write the statements to combine the person’s full name and store it into the full string
variable.
string first = “First”;
string middle = “Middle”;
string last = “Last”;
string space = “;
string full = “”;
Name:
Class:
Date:
.
Page 6
Name:
Class:
Date:
.
Page 7
Name:
Class:
Date:
.
Page 8