C++ Chpt_04

¡Supera tus tareas y exámenes ahora con Quizwiz!

9. The '!' logical operator has higher precedence than the other logical operators.

!

21. Convert the following conditional expression into an if/else statement. q = (x < y) ? (a + b) : (x * 2);

q = (x < y) ? (a + b) : (x * 2); if (x < y) q = (a + b); else q = x * 2;

14. The expression that follows the switch statement must have a(n) 'Integer' value.

Integer

11. The '&&' logical operator works best when testing a number to determine if it is within a range.

&&

4. For an if statement to conditionally execute a group of statements, the statements must be enclosed in a set of .

'Braces'

1. An expression using the greater-than, less-than, greater-than-or-equal-to, less-than-or equal-to, equal, or not-equal operator is called a(n)

'Relational expression.

24. Assume the variables x = 5, y = 6, and z = 8. Indicate if each of the following conditions is true or false:

A) (x >= 0) || (x <= y) B) (z - y) > y C) !((z - y) > x)

36. Sometimes either a switch statement or an if/else if statement can be used to implement logic that requires branching to different blocks of program code. But the two are not interchangeable.

A) Under what circumstances would an if/else if statement be a more appropriate choice than a switch statement? B) Under what circumstances would a switch statement be a more appropriate choice than an if/else if statement? C) Under what circumstances would a set of nested if/else statements be more appropriate than either of the other two structures? Try to come up with at least one example case for each of the three, where it is the best way to implement the desired branching logic.

35. Each of the following program segments has errors. Find as many as you can.

A) cout << "Enter your 3 test scores and I will "; << "average them:"; int score1, score2, score3, cin >> score1 >> score2 >> score3; double average; average = (score1 + score2 + score3) / 3.0; if (average = 100); perfectScore = true;// Set the flag variable cout << "Your average is " << average << endl; bool perfectScore; if (perfectScore); { cout << "Congratulations!\n"; cout << "That's a perfect score.\n"; cout << "You deserve a pat on the back!\n"; B) double num1, num2, quotient; cout << "Enter a number: "; cin >> num1; cout << "Enter another number: "; cin >> num2; if (num2 == 0) cout << "Division by zero is not possible.\n"; cout << "Please run the program again "; cout << "and enter a number besides zero.\n"; else quotient = num1 / num2; cout << "The quotient of " << num1 << cout << " divided by " << num2 << " is "; cout << quotient << endl; C) int testScore; cout << "Enter your test score and I will tell you\n"; cout << "the letter grade you earned: "; cin >> testScore; if (testScore < 60) cout << "Your grade is F.\n"; else if (testScore < 70) cout << "Your grade is D.\n"; else if (testScore < 80) cout << "Your grade is C.\n"; else if (testScore < 90) cout << "Your grade is B.\n"; else cout << "That is not a valid score.\n"; else if (testScore <= 100) cout << "Your grade is A.\n"; D) double testScore; cout << "Enter your test score and I will tell you\n"; cout << "the letter grade you earned: "; cin >> testScore; switch (testScore) { case (testScore < 60.0): cout << "Your grade is F.\n"; break; case (testScore < 70.0): cout << "Your grade is D.\n"; break; case (testScore < 80.0): cout << "Your grade is C.\n"; break; case (testScore < 90.0): cout << "Your grade is B.\n"; break; case (testScore <= 100.0): cout << "Your grade is A.\n"; break; default: cout << "That score isn't valid\n"; }

16. What value will be stored in the variable t after each of the following statements executes?

A) t = (12 > 1); '1' B) t = (2 < 0); '0' C) t = (5 == (3 * 2)); '0' D) t = (5 == 5); '1'

30. Write one or more C++ statements that assign the correct value to discount, using the logic described here:

Assign .20 to discount if dept equals 5 and price is $100 or more. Assign .15 to discount if dept is anything else and price is $100 or more. Assign .10 to discount if dept equals 5 and price is less than $100. Assign .05 to discount if dept is anything else and price is less than $100.

13. A variable with " "(or local)' scope is only visible when the program is executing in the block containing the variable's definition.

Block

15. A program will "fall through" to the following case section if it is missing the 'Break' statement.

Break

6. The trailing else in an if/else if statement has a similar purpose as the ' ' section of a switch statement.

Default

7. If the sub-expression on the left of the && logical operator is 'False', the right sub-expression is not checked.

False

3. The if statement regards an expression with the value 0 as 'False' and an expression with a non zero value as 'True'.

False True

10. Logical operators have ' ' precedence than relational operators.

Lower

23. Assume the variables x = 5, y = 6, and z = 8. Indicate if each of the following conditions is true or false: A) (x == 5) || (y > 3) B) (7 <= x) && (z > 4) C) (2 != y) && (z != 4)

T F T

8. If the sub-expression on the left of the || logical operator is 'True', the right sub-expression is not checked.

True

5. In an if/else statement, the if part executes its statement(s) if the expression is 'True', and the else part executes its statement(s) if the expression is 'False'.

True False

2. The value of a relational expression is 0 if the expression is

expression is 'False'. 'True' or 1 if the

31. The following statement should determine if x is not greater than 20. What is wrong with it?

if (!x > 20) should read: if (!(x > 20))

22. Convert the following if/else if statement into a switch statement:

if (choice == 1) { cout << fixed << showpoint << setprecision(2); } else if ((choice == 2) || (choice == 3)) { cout << fixed << showpoint << setprecision(4); } else if (choice == 4) { cout << fixed << showpoint << setprecision(6); } else { cout << fixed << showpoint << setprecision(8); }

33. The following statement should determine if count is outside the range of 0 through 100. What is wrong with it?

if (count < 0 && count > 100) should read if (count < 0 || count > 100)

32. The following statement should determine if count is within the range of 0 through 100. What is wrong with it?

if (count >= 0 || count <= 100) should read: if (count >= 0 || count <= 100)?

25. Write a C++ statement that prints the message "The number is valid." if the variable grade is within the range 0 through 100.

if (grade >= 0 && grade <= 100) cout << "The number is valid.";

27. Write a C++ statement that prints the message "The number is not valid." if the variable hours is outside the range 0 through 80.

if (hours < 0 || hours > 80) cout << "The number is not valid.";

29. Using the following chart , write a C++ statement that assigns .10, .15, or .20 to commission, depending on the value in sales.

if (sales < 10000) commission = .10; else if (sales <= 15000) commission = .15; else commission = .20;

19. Write an if/else statement that prints "Excellent" when score is 90 or higher, "Good" when score is between 80 and 89, and "Try Harder" when score is less than 80.

if (score >= 90) cout << "Excellent"; else if (score >=80) cout << "Good" else cout << "Try Harder";

17. Write an if statement that assigns 100 to x when y is equal to 0.

if (y==0) x = 100;

12. The '||' logical operator works best when testing a number to determine if it is outside a range.

||


Conjuntos de estudio relacionados

English - Part V- Short Answer Questions

View Set

Chapter 7: Corporate Debt Instruments

View Set

Module 4: Venture Growth, Scaling Scalability

View Set

Chapter 62: Management of Patients with Burn Injury

View Set

Chapter 5, FIN 269 Personal Finance chapter 4

View Set