CHAPTER 4 (42-65) REVIEW QUESTIONS Flashcards

1
Q
  1. T F The = operator and the = = operator perform the same operation when used in a Boolean expression.
A
  1. false
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
  1. T F A variable defined in an inner block may not have the same name as a variable defined in the outer block.
A
  1. false
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
  1. T F A conditionally executed statement should be indented one level from the if statement.
A
  1. true
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  1. T F All lines in a block should be indented one level.
A
  1. true
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
  1. T F It’s safe to assume that all uninitialized variables automatically start with 0 as their value.
A
  1. false
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
  1. 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.
A
  1. true
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
  1. 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.
A
  1. false
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
  1. T F The scope of a variable is limited to the block in which it is defined.
A
  1. true
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
  1. T F You can use the relational operators to compare string objects.
A
  1. true
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
  1. T F x != y is the same as (x > y || x < y)
A
  1. true
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
  1. T F y < x is the same as x >= y
A
  1. false
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
  1. T F x >= y is the same as (x > y && x = y)
A
  1. false
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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

A
  1. T
  2. F
  3. T
  4. T
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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>

A
  1. 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
  1. // 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>
A
  1. The conditionally executed blocks in the if/else construct should be enclosed in braces.

The following statement:
cout &laquo_space;“The quotient of “ &laquo_space;num1 «
should read:
cout &laquo_space;“quotient of “ &laquo_space;num1;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
  1. // 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>
A
  1. The trailing else statement should come at the end of the if/else construct.
17
Q
  1. // 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
  1. A switch statement cannot be used to test relational expressions. An if/else if statement should be used instead.
18
Q
  1. The following statement should determine if x is not greater than 20. What is wrong with it?
    if (!x > 20)
A
  1. It should read if (!(x > 20))
19
Q
  1. The following statement should determine if count is within the range of 0 through

If(count >=0 && count <=100)

A
  1. It should use && instead of ||.
20
Q
  1. The following statement should determine if count is outside the range of 0 through

What is wrong with it?
if (count < 0 && count > 100)

A
  1. It should read if (count < 0 || count > 100)
21
Q
  1. 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;
A
  1. The : and ? are transposed. The statement should read:
    z = (a < 10) ? 0 : 7;