Deck 2: Flow of Control

Full screen (f)
exit full mode
Question
Write multiway if-else statements for which the output is "Alarm: Boiler Pressure: TOO HIGH" if the value of the variable boiler_pressure is greater than 1000 (psi),and the output is "Boiler Pressure: TOO LOW" if the value of boiler_pressure is below 100(psi),otherwise the output is "Boiler Pressure: within normal limits."
Use Space or
up arrow
down arrow
to flip the card.
Question
In a do-while loop,a continue statement terminates the loop.
Question
In a while loop,the Boolean_Expression is executed before each execution of the loop body.
Question
Write Boolean expressions that represent the given English expressions.Assume any variables used have been declared and initialized.
a)alpha is greater than 1
b)x is odd
c)x and y are odd
d)ch is an upper case alphabetic character (between 'A' and 'Z').
e)digit,which is f type char,has value that is indeed a digit.
Question
A break statement is used in loops only.
Question
A numeric integer grade is between 50 and 99.Using integer division or otherwise obtain a int value that is 5,6,7,8,or 9 from the numeric grade.Write code using a switch statement using this number in the selector expression that assigns letter grades based on this 10 point scheme:
if the numeric_grade is not less than 90,the letter_grade is an A,
if the numeric_grade is not less than 80,the letter_grade is a B,
if the numeric_grade is not less than 70,the letter_grade is C,
if the numeric_grade is not less than 60,the letter_grade is D,
otherwise the letter_grade is F.
Question
The if,while and for statements control only one statement.
Question
Assume variables first,second,and max are declared to be double and are initialized.Write a sequence of lines of code that cause the larger of the values in first and second to be stored in max.
Question
Assume variables first and second are declared to be double and are initialized.Write a sequence of lines of code that cause the values stored in first and second to be exchanged if the value of first is not less than second.
Question
Write multiway if-else statements in which letter grades are assigned based a numeric grade based on this "ten point" scheme:
if the numeric grade is not less than 90,the letter grade is an A,
if the numeric grade is not less than 80,the letter grade is a B,
if the numeric grade is not less than 70,the letter grade is C,
if the numeric grade is not less than 60,the letter grade is D,
otherwise the letter grade is F.
Question
The value of count is 0;limit is 10.Evaluate:
(count == 0)&&(limit < 20)
Question
Suppose we have these declarations,
int x = -1,y = 0,z = 1;
This Boolean expression is correct and it does what the programmer intends.
x < y < z
Question
Given the declaration
int x = 0;
The following expression causes a divide by zero error:
(x !=0)|| (2/x < 1);
Question
Write Boolean expressions that represent the given English expressions.Assume any variables used have been declared and initialized.
a)at least one of x or y is odd
b)at least one of x or y is non-negative (x is non-negative is written x >= 0)
c)The hit is no more than 2.5 units away from target
d)x is 1 or x is equal to y
e)t is strictly between 3.2 and 3.3
Question
You want to determine whether time has run out.The following code correctly implements this.
!time > limit
Question
When a loop is nested in side another loop,a break or continue statement terminates or restarts the outermost loop of the nested loop structure.
Question
The value of count is 0;limit is 10.Evaluate:
count == 0 && limit < 20
Question
Use the condition operator (x?y:z)to write a very compact expression that assigns the maximum of variables n1 and n2 to the variable max.You should assume that any variables you use have been declared and initialized appropriately.
Question
Explain the programmer's saying from the text,section 2.2,"Garbage in means garbage out."
Question
The value of count is 0;limit is 10.Evaluate:
(count != 0)||(limit < 20)
Question
A switch statement must have

A)a default case
B)more than one non-default case
C)a break statement
D)none of the above
E)all of the above
Question
If the following code fragment is executed in an otherwise complete and correct program,which expression will be executed? Why? x = 0;
If (x = 12)
Yes_statement;
Else
No_statement;

A)The no_statement will be executed because x is not 12.
B)The statement has incorrect syntax so will not compile at all.
C)x=12 is illegal in the Boolean expression of an if statement.
D)The yes_statement will be executed.
Question
Write the following do-while statement with a while construct,and maybe some extra code.
x = 10;
do
{
cout << x << endl;
x = x - 3;
} while ( x > 0 );
Question
In the expression (j > 0 && j+1 == 10),which operator executes last?

