posted 22 years ago
FROM: www.danchisholm.net/august04/topic/control.html
(older exam version)
What is the result of attempting to compile and run the above program?
a. Prints: v w x y
b. Prints: v w x y z Default
c. Prints: v y w z
d. Prints: Default Default Default Default
e. Runtime Exception
f. Compiler Error
g. None of the Above
ANSWER: C
EXPLANATION:
The constant case expressions can be any expression that is assignable to the type of the switch expression. 'd'-'a'=3. ~0=-1. 4&5=4.
-----------
I thought that the rules for what is allowed as x for " case x: " are as follows:
(1) x must be assignable to the variable in the switch statement
(2) x can be byte, char, short, or int only
(3) According to Mughal, "case labels are constant expressions"
*this is where I am unsure*
When I think "constant expressions", I think of values that are known at compile time. Such as the number 7 or a final int variable j. But, 'd'-'a', ~0, 4&5 are computed at runtime, right?
any insight will be very much appreciated.
[ Jess added [URL=http://www.javaranch.com]UBB [CODE] tags ]
[ August 19, 2002: Message edited by: Jessica Sant ]