Given this code in a method:
4. int x = 0; 5. int[] primes = {1,2,3,5}; 6. for(int i: primes) 7. switch(i) { 8. case 1: x += i; 9. case 5: x += i; 10. default: x += i; 11. case 2: x += i; 12. } /*from w ww .j av a 2s . c o m*/ 13. System.out.println(x);
What is the result?
D is correct.
The code is all legal.
A switch's cases don't have to be in any particular order.
Also remember that when a case is matched, its code, and all the subsequent cases' code, will run unless a break is encountered.