This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SCJA Mock Exam Question on the Switch Statement

 
Stephen Davies
Ranch Hand
Posts: 352
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
greg fuentes
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
patrick avery
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic