Chapter 04 Flashcards

1
Q
  1. Write an if statement that assigns 100 to x when y is equal to 0.
A

if ( y== 0)

x = 100;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
  1. Write nested if statements that perform the following tests: If amount1 is greater
    than 10 and amount2 is less than 100, display the greater of the two.
A

if (amount1 > 10)
if (amount2 < 100)
cout &laquo_space;(amount1 > amount2 ? amount1 : amount2);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
  1. Write an if statement that prints the message The number is valid if the variable
    temperature is within the range -50 through 150.
A

if (temperature >=-50 && temperature &laquo_space;“The number is valid”;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  1. Assume str1 and str2 are string objects that have been initialized with different
    values. Write an if/else statement that compares the two objects and displays the
    one that is alphabetically greatest.
A

if (str1 > str2)

cout

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
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

false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
  1. T F All lines in a block should be indented one level.
A

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 if part of another statement, the only
    time the inner if is executed is when the expression of the outer if is true.
A

true

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

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
  1. T F x != y is the same as (x > y || x < y)
A

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

false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
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 4
56. T F 2 != y && z != 4
57. T F x >= 0 || x

A
  1. T
  2. F
  3. T
  4. T
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
  1. The following statement should determine if count is within the range of 0 through
  2. What is wrong with it?
    if (count >= 0 || count <= 100)
A
63. It should use
&&
instead of
||
.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
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
65. The
\:
and
?
are transposed. The statement should read:
z = (a < 10) ? 0 : 7;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
  1. Describe the difference between the if/else if statement and a series of if
    statements.
A
1. 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
  1. In an if/else if statement, what is the purpose of a trailing else?
A

executed when all the preceding conditional expressions are false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
  1. What is an flag and how does it work?
A
  1. 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q
  1. Can an if statement test expressions other than relational expressions? Explain.
A

Yes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q
  1. Briefly describe how the && operator works.
A
  1. It takes two expressions as operands and creates a single expression that is true only when
    both subexpressions are true.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q
  1. Briefly describe how the || operator works.
A
  1. It takes two expressions as operands and creates a single expression that is true when either of the subexpressions are true.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q
  1. Why are the relational operators called relational?
A
  1. 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q
  1. Why do most programmers indent the conditionally executed statements in a decision
    structure?
A

because they need to execute based on the condition evaluation either executes true or false block.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q
  1. An expression using the greater-than, less-than, greater-than-or-equal to, less-than-orequal-
    to, equal, or not-equal to operator is called a(n) __________ expression.
A

relational

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q
  1. A relational expression is either __________ or __________.
A

true or false

24
Q
  1. The value of a relational expression is 0 if the expression is __________ or 1 if the
    expression is __________.
A

false true

25
Q
  1. The if statement regards an expression with the value 0 as __________.
A

false

26
Q
  1. The if statement regards an expression with a nonzero value as __________.
A

true

27
Q
  1. For an if statement to conditionally execute a group of statements, the statements
    must be enclosed in a set of __________.
A

braces

28
Q
  1. 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
    __________.
A

true/false

29
Q
  1. The trailing else in an if/else if statement has a similar purpose as the
    __________ section of a switch statement.
A

default

30
Q
  1. The if/else if statement is actually a form of the __________ if statement.
A

nested

31
Q
  1. If the sub-expression on the left of the __________ logical operator is false, the right
    sub-expression is not checked.
A

&&

32
Q
  1. If the sub-expression on the left of the __________ logical operator is true, the right
    sub-expression is not checked.
A

||

33
Q
  1. The __________ logical operator has higher precedence than the other logical operators.
A

!

34
Q
  1. The logical operators have __________ associativity.
A

left to right

35
Q
  1. The __________ logical operator works best when testing a number to determine if it
    is within a range.
A

&&

36
Q
  1. The __________ logical operator works best when testing a number to determine if it
    is outside a range.
A

||

37
Q
  1. A variable with __________ scope is only visible when the program is executing in the
    block containing the variable’s definition.
A

local

38
Q
  1. You use the __________ operator to determine whether one string object is greater
    then another string object.
A

>

39
Q
  1. An expression using the conditional operator is called a(n) __________ expression.
A

conditional

40
Q
  1. The expression that is tested by a switch statement must have a(n) __________ value.
A

integer

41
Q
  1. The expression following a case statement must be a(n) __________ __________.
A

integer literal or constant

42
Q
  1. A program will fall through a case section if it is missing the __________ statement.
A

break

43
Q
  1. What value will be stored in the variable t after each of the following statements executes?
    A) t = (12 > 1);__________
    B) t = (2
A

1
0
0
1

44
Q
  1. Write an if/else statement that assigns 0 to x when y is equal to 10. Otherwise it
    should assign 1 to x.
A

if (y==0)
x=0;
else
x=1;

45
Q
  1. Write an if statement that sets the variable hours to 10 when the flag variable
    minimum is set.
A

if (minimum)

hours = 10

46
Q
  1. Write an if statement that prints the message The number is valid if the variable
    grade is within the range 0 through 100.
A

if (grade >= 0 && grade &laquo_space;“The number is valid.”;

47
Q
  1. Write an if statement that prints the message The number is not valid if the variable
    hours is outside the range 0 through 80.
A

if (hours < 0 || hours >80 )

cout &laquo_space;“The number is not valid.”;

48
Q
  1. Convert the following if/else if statement into a switch statement:
    if (choice == 1)
    {
    cout
A

switch (choice)
{
case 1: cout

49
Q
  1. T F The = operator and the == operator perform the same operation when used in
    a Boolean expression.
A

F

50
Q
  1. T F A conditionally executed statement should be indented one level from the if
    statement.
A

T

51
Q
  1. T F It s safe to assume that all uninitialized variables automatically start with 0 as
    their value.
A

F, un initialized variable are having garbage values in it.

52
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

F

53
Q
  1. T F You can use the relational operators to compare string objects.
A

T

54
Q
  1. T F y < x is the same as x >= y
A

F, x is greater than y but may not equal to y

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

if (!(x>20))

56
Q
  1. The following statement should determine if count is outside the range of 0 through
  2. What is wrong with it?
    if (count < 0 && count > 100)
A

||