Chapter 4 - Review Questions

Ace your homework & exams now with Quizwiz!

!

The _______ logical operator has higher precedence tan the other logical operators.

integer

The expression that is tested by a switch statement must have a(n) _______ value.

conditional

An expression using the conditional operator is called a(n) _______ expression.

braces

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

integer constant

The expression following a case statement must be a(n) _______ _______.

true

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, false

In an if/else statement, the if part executes its statement or block if the expression is _______, and the else part executes its statement or block if the expression is _______.

in an if/else if statement, the conditions are tested until one is found to be true. The conditionally executed statement(s) are executed and the program exits the if/else if statement. In a series of if statements, all of the if statements execute and test their conditions because they are not connected

Describe the difference between the if/else if statement and a series of if statements.

&&

If the sub-expression on the left of the _______ logical operator is false, the right sub-expression is not checked.

||

If the sub-expression on the left of the _______ logical operator is true, the right sub-expression is not checked.

the trailing else provides code that is executed when none of the conditions in the if/else if statement are true

In an if/else if statement, what is the purpose of a trailing else?

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

The following statement should determine if x is not greater than 20. What is wrong with it? if (!x > 20)

true

The if statement regards an expression with a nonzero value as _______.

false

The if statement regards an expression with the value 0 as _______.

nested

The if/else if statement is actually a form of the _______ if statement.

left-to-right

The logical operators have _______ associativity.

true

The scope of a variable is limited to the block in which it is defined.

default

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

false, true

The value of a relational expression is 0 if the expression is _______ or 1 if the expression is _______.

the trailing else statement should come at the end of the if/else construct

/ 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; }

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 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; }

The conditionally executed blocks in the if/else construct should be enclosed in braces. The following statement: cout << "The quotient of " << num1 << should read: cout << "quotient of " << num1;

// 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; }

a switch statement cannot be used to test relational expressions. An if/else if statement should be used instead

// 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; }

true

A conditionally executed statement should be indented one level from the if statement.

break

A program will "fall through" a case section if it is missing the _______ statement.

true, false

A relational expression is either _______ or _______.

false

A variable defined in an inner block may not have the same name as a variable defined in the outer block.

block

A variable with _______ scope is only visible when the program is executing in the block containing the variable's definition.

true

All lines in a block should be indented one level.

relational

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

true

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: 2 != y && z != 4

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: 7 <= x && z > 4

true

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: x == 5 || y > 3

true

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: x >= 0 || x <= y

it takes two expressions as operands and creates a single expression that is true only when both subexpressions are true

Briefly describe how the && operator works.

it takes two expressions as operands and creates a single expression that is true when either of the subexpressions are true

Briefly describe how the || operator works.

yes. The if statement can test any value that yields a Boolean value (true or false) or a numeric value. When testing a numeric expression, a nonzero numeric value is considered true, and the value 0 is considered false

Can an if statement test expressions other than relational expressions?

false

It's safe to assume that all uninitialized variables automatically start with 0 as their value.

false

The = operator and the = = operator perform the same operation when used in a Boolean expression.

&&

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

||

The _______ logical operator works best when testing a number to determine if it outside the range.

The : and ? are transposed. The statement should read: z = (a < 10) ? 0 : 7;

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;

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

The following statement should determine if count is outside the range of 0 through100. What is wrong with it? if (count < 0 && count > 100)

it should use && instead of ||

The following statement should determine if count is within the range of 0 through100. What is wrong with it? if (count >= 0 || count <= 100)

a flag is a boolean variable signaling that some condition exists in the program. When the flag is set to false it indicates the condition does not yet exist. When the flag is set to true it indicates that the condition does exist

What is a flag and how does it work?

1

What value will be stored in the variable t after each of the following statements executes? t = (12 > 1); _______

0

What value will be stored in the variable t after each of the following statements executes? t = (2 < 0); _______

0

What value will be stored in the variable t after each of the following statements executes? t = (5 == (3 * 2)); _______

1

What value will be stored in the variable t after each of the following statements executes? t = (5 == 5); _______

false

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.

because they test for specific relationships between items. The relationships are greater-than, less-than, equal-to, greater-than or equal-to, less-than or equal-to, and not equal-to

Why are the relational operators called relational?

it visually sets the conditionally-executed statements apart from the surrounding code. This is so you can easily identify the code that is conditionally-executed

Why do most programmers indent the conditionally executed statements in a decision structure?

true

You can use the relational operators to compare string objects.

>

You use the _______ operator to determine whether one string object is greater then another string object.`

true

x != y is the same as (x > y || x < y)

false

x >= y is the same as (x > y && x = y)

false

y < x is the same as x >= y


Related study sets

Substance Abuse and Abuse NCLEX Practice Quiz: 75 Questions

View Set

Marketing exam 2 (chapters 7-12)

View Set

kin-69: stress management final review

View Set

Chapter 46: Caring for Clients with Disorders of the Lower Gastrointestinal Tract (NCLEX Review Questions/PrepU)

View Set