CH 04 Q U I Z
Assume a is 13 and b is 10; what prints? string s{"feed the fish"}; cout << s.substr(a, b) << endl;
""
What value is stored in a after this runs? string s{"ABCDEFGHIJKLM"}; auto a = s.substr(1, 4);
"BCDE"
What value is stored in a after this runs? string s{"abcdefg"}; auto a = s.substr(3);
"defg"
Assume a is 9 and b is 10; what prints? string s{"feed the fish"}; cout << s.substr(a, b) << endl;
"fish"
Assume a is 5 and b is 3; what prints? string s{"feed the fish"}; cout << s.substr(a, b) << endl;
"the"
Assume c is a char variable. What value s stored in the variable a? string s{"guten tag"}; auto len = s.size(); auto a = s.front(); s.at(len) = a; s[len] = c;
'g'
In Line 2, what is the explicit argument? string s{"happy"}; auto pos = s.find('y');
'y'
How many variables appear in the following code segment? int n = 4; int& r1 = n; auto& r2 = r1; r1 = 3; r2 = 5; cout << n << endl;
1
What value is stored in a after this runs? string s{"ABCDEFD"}; auto a = s.find('D');
3
Assume c is a char variable. Which line throws an error because of range checking? string s{"guten tag"}; // 1 auto len = s.size(); // 2 auto a = s.front(); // 3 s.at(len) = a; // 4 s[len] = c; // 5
4
Which of these lines are legal? [1] int n1 = 4; [2] double n2 = 3.145; [3] unsigned char n3 = 158; [4] int n4 = n2; [5] int& r1 = n1; [6] int& r2 = n2; [7] double& r3 = n1; [8] const int& r4 = n2;
4 5 8
Which lines cause syntax errors? [1] string s{"shiver me timbers"}; [2] auto len = s.size(); [3] s.front() = 'S'; [4] s.back() = "S"; [5] s[len] = 'X'; [6] s.substr(0, 1) = "W"; [7] auto a = s.substr(0, 100); [8] auto b = s.substr(4, 3); [9] auto c = s.substr(len);
4 6
Assume c is a char variable. Which line produces undefined behavior? string s{"guten tag"}; // 1 auto len = s.size(); // 2 auto a = s.front(); // 3 s.at(len) = a; // 4 s[len] = c; // 5
5
What does this code segment print? int n = 4; int& r1 = n; auto& r2 = r1; r1 = 3; r2 = 5; cout << n << endl;
5
Which of these lines are illegal? [1] int n1 = 4; [2] double n2 = 3.145; [3] unsigned char n3 = 158; [4] int n4 = n2; [5] int& r1 = n1; [6] int& r2 = n2; [7] double& r3 = n1; [8] const int& r4 = n2;
6 7
Which lines compile and return string objects? [1] string s{"shiver me timbers"}; [2] auto len = s.size(); [3] s.front() = 'S'; [4] s.back() = "S"; [5] s[len] = 'X'; [6] s.substr(0, 1) = "W"; [7] auto a = s.substr(0, 100); [8] auto b = s.substr(4, 3); [9] auto c = s.substr(len);
7 8 9
What header file do you include to call the isupper() function?
<cctype>
Data member is the term used in C++ for what is called a method in Java
False
In C++ a char may be one, two or three bytes, when using UTF-8.
False
The getline() function is part of the string class.
False
The toupper() member function ignores case when it searches.
False
Assume c is a char variable. What type is the expression s.last()? string s{"guten tag"}; auto len = s.size(); auto a = s.front(); s.at(len) = a; s[len] = c;
None of these
Assume c is a char variable. Which line produces a syntax error? string s{"guten tag"}; // 1 auto len = s.size(); // 2 auto a = s.front(); // 3 s.at(len) = a; // 4 s[len] = c; // 5
None of these
In Line 2, what is the parameter? string s{"happy"}; auto pos = s.find('y');
None of these
Which lines cause runtime errors (exceptions)? [1] string s{"shiver me timbers"}; [2] auto len = s.size(); [3] s.front() = 'S'; [4] s.back() = "S"; [5] s[len] = 'X'; [6] s.substr(0, 1) = "W"; [7] auto a = s.substr(0, 100); [8] auto b = s.substr(4, 3); [9] auto c = s.substr(len);
None of these
Assume a is 14 and b is 10; what prints? string s{"feed the fish"}; cout << s.substr(a, b) << endl;
Runtime error
What value is stored in a after this runs? string s{"ABC"}; auto a = s.substr(4, 5);
Runtime error because start (4) must bet 0..3
Calling s.at(0) returns the same reference as s.front().
True
s.back() = 'x'; changes the last character in the string object s to 'x'.
True
s.at(0) = 'c'; changes the first character in the string object s to 'c' s.at(0) = "c"; changes the first character in the string object s to 'c'
True False
The string find() member function may be used to search for a substring The string find() member function takes either a string or character as an argument The string find() member function throws an exception if the target cannot be found.
True True False
Calling s.at(1) returns a reference to the second character in the string object s Calling s.at(1) returns a copy of the second character in the string object s
True False
string s{"ahoy"}; auto a = s.size(); auto b = s.back(); auto c = s.at(0); auto d = s.substr(a); auto e = s.substr(0, 1);
[a] : string::size_type [b] : 'y' [c] : 'a' [d] : "" [e] : "a"
To enter a Unicode character into a C++ string, use an escape sequence starting with:
\U
Match the letter of the variable in the figure with the correct value or expression below string s{"walk the plank"}; auto a = s.find('a'); auto b = s.find('a', 3); auto c = s.find("nk"); auto d = s.find("Walk");
a : 1 b : 11 c : 12 d : string::npos
To produce one of two values (of any type) in an expression, use:
a conditional operator
To combine several test conditions to produce a single Boolean value, use:
a logical operator
The relative order of two variables is tested using:
a relational operator
Which of these selects a character (char) from a string?
auto a = s[0];
String parameters should be passed to functions:
by constant reference ( const string& s) when not modified in the function. by reference (string& s) when modified in the function
Assume c is a char variable. What type is the variable a? string s{"guten tag"}; auto len = s.size(); auto a = s.front(); s.at(len) = a; s[len] = c;
char
In Line 2, what is the request? string s{"happy"}; auto pos = s.find('y');
find
One-way, independent decisions use:
if
Either/or decisions should use:
if . . . else
Multiple possible outputs, testing a single condition, use:
if . . . else . . . if . . . else
Leveled decisions, such as processing income taxes are best handled with:
if . . . if . . . else . . . else
This compiles, runs and prints 12. What is the correct parameter declaration for x?
int& x
In Line 2, what is the result of this function call? string s{"happy"}; auto pos = s.find('y');
pos
In Line 2, what is the receiver? string s{"happy"}; auto pos = s.find('y');
s
All of these are declared in the <string> header; which are member functions?
size() front() find() at()
What value is stored in a after this runs? string s{"ABCDEFD"}; auto a = s.find('G');
string::npos
What type is the variable len? string s{"guten tag"}; auto len = s.size(); auto a = s.front(); s.at(len) = a; s[len] = c;
string::size_type
Decisions based on numbered blocks of code are best handled with:
switch
In Line 2, what is the implicit argument? string s{"happy"}; auto pos = s.find('y');
the address of s
A C++ string that contains Unicode characters should be preceded by:
u8
This compiles, runs and prints 4, 3. What is the correct prototype? int x = 3, y = 4; swap(x, y); cout << x << ", " << y << endl;
void swap(int& a, int& b);
What is stored in s after this code runs? string s{"xyzw"}; s.at(2) = 'Y';
xyYw