A)>
B)&&
C)+
D)==
Question
For each of the following situations,tell which type loop (while,do-while,or for)would be best in that situation:
a)Reading a list of an unknown number of homework grades for a single student.
b)Summing a series such as 1 +1/2 +1/(22)+ 1/(23)+ … + 1/(28)
c)Testing a newly coded library function to see how it performs for different values of its arguments.
d)Reading in the number of days of vacation taken by an employee.
Question
Write a program that reads in and sums the squares of positive integers until a value that 0 or less is read in.
Question
Consider the if statement: if(condition)yes_clause;else no_clause;
Under which of the following circumstances will both the yes_clause and the no_clause will be executed?

A)When the condition is true.
B)When the condition is false.
C)When the condition is ambiguous.
D)This will not happen.
Question
Which of the following determines the operator that is processed prior to another operator?

A)Operator associativity
B)Operator precedence
C)Whether the operator is an arithmetic operator
D)None of these determine the order in which operators are processed.
Question
What is the output of the following,if it were embedded in an otherwise correct and complete program and run? int x = 10;
While (x > 0)
{
Cout << x << " ";
X = x + 3;
}
Cout << endl;

A)10 13 16 19 ...
B)The compiler detects that this will be an infinite loop,so it does not compile. Insert lowercase be
C)This is an infinite loop.When compiled and run,it runs until machine limitations stop it,or you get tired of it and kill the process.
D)0 3 6 9.
Question
Write a program that reads in exactly 10 integers and outputs the sum.
Question
Which of the following control structures requires curly braces?

A)if-else
B)while
C)do-while
D)switch
E)for
Question
Which of the following loop statements is guaranteed to iterate the body of the loop at least once?

A)while(control)body;
B)do body while(control);
C)for (initialize;test;update)body;
D)none of the above
E)all of the above
Question
Predict the output of the following nested loops:
int n = 1;
while(n <= 10)
{
int m = 10;
while(m>=1)
{
cout << n << " times " << m
<< " = " << n*m << endl;
m--;
}
n++;
}
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/33
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 2: Flow of Control
1
Write multiway if-else statements for which the output is "Alarm: Boiler Pressure: TOO HIGH" if the value of the variable boiler_pressure is greater than 1000 (psi),and the output is "Boiler Pressure: TOO LOW" if the value of boiler_pressure is below 100(psi),otherwise the output is "Boiler Pressure: within normal limits."
if (boiler_pressure > 1000)
cout << "Boiler Pressure: TOO LOW\n";
else if (boiler_pressure < 100)
cout << "Boiler Pressure: TOO LOW\n";
else
cout << "Boiler Pressure: within normal limits.\n";
2
In a do-while loop,a continue statement terminates the loop.
False
3
In a while loop,the Boolean_Expression is executed before each execution of the loop body.
True
4
Write Boolean expressions that represent the given English expressions.Assume any variables used have been declared and initialized.
a)alpha is greater than 1
b)x is odd
c)x and y are odd
d)ch is an upper case alphabetic character (between 'A' and 'Z').
e)digit,which is f type char,has value that is indeed a digit.
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
5
A break statement is used in loops only.
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
6
A numeric integer grade is between 50 and 99.Using integer division or otherwise obtain a int value that is 5,6,7,8,or 9 from the numeric grade.Write code using a switch statement using this number in the selector expression that assigns letter grades based on this 10 point scheme:
if the numeric_grade is not less than 90,the letter_grade is an A,
if the numeric_grade is not less than 80,the letter_grade is a B,
if the numeric_grade is not less than 70,the letter_grade is C,
if the numeric_grade is not less than 60,the letter_grade is D,
otherwise the letter_grade is F.
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
7
The if,while and for statements control only one statement.
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
8
Assume variables first,second,and max are declared to be double and are initialized.Write a sequence of lines of code that cause the larger of the values in first and second to be stored in max.
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
9
Assume variables first and second are declared to be double and are initialized.Write a sequence of lines of code that cause the values stored in first and second to be exchanged if the value of first is not less than second.
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
10
Write multiway if-else statements in which letter grades are assigned based a numeric grade based on this "ten point" scheme:
if the numeric grade is not less than 90,the letter grade is an A,
if the numeric grade is not less than 80,the letter grade is a B,
if the numeric grade is not less than 70,the letter grade is C,
if the numeric grade is not less than 60,the letter grade is D,
otherwise the letter grade is F.
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
11
The value of count is 0;limit is 10.Evaluate:
(count == 0)&&(limit < 20)
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
12
Suppose we have these declarations,
int x = -1,y = 0,z = 1;
This Boolean expression is correct and it does what the programmer intends.
x < y < z
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
13
Given the declaration
int x = 0;
The following expression causes a divide by zero error:
(x !=0)|| (2/x < 1);
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
14
Write Boolean expressions that represent the given English expressions.Assume any variables used have been declared and initialized.
a)at least one of x or y is odd
b)at least one of x or y is non-negative (x is non-negative is written x >= 0)
c)The hit is no more than 2.5 units away from target
d)x is 1 or x is equal to y
e)t is strictly between 3.2 and 3.3
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
15
You want to determine whether time has run out.The following code correctly implements this.
!time > limit
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
16
When a loop is nested in side another loop,a break or continue statement terminates or restarts the outermost loop of the nested loop structure.
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
17
The value of count is 0;limit is 10.Evaluate:
count == 0 && limit < 20
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
18
Use the condition operator (x?y:z)to write a very compact expression that assigns the maximum of variables n1 and n2 to the variable max.You should assume that any variables you use have been declared and initialized appropriately.
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
19
Explain the programmer's saying from the text,section 2.2,"Garbage in means garbage out."
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
20
The value of count is 0;limit is 10.Evaluate:
(count != 0)||(limit < 20)
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
21
A switch statement must have

