• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

on switch expression

 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai ranchers why this code is giving
compiler error can any one explain this question


Given:
11. int i = 1,j = -1;
12. switch (i) {
13. case 0, 1:j = 1;
14. case 2: j = 2;
15. default; j = 0;
16. }
17. System.out.println(“j=”+j);
What is the result?
A. j = -1
B. j = 0
C. j = 1
D. j = 2
E. Compilation fails.
Answer: E


by
velan vel
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, line 17 has curly "smart" quotes rather than straight quotes. And line 15 has a semicolon rather than a colon after "default."

Other than that, the big problem is in line 13, where two cases are separated by a comma.

(Note that without break statements, these cases will all "fall through" anyway.)
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic