470
Exam Preparation Exercises
1.Which control structure should you use if you think an operation might throw
an exception?
2.Which statement raises an exception?
3.What part of the try-catch-finallystructure must have a parameter consisting of
an exception object?
4.Our code can catch exceptions it throws, but not exceptions that the system
throws. (True or False?)
5.Mark the following statements as true or false. If a statement is false, explain
why.
a.There can only be one catchclause for each try.
b.The exception handler is located within the catchclause.
c. The finallyclause is optional.
d.The finallyclause is rarely used.
6.Define the following terms: switch expression, pretest loop, posttest loop.
7.A switch expression may be an expression that results in a value of type int,
float,boolean, or char. (True or False?)
8.The values in case labels may appear in any order, but duplicate case labels are
not allowed within a given switchstatement. (True or False?)
9.All possible values for the switch expression must be included among the case
labels for a given switchstatement. (True or False?)
10.Rewrite the following code fragment using a switchstatement.
if(n == 3 )
alpha++;
else if(n == 7 )
beta++;
else if(n == 10 )
gamma++;
11.What is printed by the following code fragment if nequals 3? (Be careful here.)
switch(n + 1 )
{
case 2 : outData.println("Bill");
case 4 : outData.println("Mary");
case 7 : outData.println("Joe");
case 9 : outData.println("Anne");
T
E
A
M
F
L
Y
Team-Fly®