A)a default case
B)more than one non-default case
C)a break statement
D)none of the above
E)all of the above
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
22
If the following code fragment is executed in an otherwise complete and correct program,which expression will be executed? Why? x = 0;
If (x = 12)
Yes_statement;
Else
No_statement;

A)The no_statement will be executed because x is not 12.
B)The statement has incorrect syntax so will not compile at all.
C)x=12 is illegal in the Boolean expression of an if statement.
D)The yes_statement will be executed.
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
23
Write the following do-while statement with a while construct,and maybe some extra code.
x = 10;
do
{
cout << x << endl;
x = x - 3;
} while ( x > 0 );
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
24
In the expression (j > 0 && j+1 == 10),which operator executes last?

A)>
B)&&
C)+
D)==
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
25
For each of the following situations,tell which type loop (while,do-while,or for)would be best in that situation:
a)Reading a list of an unknown number of homework grades for a single student.
b)Summing a series such as 1 +1/2 +1/(22)+ 1/(23)+ … + 1/(28)
c)Testing a newly coded library function to see how it performs for different values of its arguments.
d)Reading in the number of days of vacation taken by an employee.
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
26
Write a program that reads in and sums the squares of positive integers until a value that 0 or less is read in.
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
27
Consider the if statement: if(condition)yes_clause;else no_clause;
Under which of the following circumstances will both the yes_clause and the no_clause will be executed?

A)When the condition is true.
B)When the condition is false.
C)When the condition is ambiguous.
D)This will not happen.
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
28
Which of the following determines the operator that is processed prior to another operator?

A)Operator associativity
B)Operator precedence
C)Whether the operator is an arithmetic operator
D)None of these determine the order in which operators are processed.
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
29
What is the output of the following,if it were embedded in an otherwise correct and complete program and run? int x = 10;
While (x > 0)
{
Cout << x << " ";
X = x + 3;
}
Cout << endl;

A)10 13 16 19 ...
B)The compiler detects that this will be an infinite loop,so it does not compile. Insert lowercase be
C)This is an infinite loop.When compiled and run,it runs until machine limitations stop it,or you get tired of it and kill the process.
D)0 3 6 9.
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
30
Write a program that reads in exactly 10 integers and outputs the sum.
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
31
Which of the following control structures requires curly braces?

A)if-else
B)while
C)do-while
D)switch
E)for
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
32
Which of the following loop statements is guaranteed to iterate the body of the loop at least once?

A)while(control)body;
B)do body while(control);
C)for (initialize;test;update)body;
D)none of the above
E)all of the above
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
33
Predict the output of the following nested loops:
int n = 1;
while(n <= 10)
{
int m = 10;
while(m>=1)
{
cout << n << " times " << m
<< " = " << n*m << endl;
m--;
}
n++;
}
Unlock Deck
Unlock for access to all 33 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 33 flashcards in this deck.