Test Bank for Absolute C++, 6th Edition

Preview Extract
Chapter 2 – Test Questions These test questions are true-false, fill in the blank, multiple choice, and free form questions that may require code. The multiple choice questions may have more than one correct answer. You are required to mark and comment on correct answers.. Mark all of the correct answers for full credit. The true false questions require an explanation in addition to the true/false response, and, if false, also require a correction. True False: 1. The if, while and for statements control only one statement. Answer: True Explanation: The one statement may be a block (statements that are enclosed with curly braces { }) or a simple statement. 2. Given the declaration int x = 0; The following expression causes a divide by zero error: (x !=0) || (2/x < 1); Answer: False. Explanation: The || operator uses short-circuit evaluation. The first member of this expression is true; the truth value of the complete expression can be determined from this; consequently, the second expression is not evaluated. There is no divideby-zero error. 3. Suppose we have these declarations, int x = -1, y = 0, z = 1; This Boolean expression is correct and it does what the programmer intends. x < y < z Answer: False Test Bank for Savitch Absolute C++ 6e Page 2 Explanation: Unfortunately, the expression compiles without error and runs. The < operator associates (groups) left to right, so the expression evaluates as (x < y) < z The left hand expression evaluates to true, which, if compared to a numeric type, converts to 1. When compared to 1, this is false. What the programmer intends, expressed as mathematacs might is -1 < 0 limit Answer: False. Explanation: The expression always evaluates to false. This cannot be what the programmer intended. The compiler doesnโ€Ÿt catch the problem because the code is legal, correct C++. Corrected code is !(time > limit) Code execution proceeds as follows: The operator ! takes a bool argument. It returns the opposite bool value. The value of time is converted to a bool. The value of time is certainly nonzero, hence !time is !true, i.e., false. The > compares this result with a numeric value, limit, (an int or perhaps some kind of floating point). The value on the left (false) is converted to a 0 value of that type. The value of limit is unlikely to be a negative number and we are concerned about time running out, so it is unlikely that time is zero. Consequently, the inequality becomes 0>limit, where limit is nonzero. This is false. 5. The value of count is 0; limit is 10. Evaluate: (count == 0)&&(limit < 20) Answer: true 6. The value of count is 0; limit is 10. Evaluate: count == 0 && limit < 20 Answer: true ยฉ2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. Test Bank for Savitch Absolute C++ 6e Page 3 Explanation: The operators == and < have higher precedences than &&, hence the expressions, count == 0 and limit < 10 are evaluated (to true) then the && is executed. 7. The value of count is 0; limit is 10. Evaluate: (count != 0)||(limit < 20) Answer: true. Explanation: The first expression evaluates to false, the value of the || expression is determined by the second expression. The second expression is true so the || expression evaluates to true. 8. In a while loop, the Boolean_Expression is executed before each execution of the loop body. Answer: true 9. In a do-while loop, a continue statement terminates the loop. Answer: False Explanation: The continue statement causes the Boolean_Expression to be executed. If true, the body executes, otherwise the loop terminates. 10. A break statement is used in loops only. Answer: False. Explanation: In addition to its use in loops, a break statement is used in the switch statement to transfer control to the next statement after the switch block. 11. When a loop is nested in side another loop, a break or continue statement terminates or restarts the outermost loop of the nested loop structure. Answer: False Explanation: A break or continue terminates or restarts only the innermost loop containing the break or continue. ยฉ2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. Test Bank for Savitch Absolute C++ 6e Page 4 Free Form Questions: 1. Assume variables first and second are declared to be double and are initialized. Write a sequence of lines of code that cause the values stored in first and second to be exchanged if the value of first is not less than second. Answer: // double first, second; // these have been initialized if (!(first < second)) { double temp = first; first = second; second = temp; } //assert: first 1000) cout << โ€œBoiler Pressure: TOO LOWnโ€; else if (boiler_pressure < 100) cout << โ€œBoiler Pressure: TOO LOWnโ€; else cout <= 90) letter_grade = โ€žAโ€Ÿ; else if (numeric_grade >= 80) letter_grade = โ€žBโ€Ÿ; else if (numeric_grade >= 70) letter_grade = โ€žCโ€Ÿ; else if (numeric_grade >= 60) letter_grade = โ€žDโ€Ÿ; else letter_grade = โ€žFโ€Ÿ; 4. Assume variables first, second, and max are declared to be double and are initialized. Write a sequence of lines of code that cause the larger of the values in first and second to be stored in max. Answer: //double first, second, max //first and second have been initialized if ( (first = first && max >= second 5. A numeric integer grade is between 50 and 99. Using integer division or otherwise obtain a int value that is 5, 6, 7, 8, or 9 from the numeric grade. Write code using a ยฉ2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. Test Bank for Savitch Absolute C++ 6e Page 6 switch statement using this number in the selector expression that assigns letter grades based on this 10 point scheme: if the numeric_grade is not less than 90, the letter_grade is an A, if the numeric_grade is not less than 80, the letter_grade is a B, if the numeric_grade is not less than 70, the letter_grade is C, if the numeric_grade is not less than 60, the letter_grade is D, otherwise the letter_grade is F. Answer: int value = numeric_grade/10; switch(value) { case 9: letter_grade = โ€žAโ€Ÿ; break; case 8: letter_grade = โ€žBโ€Ÿ; break; case 7: letter_grade = โ€žCโ€Ÿ; break; case 6: letter_grade = โ€žDโ€Ÿ; break; default: letter_grade = โ€žFโ€Ÿ; break; } 6. Write Boolean expressions that represent the given English expressions. Assume any variables used have been declared and initialized. a) alpha is greater than 1 ยฉ2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. Test Bank for Savitch Absolute C++ 6e Page 7 b) x is odd c) x and y are odd d) ch is an upper case alphabetic character (between ‘A’ and ‘Z’). e) digit, which is f type char, has value that is indeed a digit. Answer: a. alpha > 1 b. (x%2==1) c. (x % 2==1) && (y % 2==1) d. (‘A’ <= ch) && (ch <= 'Z') e. ('0' <= digit) && (digit = 0) c) The hit is no more than 2.5 units away from target d) x is 1 or x is equal to y e) t is strictly between 3.2 and 3.3 Answer: a) (x%2==1) || (y%2==1) b) (x >= 0) || (y>=0) c) ((hit – target) <= 2.5 ) && ((hit – target) <= -2.5) d) (1==x) || (x==y) e) (3.2 < t) && (t n2) ? n1 : n2; Explanation: The parentheses are present only for readability. That the > operator has higher precedence makes the parentheses unnecessary. 14. What is the output from each of the following loops? a) while ( 0 ) cout << โ€žXโ€Ÿ; cout << endl; b) do cout << โ€žXโ€Ÿ; while ( y != y ); cout << endl; c) int i = 1; while (i < 9) { cout i; i++; } cout << endl; d) char c = 'A'; do { cout << c << " "; c = c + 2; } while ( c <= 'M' ) cout << endl; e) int i = 0; while (i < 50) { ยฉ2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. Test Bank for Savitch Absolute C++ 6e Page 9 if ( i < 20 && i != 15 ) cout << 'X'; i++; } cout << endl; Answer: The output of each loop is: a. The only output is a carriage return. b. X c. 12345678 d. A C E G I K M e. XXXXXXXXXXXXXXXXXXX (There are 19 Xs.) 15. Write the following do-while statement with a while construct, and maybe some extra code. x = 10; do { cout << x < 0 ); Answer: A simple minded change from do while to while and insertion of the loop body gives this: x = 10; cout << x < 0 ) { cout << x < 0 ) { cout << x << endl; x = x – 3; } 16. Write a program that reads in exactly 10 integers and outputs the sum. Answer: #include //loop to accept 10 integers and sum int main() { using namespace std; int x, sum = 0; cout << "Enter 10 integers, each followed by ” << " I will give the sum." << endl; for(int i =0; i < 10; i++) { cout << "Enter integer " << i <> x; sum = sum + x; } cout << "sum: " << sum <>Delete above comment #include using namespace std; //loop to accept positive integers until nonnegative //integer, then return sum int main() { int x = 1, i = 0, sum = 0; cout << "Enter positive integers, followed by ” << " 0 or negative stops." << endl << " I will give the sum." < 0 ) { cout << "Enter integer " << i <> x; sum = sum + x; i++; } cout << "sum: " << sum << endl; return 0; } 18. For each of the following situations, tell which type loop (while, do-while, or for) would be best in that situation: a) Reading a list of an unknown number of homework grades for a single student. b) Summing a series such as 1 +1/2 +1/(22) + 1/(23) + โ€ฆ + 1/(28) c) Testing a newly coded library function to see how it performs for different values of its arguments. d) Reading in the number of days of vacation taken by an employee. Answer: a) A while loop because the list of grades may be empty. ยฉ2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. Test Bank for Savitch Absolute C++ 6e Page 12 b) A for loop, because the length of the list of numbers is known. c) A do-while loop could be used since there will be at least one value tested. d) A while loop because the list of grades may be empty. 20. Predict the output of the following nested loops: int n = 1; while(n =1) { cout << n << โ€œ times โ€œ << m << โ€œ = โ€œ << n*m << endl; m–; } n++; } Answer: The output is a multiplication table 1 times 10 = 10 1 times 9 = 9 . . . 1 times 1 = 1 2 times 10 = 20 . . . 2 times 1 = 2 3 times 10 = 30 . . . 3 times 1 = 3 4 times 10 = 40 4 times 9 = 36 . . . ยฉ2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. Test Bank for Savitch Absolute C++ 6e 4 times 2 = 8 4 times 1 = 4 . . . 9 times 10 = 90 . . . 9 times 1 = 9 10 times 10 = 100 . . . 10 times 1 = 10 21. Rewrite the following while loops as for loops: a) int i = 1; while(i<=10) { if (i<5 && i !=2) cout << 'X'; i++; } b) int i =1; while(i<=10) { cout << 'X'; i = i + 3; } c) int n = 100; do { cout << 'X'; n = n + 100; }while(n < 1000); ยฉ2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. Page 13 Test Bank for Savitch Absolute C++ 6e Page 14 Answer: a) for( int i=1; i<=10; i++) if (i<5 && i !=2) cout << 'X'; b) for(int i=1; i<=10; i = i + 3) cout << 'X'; c) for( int n = 100; n <1000; n = n + 100) cout << 'X'; 22) Here is some code that uses an enum: enum color {red, green, blue}; color paint = green; cout << paint << endl; Rewrite this using strong enums. What is the advantage of strong enumerations over the old style enumeration? Answer: color paint = color::blue; switch (paint) { case color::red: cout << "red"; case color::green: cout << "green"; case color::blue: cout << "blue"; } You may have discovered that you cannot print out an enum class with cout. This is because enum classes are strongly typed and are not a simple mapping to integers. This allows for stronger type checking which can help prevent storing invalid values in an eum class variable. ยฉ2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. Test Bank for Savitch Absolute C++ 6e Page 15 Multiple Choice There may be more than one correct answer. You must give all correct answers for full credit. An explanation is required. 1. Which control construct repeats a sequence of statements zero or more times? a) while statement b) do-while statement c) for statement d) switch statement e) if-else statement Answer: a), c) Explanation:: b) repeats 1 or more times, d) and e) are selection statements 2. What is the value of the bool valued expression, 1 < x < 10? Does the value of this depend on the value of x? Explain, and give the expression that the programmer probably meant. a) This statement is incorrect as it is always false. b) This statement is correct and its value depends on x. c) This statement is incorrect and it is always true d) This statement is incorrect, but it does depend on the value of x. Answer: c) This expression is always true. The value does not depend on x. This is the mathematiciansโ€Ÿ shorthand for what the programmer probably meant: (1 < x)&&(x < 10) Explanation: The < operator associates (groups) left to right, so the expression evaluates as (1 < x) < 10. The expression (1 < x) evaluates to either false or true, regardless of x. These bool values convert to int values 0 or 1 when compared to int value 10. The expression evaluates to true for all values of x. 3. Which of the following is true of the && operator? a) It has two operands. b) It can have one operand. c) It uses short circuit evaluation. d) It is the logical AND operator. ยฉ2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. Test Bank for Savitch Absolute C++ 6e Page 16 e) It returns true if either operand is true. Answer: a), c), and d). Explanation: b) is a wrong number of argments, e) is the OR command. 4. Which of the following is true of the || operator? a) It has two operands. b) It can have one operand. c) It is the logical OR operator. d) It returns true if either operands is true. e) It uses short circuit evaluation. Answer: a) c) d) and e) Explanation: b) is a wrong number of operands 5. In a switch statement, when a break statement is encountered, an immediate transfer of control is made to a) the default case of the switch statement b) a goto statement c) the else clause d) the statement beyond the end of the switch statement. e) none of these Answers: d) 6. An assignment of the value of a conditional expression to a variable (x =y?z:w;) can always be replaced by a) a switch statement with assignment statements for its case statements. b) one or more ifs with else clauses and assignment statements for its true and false clauses. c) one or more nested while loops with assignments for the bodies of the loops. d) one or more ifs without any else clauses and assignment statements for its yes_statement(s). e) none of these is correct. Answer: a), b), c) and d) Explanation: c) is correct too, though strange. Here is one solution: ยฉ2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. Test Bank for Savitch Absolute C++ 6e #include using namespace std; int main() { int x , z = 10, w =-10; bool y = true; while( y || (x = w)) { while (y) { x = z; break; } break; } cout << x << endl; y = false; while( y || (x = w)) { while (y) { x = z; break; } break; } cout << x << endl; return 0; } ยฉ2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. Page 17 Test Bank for Savitch Absolute C++ 6e Page 18 /* Output is: 10 -10 */ 7. In distinguishing an expression as true or false, C++ sees which of the following as true? a) true b) 0 c) 1 d) Any non-zero value e) The character 'F' Answer: a), c), and d) e) โ€žFโ€Ÿ is coerced char to bool, perhaps through an intermediate type 8. Which of the following determines the operator that is processed prior to another operator? a) Operator associativity b) Operator precedence c) Whether the operator is an arithmetic operator d) None of these determine the order in which operators are processed. Answer: b) 9. The following program purports to sum all entered int values that are greater than 5. It compiles without any error message, and it executes without error message, but nevertheless is wrong. Name all the errors. // Display the sum of all entered int values // greater than 5 #include int main() { using namespace std; int x, sum; while (x > x; if (x > 5); sum = sum +x; } cout < 5 is โ€œ << sum < b) a = 4; if ( b > c) a = 5; else a = 6; cout << a c, evaluates to false so the else clause executes, so that a is assigned the value 6. 16. Here is a collection of if and if-else statements with semicolons in various places. Assume all variables have been declared and initialized. Which of these are correct and are likely to give the programmers intent? Which are correct but unlikely to give the programmer’s intent? Give the error for the remaining. a) if ( a > b ); ยฉ2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. Test Bank for Savitch Absolute C++ 6e Page 22 a = b; else b = a; b) if(a > b ) a = b; else; b = a; c) if(a > b ) a = b; else b = a; d) if(a > b) a = b else b = a; e) if( x !=0 ) a = a / x Answer: c) is correct and is likely to be the programmerโ€Ÿs intent. b) compiles but is unlikely to be the programmerโ€Ÿs intent. Explanation: a) Compiler error: The semicolon at the end of the if line introduces a null statement, making the else keyword an error. The error will not be detected by the compiler until the else is encountered. b) Compiles with no errors, but is unlikely to do what the code author intended: The indentation suggests that the programmer meant the a=b and b=a lines to be alternatives chosen based on whether a>b. The semicolon at the end of the else causes the statement that appears to be the else clause always to be executed. c) correct, and apparently does what the programmer intended. d) Here the compiler will find an error at (or after) the else, since this is the first token after the assignment a=b, where a semicolon is needed. ยฉ2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. Test Bank for Savitch Absolute C++ 6e Page 23 e) This is the same error as in d), but the error will be detected at or after the last x on the second line. 17. Here is a collection of while and do-while statements. Identify: i. those that are correct, and are likely to give the programmers intent; ii. those that are correct, but unlikely to give the programmer’s intent, and iii. what compiler error will the rest generate? Assume all variables have been declared, but not necessarily initialized. a) cin >> n; while (-1 != n) { sum = 0; sum = sum + n; } b) cin >> value; while ( value != -1 ) sum = sum + value; cin >> value; c) cin >> n; int i = 1, >>Semicolon not comma while ( i > count >> limit; do count++ while ( count ๏€ช๏€ count > limit ); e) cin >> x; do x++; while( x > x ); ยฉ2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. Test Bank for Savitch Absolute C++ 6e Page 24 Answer: a) This compiles. It is unlikely to be what the programmer intended. What the intent might be is hard to guess, since the value of sum is reset to 0 each time the loop executes. b) This compiles. The indentation suggests that the programmer intended the two lines following the to be in the body of the loop. The second of these is not controlled by the while clause, resulting in an infinite loop . >>Something is wrong. Maybe the indentataion?? c) This compiles. There are two intent errors evident: the semicolon on the second line and the missing braces. If the loop is entered, this is an infinite loop, since the statement controlled by the loop is the null statement inserted by the semicolon on the second line changes neither i nor n. The intent evidently was to run the loop n or n-1 times. d) The syntax error is a semicolon missing from count++. e) This compiles, but the loop executes its body only once. It is difficult to guess what the programmers intent might have been, but this probably isn’t it! 18. If the following code fragment is executed in an otherwise complete and correct program, which expression will be executed? Why? x = 0; if (x = 12) yes_statement; else no_statement; a) The no_statement will be executed because x is not 12. b) The statement has incorrect syntax so will not compile at all. c) x=12 is illegal in the Boolean expression of an if statement. d) The yes_statement will be executed. Answer: d) Explanation: The expression x = 12 has the value 12, which is converted to true, causing the if always to execute the yes_statement. ยฉ2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. Test Bank for Savitch Absolute C++ 6e Page 25 19. Which of the following control structures requires curly braces? a) if-else b) while c) do-while d) switch e) for Answer: d) Explanation: The other control constructs operate on a single statement, which may be, but is not required to be, a compound statement. 20. In the expression (j > 0 && j+1 == 10), which operator executes last? a) > b) && c) + d) == Answer: b) Explanation: The precedences, higher to lower are: +, >, ==, &&. So the order is j+1, j>0, then (j+1)==10, then (finally) the ((j>0) && ((j+1) == 10)) 21. When you donโ€Ÿt recall operator precedences you can a) Look in a table of precedences b) Guess c) Use parentheses d) Experiment with the compiler Answer: c) is perhaps best and easiest, but a) and d) are reasonable if you arenโ€Ÿt taking an exam. 22. Consider the if statement: if(condition) yes_clause; else no_clause; Under which of the following circumstances will both the yes_clause and the no_clause will be executed? a) When the condition is true. b) When the condition is false. ยฉ2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. Test Bank for Savitch Absolute C++ 6e c) When the condition is ambiguous. d) This will not happen. Answer: d) 23. The following are true about the expression left && right. a) The expression is false when left is false and right is false b) The expression is true when left is true and right is false c) The expression is false when left is false and right is true d) The expression is true when left is true and right is true Answer: a) c) and d) 24. The following are true about the expression left || right. a) The expression is false when left is false and right is false b) The expression is true when left is true and right is false c) The expression is false when left is false and right is true d) The expression is true when left is true and right is true Answer: a) b) and d) 25. The statements int x = 1; int y; y = x++; a) Assign y the value 2; b) Change the value of x to 2. c) Assign y the value 0; d) Assign y the value 1; e) The ++ is a postfix operator. Answer: b) d) and e) 26. The statements int x = 1; int y; y = –x; a) Assign y the value 1; b) Change the value of x to 0 c) Assign to y the value 1; d) Assign to y the value 0; e) The — is a prefix operator. Answer: b) d) and e) ยฉ2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. Page 26 Test Bank for Savitch Absolute C++ 6e Page 27 27. What is the output of the following, if it were embedded in an otherwise correct and complete program and run? int x = 10; while (x > 0) { cout << x << โ€œ โ€; x = x + 3; } cout << endl; a) 10 13 16 19 . . . b) The compiler detects that this will be an infinite loop, so it does not compile. Insert lowercase be c) This is an infinite loop. When compiled and run, it runs until machine limitations stop it, or you get tired of it and kill the process. d) 0 3 6 9. Answer: a) and c) 28. This question asks about nesting of if, if-else, switch, while, dowhile, and for statements: a) These constructs may not be nested in at all. b) These constructs may be nested in any way that meets the needs of algorithms the programmer is coding. c) Only control constructs of a given kind may be nested (while loops within while loops; if-else within if-else etc.) d) The question does not make sense in C++. Answer: b) Control constructs may be nested arbitrarily. 29. Each of the following has at least one error, either intent, or an error that may be caught by the compiler or both). What is the error? Assume all the variables you see are defined and initialized. a) for(int i = 0; i 0) ยฉ2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. Test Bank for Savitch Absolute C++ 6e Page 28 x = 3 else x =4; c) if(x = 0) x = MAXINT; d) if x > 0 x = 3; e) if (x>0) then x =3; Answer: a) Not wrong, but the semicolon is probably an intent error. b) Semicolon missing in the yes_clause. c) The = probably should be ==. d) needs parenthese to be correct C++. e) C++ does not use โ€œthenโ€ as a keyword. If we blindly fix the syntax, whatever the โ€thenโ€ might be, there needs to be a semicolon after it, and the sequence then; x=3; should probably be in curly braces. Actually, there no way to guess what the programmer might mean here. There is a good chance the programmer is a refugee from Pascal, and then should be deleted. ยฉ2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

Document Preview (28 of 362 Pages)

User generated content is uploaded by users for the purposes of learning and should be used following SchloarOn's honor code & terms of service.
You are viewing preview pages of the document. Purchase to get full access instantly.

Shop by Category See All


Shopping Cart (0)

Your bag is empty

Don't miss out on great deals! Start shopping or Sign in to view products added.

Shop What's New Sign in