File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a swith/case question

 
Shiao Kung Chux
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. int i=1, j=0;
2.
3. switch(i){
4.case 2:
5. j+=6;
6.
7. case 4:
8. j+=1;
9.
10. default:
11. j +=2;
12.
13. case 0:
14. j +=4;
15.}
16.

What is the value of j at line 16?
A. 0
B. 1
C. 2
D. 4
E. 6


why the answer is E?
 
Tilo Hemp
Ranch Hand
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
- no case matches 1, so default case is executed
- at the end of the default case, no break statement is given, so case 0 is executed also ("fall through")

regards
 
Shiao Kung Chux
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tks
 
Arnab Sadhukhan
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer is 6.
First the default case will be executed, and after default the last case will be executed.
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shiao,

Keep in mind that when using a switch statement, the case option is just an entry point for processing. So if after enter the entry point the program doesn't find any break instruction, the program will still process everything it found.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic