Name:
Class:
Date:
Page 1
Indicate whether the statement is true or false.
1. The switch clause can always be used in replace of a multiple-alternative selection structure.
a.
True
b.
False
2. A common error made when writing selection structures is to reverse the decisions made by the outer and nested
structures.
a.
True
b.
False
3. Selection structures containing several alternatives are referred to as multiple-alternative selection structures.
a.
True
b.
False
4. Using an unnecessary selection structure is one of the mistakes that causes logic errors in selection structures.
a.
True
b.
False
5. The dropout statement tells the computer to exit the switch statement.
a.
True
b.
False
6. Not having a good compiler is one of the mistakes that causes logic errors in selection structures.
a.
True
b.
False
7. Two different selection structures in sequence (one after another) are called a nested selection structure.
a.
True
b.
False
8. A common error made when writing selection structures is to use a compound condition in the outer selection structure
when a nested selection structure is needed.
a.
True
b.
False
9. One or more select clauses are placed after the switch clause and between the switch statement’s opening and
closing braces.
a.
True
b.
False
10. An unnecessary nested selection structure may still produce the correct results, but is less efficient.
a.
True
b.
False
Indicate the answer choice that best completes the statement or answers the question.
Name:
Class:
Date:
Page 2
11. In a flowchart, what does a diamond that has more than two flow lines represent?
a.
extended selection structure
b.
optional selection structure
c.
single-alternative selection structure
d.
dual-alternative selection structure
12. A secondary decision is always made by which selection structure?
a.
inner
b.
nested
c.
alternative
d.
single-alternative
13. If the default clause is the last clause in a switch statement, what statement is required after the default clause?
a.
a case statement
b.
an exit statement
c.
a break statement
d.
no statement is required
14. Which statement is used within a switch statement to tell the computer to exit the switch statement at that point?
a.
exit statement
b.
break statement
c.
dropout statement
d.
default statement
15. If there is no default clause in a switch statement and no match is found, what happens?
a.
the computer loops to the beginning of the switch
statement
b.
an error code is generated
c.
the program execution is terminated
d.
the instruction after the closing brace is
executed
16. What type of clause may you include within a switch statement in addition to case clauses?
a.
swap
b.
exit
c.
default
d.
else
17. Each case clause within a switch statement contains a value followed by which punctuation mark?
a.
comma
b.
colon
c.
period
d.
semicolon
18. Examine the following switch statement. What is needed to make a valid switch statement?
switch
{
case 10:
cout << “The answer is 10.”;
break;
case 20:
case 30:
cout << “The answer is 20 or 30.”;
break;
default:
cout << “The answer is none of them.”;
break;
case 40:
cout << “The answer is 40.”;
}
Name:
Class:
Date:
Page 3
a.
the default clause must be moved to the end
b.
a statement block is needed after the case 20: clause
c.
a break statement is needed after case 40:
d.
a selectorExpression is needed
19. Given the code below beginning a switch statement, what type of data would be required for the value in each case
clause?
char letter = ’;
cout << “Enter a letter: ‘’;
cin >> letter;
switch (letter)
{
……
a.
int
b.
double
c.
char
d.
bool
20. Within a switch statement, the _____ clause can contain one or more statements to be executed when the
selectorExpression does not match any of the _____ clauses.
a.
if, else
b.
case, default
c.
break, case
d.
default, case
21. Instead of an if statement, what type of statement can you often use to code a multiple-alternative selection structure?
a.
select
b.
switch
c.
choose
d.
else
22. Which character encloses a statement block within a switch statement?
a.
single quotes
b.
double quotes
c.
braces
d.
parentheses
23. What type of clause within a switch statement represents a different alternative?
a.
if clause
b.
else clause
c.
case clause
d.
switch clause
24. What can be used to graphically represent a nested selection structure?
a.
Flowcharts
b.
Pseudocode arrays
c.
Venn diagrams
d.
IPO charts
25. The data type of the value in a case clause within a switch statement must be compatible with the data type of which
of the following?
a.
selectorExpression
b.
if statement
c.
else clause
d.
defaultExpression
26. What type of clause is needed in a switch statement if you want a block of code to be executed when there is no
match with any of the case clauses?
a.
break
b.
if
c.
else
d.
default
Name:
Class:
Date:
Page 4
27. A programmer determines whether a problem’s solution requires a nested selection structure by studying which of the
following?
a.
flow model
b.
problem specification
c.
data validation tables
d.
none of the above
28. In the following switch statement, what is the expected output?
switchval = 8323;
switch (switchval)
{
case 30:
cout << “switchval is 30″;
break;
case 40:
cout << “switchval is 40″;
break;
case 50:
cout << “switchval is 50″;
break;
default:
cout << “switchval is more than 50″;
}
a.
switchval is 30
b.
switchval is 40
c.
switchval is 50
d.
switchval is more than 50
29. A common error made when writing selection structures is to do which of the following to the decisions made by the
outer and nested structures?
a.
sequence
b.
reverse
c.
dither
d.
cancel out
30. What are the consequences of writing selection structures to include an unnecessary nested selection structure?
a.
always incorrect results
b.
less efficient code
c.
always correct results
d.
more efficient code
31. Which of the following is true about case clauses in a switch statement?
a.
they must contain a break statement
b.
the value can be a literal constant
c.
the data type can be different from the selectorExpression
d.
each case clause has a default clause
32. What type of decision is always made by an outer selection structure?
a.
primary
b.
secondary
c.
alternative
d.
outer
33. In a switch statement, what immediately follows the switch clause?
a.
break clause
b.
alternativeExpression
c.
case clause
d.
selectorExpression
34. A common error made when writing selection structures is to use a compound condition rather than which of the
following?
Name:
Class:
Date:
Page 5
a.
a for loop
b.
a switch statement
c.
a single condition
d.
a nested selection structure
35. Which clause can be entered anywhere within a switch statement, but is usually coded as the last clause in the switch
statement?
a.
dropout
b.
exit
c.
loop
d.
default
36. When either a selection structure’s true path or its false path contains another selection structure, the inner selection
structure is referred to as which selection structure?
a.
single-alternative
b.
dual-alternative
c.
nested
d.
integrated
37. In the following switch statement, must there be a break statement before the ending brace? Explain your answer.
switch (x)
{
case 1:
cout << “one” << endl;
break;
case 2:
cout << “two” << endl;
break;
case 3:
cout << “three” << endl;
}
38. Write the switch statement equivalent to the following set of code. Assume x has already been declared to be of the
int data type.
if (x == 1)
cout << “one” << endl;
if (x == 2)
cout << “two” << endl;
if (x == 3)
cout << “three << endl;
39. Discuss why the data type of the result of the selectorExpression must match the data type of the value in the case
clause within the switch statement.
40. Write the nested if/else equivalent of the code below:
if (x == 1)
cout << “one” << endl;
if (x == 2)
cout << “two” << endl;
if (x == 3)
cout << “three << endl;
Name:
Class:
Date:
Page 6
41. Discuss some of the ways you can make your program more readable when using selection structures.
42. Rewrite the following code using a switch statement. Assume the variable code has already been declared to be of the
int data type.
if ( (code == 1) || (code == 4) )
cout << “Level 1” << endl;
if ( (code == 2) || (code == 5) )
cout << “Level 2” << endl;
if (code == 3)
cout << “Level 3” << endl;
43. If the variable x contained the value of 1, what will be printed in the following code:
switch (x)
{
case 1:
cout << one” << endl;
case 2:
cout << two” << endl;
case 3:
cout << three” << endl;
}
44. What happens in the execution of a switch statement if no case clauses are matched and there is no default clause?
45. Of the two sets of code below, which is more efficient? Why?
Set #1
if (x == 1)
cout << “one” << endl;
if (x == 2)
cout << “two” << endl;
if (x == 3)
cout << “three << endl;
Set #2
if (x == 1)
cout << “one” << endl;
else if (x == 2)
cout << “two” << endl;
else if (x == 3)
cout << “three << endl;
//end if
Name:
Class:
Date:
Page 7
Name:
Class:
Date:
Name:
Class:
Date:
Page 9