aspose file tools*
The moose likes Associate Certification (SCJA,OCAJ 5/6) and the fly likes SCJA Mock Exam Question on the Switch Statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Associate Certification (SCJA,OCAJ 5/6)
Bookmark "SCJA Mock Exam Question on the Switch Statement " Watch "SCJA Mock Exam Question on the Switch Statement " New topic
Author

SCJA Mock Exam Question on the Switch Statement

Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
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


be a well encapsulated person, don't expose your privates, unless you public void getWife()!
greg fuentes
Greenhorn

Joined: Sep 12, 2008
Posts: 3
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

Joined: Sep 12, 2008
Posts: 3
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

Joined: Sep 12, 2008
Posts: 46
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


SCJA 96%
SCJP 6 88%
skipping SCJD to work on passing SCWCD
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
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
 
subject: SCJA Mock Exam Question on the Switch Statement