Date 04/26/2010 COMP1013 STUDY QUESTIONS AND ANSWERS 1. True or False? An example of a logical (Boolean) expression is an arithmetic expression followed by a relational operator followed by an arithmetic expression. 2. True or False? Boolean variables cannot store the result of a comparison of two variables. 3. True or False? Syntactically, the only expressions that can be assigned to Boolean variables are the literal values true and false. 4. True or False? If ch1 contains the value 'C' and ch2 contains the value 'K', the value of the C++ expression ch1 <= ch2 is true. 5. True or False? If P and Q are logical expressions, the expression P AND Q is TRUE if either P or Q is TRUE or both are TRUE. 6. True or False? The expression !(n < 5) is equivalent to the expression n > 5. 7. True or False? To test whether someInt equals 25 or 30, the C++ expression someInt == 25 || 30 has the correct semantics but produces a syntax (compile-time) error. 8. True or False? According to DeMorgan's Law, the expression !(x <= y || s > t) is equivalent to x <= y && s > t 9. True or False? The statement if (grade == 'A' || grade == 'B' || grade == 'C') cout << "Fail"; else cout << "Pass"; prints Pass if grade is 'A', 'B', or 'C' and prints Fail otherwise. 10. True or False? If a C++ If statement begins with if (age = 30) the If condition is an assignment expression, not a relational expression. 11. True or False? The code segment if (speed cout << if (speed cout << if (speed cout << <= 40) "Too slow"; > 40 && speed <= 55) "Good speed"; > 55) "Too fast"; could be written equivalently as if (speed <= 40) cout << "Too slow"; else if (speed <= 55) cout << "Good speed"; else cout << "Too fast"; 12. True or False? If the code fragment if (a >= 10) if (a < 20) a = a + 2; else a = a + 1; is indented according to the manner in which it is executed, the correct indentation is if (a >= 10) if (a < 20) a = a + 2; else a = a + 1; 13. True or False? Testing is performed only in the implementation phase of a program's life cycle. 14. True or False? Given the module description PRINT AVERAGE (Level 1) Print a heading Print sumOfValues / countOfValues the following is an appropriate module precondition: "sumOfValues is assigned AND countOfValues does not equal 0." 15. True or False? Given the module description PRINT AVERAGE (Level 1) Print a heading IF countOfValues isn't 0 Print sumOfValues / countOfValues ELSE Print an error message the following is an appropriate module precondition: "sumOfValues is assigned AND countOfValues does not equal 0." 16. Which of the following does not constitute a logical (Boolean) expression? A) an arithmetic expression followed by a relational operator followed by an arithmetic expression B) an arithmetic expression followed by a logical operator followed by an arithmetic expression C) a Boolean variable or constant D) a logical expression followed by a binary logical operator followed by a logical expression E) a unary logical operator followed by a logical expression 17. Which of the following is not a C++ relational operator? A) == B) < C) != D) && E) >= 18. If p is a Boolean variable, which of the following logical expressions always has the value false? A) p && p B) p || p C) p && !p D) p || !p E) b and d above 19. If DeMorgan's Law is used to negate the expression (i < j) && (k == l) then the result is: A) (i < j) || (k == l) B) (i > j) && (k != l) C) (i >= j) || (k != l) D) (i > j) || (k != l) E) (i >= j) && (k != l) 20. Given a Boolean variable isEmpty, which of the following is a valid C++ assignment statement? A) isEmpty = true; B) isEmpty = !isEmpty; C) isEmpty = m > n; D) a and b above E) a, b, and c above 21. Which logical operator (op) is defined by the following table? (T and F denote TRUE and FALSE.) P Q P op Q -----------T T T T F F F T F F F F A) NOT B) AND C) OR D) none of the above 22. Which C++ logical expression correctly determines whether the value of beta lies between 0 and 100? A) 0 < beta < 100 B) 0 < beta && beta < 100 C) (0 < beta) && (beta < 100) D) b and c above E) a, b, and c above 23. This question is about short-circuit evaluation of logical expressions. Consider the following expression in some imaginary programming language (not C++): (N > 5) AND (K / N < 12) If N equals 0 when this expression is evaluated, which of the following statements about the expression is true? A) It causes a divide-by-zero error only if the language uses short-circuit evaluation. B) It causes a divide-by-zero error only if the language does not use shortcircuit evaluation. C) It causes a divide-by-zero error whether or not the language uses shortcircuit evaluation. D) It never causes a divide-by-zero error. 24. If the int variables i, j, and k contain the values 10, 3, and 20, respectively, what is the value of the following logical expression: j < 4 || j == 5 && i <= k A) 3 B) false C) 20 D) true 25. After execution of the following code, what will be the value of angle if the input value is 10? n >> angle; if (angle > 5) angle = angle + 5; else if (angle > 2) angle = angle + 10; A) 0 B) 5 C) 10 D) 15 E) 25 26. After execution of the following code, what will be the value of angle if the input value is 10? cin >> angle; if (angle > 5) angle = angle + 5; if (angle > 2) angle = angle + 10; A) 0 B) 5 C) 10 D) 15 E) 25 27. After execution of the following code, what will be the value of angle if the input value is 0? cin >> angle; if (angle > 5) angle = angle + 5; else if (angle > 2) angle = angle + 10; else angle = angle + 15; A) 0 B) 5 C) 10 D) 15 E) 25 28. After execution of the following code, what will be the value of angle if the input value is 0? cin >> angle; if (angle > 5) angle = angle + 5; else if (angle > 2) angle = angle + 10; A) 0 B) 5 C) 10 D) 15 E) 25 29. What is the output of the following C++ code fragment? (Be careful here.) int1 = 120; cin >> int2; // Assume user types 30 if ((int1 > 100) && (int2 = 50)) int3 = int1 + int2; else int3 = int1 - int2; cout << int1 << ' ' << int2 << ' ' << int3; A) 120 30 150 B) 120 30 90 C) 120 50 170 D) 120 50 70 E) 120 30 70 30. Consider the following If statement, which is syntactically correct but uses poor style and indentation: if (x >= y) if (y > 0) x = x * y; else if (y < 4) x = x y; Assume that x and y are int variables containing the values 9 and 3, respectively, before execution of the above statement. After execution of the statement, what value will x contain? A) 9 B) 1 C) 6 D) 27 E) none of the above 31. Consider the following If statement, which is syntactically correct but uses poor style and indentation: if (x >= y) if (y > 0) x = x * y; else if (y < 4) x = x y; Assume that x and y are int variables containing the values 3 and 9, respectively, before execution of the above statement. After execution of the statement, what value will x contain? A) 9 B) -6 C) 6 D) 27 E) none of the above 32. What is the output of the following code fragment if the input value is 20? (Be careful here.) cin >> someInt; if (someInt > 30) cout << "Moe "; cout << "Larry "; cout << "Curly"; A) Curly B) Moe Larry Curly C) Larry Curly D) no output; there is a compile-time error E) no output; there is a run-time error 33. Assuming alpha and beta are int variables, what is the output of the following code (which is indented poorly)? alpha = 3; beta = 2; if (alpha < 2) if (beta == 3) cout << "Hello"; else cout << "There"; A) Nothing is output. B) Hello C) There D) HelloThere 34. What does the following statement print? (All variables are of type int.) if (j < k) if (k < j) cout << 1; else cout << 2; else if (j < k) cout << 3; else cout << 4; A) It prints nothing unless j equals k. B) It always prints 4. C) It prints 2 if j equals k and 4 otherwise. D) It prints 2 if j < k and 1 if k <= j. E) It prints 2 if j < k and 4 otherwise. 35. Given the following code: string name1; string name2; name1 = "Mark"; name2 = "Mary"; what is the value of the relational expression string1 < string2 ? A) true B) false C) none; it causes a compile-time error D) none; it causes a run-time error 36. Given the following code: string name1; string name2; name1 = "Maryanne"; name2 = "Mary"; what is the value of the relational expression string1 <= string2 ? A) true B) false C) none; it causes a compile-time error D) none; it causes a run-time error 37. What is the missing If condition in the following code fragment? The program is supposed to halt if the input file does not exist. ifstream inFile; inFile.open("myfile.dat"); if ( ) { cout << "Cannot open input file." << endl; return 1; } A) inFile B) myfile.dat C) !inFile D) !myfile.dat E) inFile != myfile.dat 38. The phrase "minimum complete coverage" means that we test a program with A) at least one set of data. B) data that executes every branch at least once. C) data that executes all possible combinations of branches at least once. D) all possible data values. 39. In order to test the boundaries of the following condition, what data values would you use for the variable alpha? (alpha is of type int.) alpha >= 1 A) 0, 1, and INT_MAX B) 1, 2, and INT_MAX C) INT_MIN, 1, and INT_MAX D) INT_MIN, 0, 1, and INT_MAX E) INT_MIN, 1, 2, and INT_MAX 40. A(n) ____________________ expression is an expression composed of logical (Boolean) values and operations. 41. The operators <, ==, >=, and != are examples of ____________________ operators. (Do not answer "binary.") 42. In programming languages that use ____________________ evaluation of a logical expression, evaluation proceeds in left-to-right order and stops as soon as the final truth value can be determined. 43. The operators &&, ||, and ! are known as ____________________ operators. 44. In programming languages that use ____________________ evaluation of a logical expression, all subexpressions are evaluated before applying any logical operators. 45. Write a C++ logical expression that is true if the variable testScore is greater than or equal to 90 and less than or equal to 100: ____________________ 46. Write a C++ logical expression that is false if either x or y is equal to 5: ____________________ 47. The order in which the computer executes statements in a program is called the ____________________. 48. An If statement is an example of a(n) ____________________ control structure. 49. An If-Then-Else-If structure represents a(n) ____________________-way branch. 50. The act of using a C++ stream object in a logical expression as if it were a Boolean variable is called ____________________ of the stream. 51. Any statement used to alter the normally sequential flow of control is called a(n) ____________________. 52. A(n) ____________________ is an assertion that should be true after a module has finished executing. 53. After the code is written in the implementation phase, you should go over it line by line. This process is known as a(n) ____________________. 54. A(n) ____________________ is an assertion that must be true before a module begins executing. 55. Taking some actual values and hand-calculating what the output of a program should be is called a(n) ____________________. 56. Two approaches to program testing are ____________________ coverage and data coverage. 57. A(n) ____________________ is the process of going through the steps of an algorithm to confirm that they produce the required postcondition, given the stated precondition. 58. Define flow of control. 59. What is a control structure? 60. What is the purpose of a relational operator? 61. How are relational operators used in C++? 62. Distinguish between short-circuit evaluation and full evaluation. Which does C++ use? 63. What is the result type of a relational expression? 64. Where does the name "bool" come from? 65. Write the statement that declares a boolean variable stop. 66. Write the statement that declares a boolean variable stop and assigns it the value of one<two. 67. Write a statement that tests to see if data1 is greater than data2 and stores the result in boolean variable answer. 68. What are the two values that a boolean variable can have? 69. Why is it dangerous to compare floating-point numbers for equality? 70. What is an equivalent expression for !(one == two && three > four) 71. Write a statement that stores a 0 in answer if one is greater than two. 72. Write a statement that stores 100 in answer if grade is greater than 100. 73. Write the statement that sets dataOk to True if data is less than or equal to 100. 74. If you want to have more than one statement executed within an If statement, what syntax do you use? 75. What is a compound statement? 76. What is the safest way to compare data types such as ints, floats, and chars? 77. When placing an If statement within and If statement, a computer programmer is creating a ___________. 78. True or False? The following represents a list showing the correct order of precedence (from highest to lowest) for the arithmetic, relational, and logical operators with the assignment operator included as well ! Unary + Unary – /% + < <= > >= == != && || = 79. What does the following If-Then C ++ statement mean? if (age < 18) cout << “Not an “voter.” ,, endl; eligible “: cout << 80. True or False? Implementing a test plan guarantees that a program is completely correct. Answer Key 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. True False False True False False False False False True True False False True False B D C C E B D B D D E D A C D E C A E A B C B D logical (Boolean) relational short-circuit (conditional) logical (Boolean) full 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. testScore >=90 && testScore <= 100 x != 5 && y != 5 flow of control selection (branching) multi testing the state control structure postcondition code walk-through precondition execution trace code walk-through (algorithm walk-through) The order in which the computer executes statements in a program A statement used to alter the normally sequential flow of control A relational operator tests the relationship between two values. In C++, a relational expression is created by placing a relational operator between two values. If the two values are in the relationship described by the operator, the expression returns True; otherwise the expression returns False. In full evaluation, expressions are evaluated in left-to-right order until the entire expression has been evaluated. In short-circuit evaluation, evaluation stops as soon as the truth of the expression can be determined. C++ uses short uses short-circuit evaluation. bool George Boole, a 19th century mathematician described a system of logic using variables with just two values, True and False. The bool data type is named for him. bool stop; bool stop = one<two; answer = data1 > data2; True and False When floating-point numbers are involved in calculations, errors occur in the rightmost decimal places because the representation of a real number in a computer is not always exact. one != two || three <= four if (one > two) answer = 0; if (grade > 100) grade = 100; if (data <= 100) dataOk = True; or dataOk = (data <= 100); (preferred way) 74. Enclose the statements within braces. 75. Statements enclosed within braces that are treated as one statement 76. The safest approach is always to compare ints with ints, floats with floats, chars with chars and so on. If one mixes data types in a comparison, implicit type coercion takes place, similar to how it does in arithmetic expressions. 77. nested control structure 78. True 79. If age is less than 18, first print “Not an eligible” and then print “voter.” If age is not less than 18, then skip the first output statement and go directly to “voter.” 80. False

Download
# Chapter 5 Study Questions and Answer Key

homework writing help

for a fair price!

check it here!