• 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

SCJA Mock Exam Question on the Switch Statement

 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
During my revision towards my SCJA,I came uponm this uCertify Question in a mock exam.

Q. You work as[sic] a computer faculty. You want to take the tutorial about enumeration types in Java. You write the following code:



What will be the output when you try to comile and run the code?

My answer: C: It will generate a compile-time error

-my answer is based on the fact that an argument to a switch statement must be compatible with type int (as they explain themselevs in the answer review)

uCertify answer: A: it will display the sky is the limit healthy wealthy and wise.


Is it some twisted logic that they reach their answer (like if the switch argument is not compatible the statement isrun anyway), or is their answer wrong outright. I appreciate some advice here!

Thanks in advance

Steve
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code will call the enum and get blue as the value, the switch/case block will fall through to the entry for "blue" and output the text given. Then, because there is no break: statement the code will continue through the next case and then exit.
 
greg fuentes
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
BTW, after stepping into this code with the Netbeans IDE, i can confidently say that the way it is written, the switch function sees the value "blue" and jumps right to it, bypassing the "case:red..." and the "default:..." entries. Additionally when i moved the default line to the end of the switch code block, it also outputs "default" as well. Apparently the DEFAULT entry needs to be the very last entry or it will be ignored if bypassed initially.
hope this helps. Actually watching code step through its lines using the F7 key is a great learning tool in Netbeans.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stephen: you said "-my answer is based on the fact that an argument to a switch statement must be compatible with type int (as they explain themselevs in the answer review)"

This used to be 100% correct, but the latest versions of Java also allow enums along with primitives compatible with type int in switch statements

Good Luck, Pat Avery
 
Stephen Davies
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Greg & Patrick.

I was more asking of the switch compatibility with enums, I am aware of the break statement etc. Yes you're right as of Java 5 enums can be passed to a switch block.I looked it up in my JAVA Desktop reference (5th ed.) O'Reilly. Its an interesting change, but for the best (if your into switch statements!) :roll:

I'm an eclipse man myself, but using a mac, day to day I love TextMate
[ September 14, 2008: Message edited by: Stephen Davies ]
 
Good heavens! What have you done! Here, try to fix it with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic