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;