This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Enum  Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Enum  Question" Watch "Enum  Question" New topic
Author

Enum Question

Lao Kinsuyi
Ranch Hand

Joined: Mar 22, 2007
Posts: 30
Hi all,
This is a question from master exam accompanying Bert holy bible book.

Given,
Class Stepper {
Enum Roman {I,V,X,L,C,M}
public static void main(String...bang){
int x =7;
int z =2;
Roman r=Roman.x;
do{
Switch(r){
case C:r=Roman.L; break;
case X:r=Roman.C;
case L:if (r.ordinal)>2z+=5;
case M:x++;
}z++;
}
While (x>10);
System.out.println(Z);
}
}

What is the result?
a 5
b v
c 18
d 21
e 22
f compilation fails



But,i think the answer should be compilation fails. Because, not all the alphabelts in Enum Roman block were used in Switch block(case)
Only case C,X,L,M were used.

Please, i want someone to explain with reasons why it is not compilation fails.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Lao Kinsuyi:
...i think the answer should be compilation fails...

Have you tested this code?

There are 7 errors (typos) that will prevent this from compiling. Once you fix these, try experimenting with values that are not represented as cases (for example, Roman.V).
[ July 21, 2007: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Lao Kinsuyi
Ranch Hand

Joined: Mar 22, 2007
Posts: 30
Please, someone should explain this thread(enum question)
Over to you Chandra Bhatt for your usual effort.
Padma Asrani
Ranch Hand

Joined: Mar 22, 2007
Posts: 111
hello Lao

There are lot of typos in the program and also the correct output 8 is not list, so it leads to mark for compilation fails but was the program really testing us to spot typos. If it is then I guess it is really a poor example.
Anyways i have corrected the program

You can manually find out the difference.

Regards
Padma
ram shah
Greenhorn

Joined: Jun 02, 2007
Posts: 28
I'll try to explain...



In line 6, "r" is set to value X. In line 10,Case X is executed, which sets "r" to C.Since, there is no break statement, all other cases are also executed.In line 11, as "r.ordinal()" returns 5, z is incremented to 7.
In line 14, z is again incremented by 1. value of z is now 8. In line 16, the while condition is evaluated and found to be false.So, the execution comes out of the loop and value of z is printed as 8.

Somebody please correct me if I'm wrong.
Lao Kinsuyi
Ranch Hand

Joined: Mar 22, 2007
Posts: 30
I've corrected the missing codes. Thank you.Now, it's well understood.
 
 
subject: Enum Question