cs 150 (ch. 2)
You can find the length of a string str using str.size(). In C++, size() is called:
a member function
In C++, what keyword is used for type inference?
auto
If a is false, which expressions need not be evaluated? if (a && b || c && d || e) . . .
b
If a and c are both false, which expressions need not be evaluated? if (a && b || c && d || e) . . .
b d
If a and b are true, which expressions need not be evaluated? if (a && b || c && d || e) . . .
e d c
The code shown here: auto n = 3; if (n = 0) cout << "n is 0" << endl; else cout << "n is " << n << endl;
executes the false branch displays "n is 0" contains an embedded assignment.
This code illustrates the ____________ idiom. auto n = 3; if (n % 2 == 1) cout << "Odd" << endl;
guarded action
The C++ string class is defined in the header:
<string>
Match each item with the correct statement below.
A function that calculates and returns a value : fruitful function A block containing statements that implement the function's actions : body A named block of code that carries out an action or calculates a value : function Another name for a function declaration : prototype Variables defined along with the function to receive input : parameters Executing, running or invoking the function : calling A function that carries out an action instead of calculating a value : procedure produces a value when the function is invoked : return statement Specifying the calculation or actions that occur when the function is used : defining Specifying the function name type and parameter types : declaring Values passed to the function when it is invoked : arguments
What is the output? auto x = 40; if (x <= 40) cout << "F"; if (x <= 75) cout << "C"; if (x <= 90) cout << "B"; cout << endl;
FCB
In C++ you cannot use the relational or equality operators with strings.
False
Match each item with the control structure best equipped to handle it.
Handle processing for a group of check boxes : independent if statements Handle an on or off condition : if-else statement Handle numeric selections made from a menu : the switch statement Handle processing for a group of radio buttons : sequential if statements Handle processing for income taxes : nested if statements Set a variable to one of two possible values : the conditional operator
Assume the user enters Inigo Montoya when prompted. What prints? cout << "What's your name: "; string name; cin >> name; cout << "Howdy " + name + "!" << endl;
Howdy Inigo!
This code illustrates the ____________ idiom. auto n = 3; else if (n % 2 == 1) n = -n; else if (n < 0) n++; else if (n % 2 = 0) n--; else n = 0;
None of these are correct
Assuming lastName is a string object, is this statement legal in C++? if (lastName <= "Gilbert") . . .
True
Assuming str is a string object, does this correctly test if str consists of the characters "quit" in C++? if (str == "quit") . . .
True
In C++ you can compare strings using all of the relational operators.
True
In C++ you can concatenate string objects using the + or += operators
True
Strings in C++ are mutable.
True
This code illustrates the ____________ idiom. auto n = 3; if (n % 2 == 1) cout << "Odd" << endl; else cout << "Even" << endl;
alternative action
In C++, characters of type char:
can be preceded by signed or unsigned for use as small integers generally use 8 bits of storage. use the ASCII character set are defined for the first 127 characters
In C++, the statement string s{"world"};
creates a string variable explicitly initialized with the character array "world"
In C++, the statement string s = "world";
creates a string variable implicitly initialized with the character array "world"
In C++, the statement string s{3, 'X'};
creates a string variable of size 3, filled with 'X
This code illustrates the ____________ idiom. auto n = 3; if (n % 2 == 1) n = -n; if (n < 0) n++; if (n % 2 = 0) n--;
independent if statements
What is true about string::size_type?
it is returned from the string length() member function it is the same as size_t it is returned from the string size() member function you may create variables of that type it is an unsigned integer type of some size
Which operator is used to see if all of a set of conditions is true? which operator is used to see if any of a set of conditions is true?
logical and logical or
This code illustrates the ____________ idiom. auto n = 3; if (n % 2 == 1) n = -n; else if (n < 0) n++; else if (n % 2 = 0) n--; else n = 0;
multiple selection
Assume that name is a string object. Which of these expressions are legal?
name += 'X' name < "bob" name += "fred" name == "sally"
In C++, the statement string s;
produces an empty string object
Match the letter at the right of each line in the figure with the correct statement below. string s1; // A. string s2 = "hello"; // B. string s3{"world"}; // C. string s4(20, '-'); // D. string s5(R"("bob")"); // E. #include // F. cin >> s1; // G. getline(cin, s2); // H.
reads one line of text from standard input : choice H explicitly converts a character array to a string object : choice C Reads one word or token from standard input : choice G Needed to use the C++ string type : choice F Produces a string that may contain quotes or backslashes : choice E implicitly converts a character array to a string object : choice B produces the empty string : choice A Produces a string from multiple copies of a single character : choice D
Which of these are impossible conditions? auto floor = ??? // some number; bool v1 = floor >= 0 || floor <= 20; bool v2 = floor <= 0 && floor >= 20; bool v3 = floor <= 0 || floor >= 20; bool v4 = floor >= 0 && floor <= 20; bool v5 = floor >= 0 || floor < 20; bool v6 = floor >= 0 && floor > 20; bool v7 = floor >= 0 || floor > 20; bool v8 = floor >= 0 && floor < 20;
v2
[] and () denote whether a range includes or excludes an endpoint: [ includes the endpoint ( excludes the endpoint [] = 'Closed', includes both endpoints () = 'Open', excludes both endpoints [) and (] are both 'half-open', and include only one endpoint Which variable correctly indicates that the variable floor is not in the range (0...20)? auto floor = ??? // some number; bool v1 = floor >= 0 || floor <= 20; bool v2 = floor <= 0 && floor >= 20; bool v3 = floor <= 0 || floor >= 20; bool v4 = floor >= 0 && floor <= 20; bool v5 = floor >= 0 || floor < 20; bool v6 = floor >= 0 && floor > 20; bool v7 = floor >= 0 || floor > 20; bool v8 = floor >= 0 && floor < 20;
v3
[] and () denote whether a range includes or excludes an endpoint: [ includes the endpoint ( excludes the endpoint [] = 'Closed', includes both endpoints () = 'Open', excludes both endpoints [) and (] are both 'half-open', and include only one endpoint Which variable correctly indicates that the variable floor is in the range [0...20]? auto floor = ??? // some number; bool v1 = floor >= 0 || floor <= 20; bool v2 = floor <= 0 && floor >= 20; bool v3 = floor <= 0 || floor >= 20; bool v4 = floor >= 0 && floor <= 20; bool v5 = floor >= 0 || floor < 20; bool v6 = floor >= 0 && floor > 20; bool v7 = floor >= 0 || floor > 20; bool v8 = floor >= 0 && floor < 20;
v4
Which of these are unavoidable conditions? auto floor = ??? // some number; bool v1 = floor >= 0 || floor <= 20; bool v2 = floor <= 0 && floor >= 20; bool v3 = floor <= 0 || floor >= 20; bool v4 = floor >= 0 && floor <= 20; bool v5 = floor >= 0 || floor < 20; bool v6 = floor >= 0 && floor > 20; bool v7 = floor >= 0 || floor > 20; bool v8 = floor >= 0 && floor < 20;
v5 v1
[] and () denote whether a range includes or excludes an endpoint: [ includes the endpoint ( excludes the endpoint [] = 'Closed', includes both endpoints () = 'Open', excludes both endpoints [) and (] are both 'half-open', and include only one endpoint Which variable correctly indicates that the variable floor is in the range [0...20)? auto floor = ??? // some number; bool v1 = floor >= 0 || floor <= 20; bool v2 = floor <= 0 && floor >= 20; bool v3 = floor <= 0 || floor >= 20; bool v4 = floor >= 0 && floor <= 20; bool v5 = floor >= 0 || floor < 20; bool v6 = floor >= 0 && floor > 20; bool v7 = floor >= 0 || floor > 20; bool v8 = floor >= 0 && floor < 20;
v8
In C++, what is true about concatenating string literals (character arrays)?
you do it by separating the string literals with white space
In C++, what is true about the += operator operating on string objects
you may concatenate a string literal (character array) to a string object you may concatenate creates a string variable to a string object you may concatenate a char variable to string object you may concatenate a char literal to a string object
