This question is from the mock exam http://jquest.webjump.com. The correct answers given are 'b' & 'c'. But I think only 'c' is correct. Please advise me. Given the following code fragment. 1. switch( x ) { 2. case 100 3. System.out.println("One hundred"); break; 4. case 200 5. System.out.println("Two hundred"); break; 6. case 300 7. System.out.println("Three hundred"); break; 8. } This question is from the mock exam http://jquest.webjump.com. The correct answer is given as 'a'. But I think both 'a' & 'c' are correct. Please advise me.
Choose all of the declarations of x that will not cause a compiler error. Ans : a. byte x=100; b. short x=200; c. int x = 300; d. long x = 400;
Sean Casey
Ranch Hand
Joined: Dec 16, 2000
Posts: 625
posted
0
Here b and c are both correct. Note that the argument to a switch construct has to be an integral type. short and int work fine. Note that byte would work, but the cases are out of the range of a byte.