CHAPTER 4 (42-65) REVIEW QUESTIONS Flashcards
- T F The = operator and the = = operator perform the same operation when used in a Boolean expression.
- false
- T F A variable defined in an inner block may not have the same name as a variable defined in the outer block.
- false
- T F A conditionally executed statement should be indented one level from the if statement.
- true
- T F All lines in a block should be indented one level.
- true
- T F It’s safe to assume that all uninitialized variables automatically start with 0 as their value.
- false
- T F When an if statement is nested in the if part of another statement, the only time the inner if is executed is when the expression of the outer if is true.
- true
- T F When an if statement is nested in the else part of another statement, as in an if/else if , the only time the inner if is executed is when the expression of the outer if is true.
- false
- T F The scope of a variable is limited to the block in which it is defined.
- true
- T F You can use the relational operators to compare string objects.
- true
- T F x != y is the same as (x > y || x < y)
- true
- T F y < x is the same as x >= y
- false
- T F x >= y is the same as (x > y && x = y)
- false
Assume the variables x = 5 , y = 6 , and z = 8 . Indicate by circling the T or F whether each of the following conditions is true or false:
54. T F x == 5 || y > 3
55. T F 7 <= x && z > 4
56. T F 2 != y && z != 4
57. T F x >= 0 || x <= y
- T
- F
- T
- T
Find the Errors, Each of the following programs has errors. Find as many as you can.
58. // This program averages 3 test scores.
include <iostream>
using namespace std;
int main()
{
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";
return 0;
}</iostream>
- The first cout statement is terminated by a semicolon too early.
The definition of score1, score2, and score3 should end with a semicolon.
The following statement:
if (average = 100)
should read:
if (average == 100)
perfectScore is used before it is defined.
The following if statement should not be terminated with a semicolon:
if (perfectScore);
The conditionally executed block in this if statement should end with a closing brace.
- // This program divides a user-supplied number by another
// user-supplied number. It checks for division by zero.
#include <iostream>
using namespace std;
int main()
{
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;
return 0;
}</iostream>
- The conditionally executed blocks in the if/else construct should be enclosed in braces.
The following statement:
cout «_space;“The quotient of “ «_space;num1 «
should read:
cout «_space;“quotient of “ «_space;num1;
- // This program uses an if/else if statement to assign a
// letter grade (A, B, C, D, or F) to a numeric test score.
#include <iostream>
using namespace std;
int main()
{
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";
return 0;
}</iostream>
- The trailing else statement should come at the end of the if/else construct.
- // This program uses a switch-case statement to assign a
// letter grade (A, B, C, D, or F) to a numeric test score.
#include <iostream>
using namespace std;
int main()
{
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";
return 0;
}</iostream>
- A switch statement cannot be used to test relational expressions. An if/else if statement should be used instead.
- The following statement should determine if x is not greater than 20. What is wrong with it?
if (!x > 20)
- It should read if (!(x > 20))
- The following statement should determine if count is within the range of 0 through
If(count >=0 && count <=100)
- It should use && instead of ||.
- The following statement should determine if count is outside the range of 0 through
What is wrong with it?
if (count < 0 && count > 100)
- It should read if (count < 0 || count > 100)
- The following statement should assign 0 to z if a is less than 10, otherwise it should assign 7 to z . What is wrong with it?
z = (a < 10) : 0 ? 7;
- The : and ? are transposed. The statement should read:
z = (a < 10) ? 0 : 7;