This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
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

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Switch Statements" Watch "Switch Statements" New topic

Switch Statements

Sudipta Das

Joined: Apr 05, 2001
Posts: 18
This question is from the mock exam 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 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
The switch construct CAN have ONLY char, byte, short, int data types.
Please refer to the JLS for the specifications.
Ravindra Mohan
[This message has been edited by Ravindra Mohan (edited May 04, 2001).]
I agree. Here's the link:
subject: Switch Statements
It's not a secret anymore!