GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Switch Statements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Switch Statements" Watch "Switch Statements" New topic
Author

Switch Statements

Sudipta Das
Greenhorn

Joined: Apr 05, 2001
Posts: 18
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
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.
Ravindra Mohan
Ranch Hand

Joined: Mar 16, 2001
Posts: 216
Sean,
The switch construct CAN have ONLY char, byte, short, int data types.
Please refer to the JLS http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html#35518 for the specifications.
Ravindra Mohan
[This message has been edited by Ravindra Mohan (edited May 04, 2001).]
 
GeeCON Prague 2014
 
subject: Switch Statements