File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Switch Statements

 
Sudipta Das
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 625
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 216
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic