This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes  switch Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark " switch " Watch " switch " New topic
Author

switch

xie li
Ranch Hand

Joined: Nov 30, 2005
Posts: 54
1 int i = 1,j = -1;
2 switch (i) {
3 case 0, 1:j = 1;
4 case 2: j = 2;
5 default; j = 0;
6 }
7 System.out.println("j="+j); What is the result?

A. j = -1
B. j = 0
C. j = 1
D. j = 2
E. Compilation fails.
harish shankarnarayan
Ranch Hand

Joined: Sep 12, 2005
Posts: 158
line 3,
case 0,1???


ans: Compilation fails


Harish<br />SCJP 1.4 (85%)
xie li
Ranch Hand

Joined: Nov 30, 2005
Posts: 54
why?
vipul patel
Ranch Hand

Joined: Oct 16, 2005
Posts: 146
Xie,

Answer is "Compilation fails!". Why? Note that java compiler does not understand everything you write.

1 int i = 1,j = -1;
2 switch (i) {
3 case 0, 1:j = 1;
4 case 2: j = 2;
5 default; j = 0;
6 }

Look at line 3. It is syntax error.
Look at line 5. It is syntax error as well in default; (instead of ;,

Correct Version of code is as follow.


1 int i = 1,j = -1;
2 switch (i) {
3 case 0: case 1:j = 1;
4 case 2: j = 2;
5 default: j = 0;
6 }
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: switch
 
Similar Threads
Question on switch
on switch expression
a swith/case question
GroupLayout radio button alignment
a switch problem