• 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

Enum doubt

 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
source : http://www.ejavaguru.com/scjp5freemockexam.php#enumq3
doubt : I think we need to put ; after line 4. In K&B book page 65 ,there it mentioned we require ; if more code follows. pls clarify.

Q3)
What is the output of the following code:
line1> public enum Day {
line2>MONDAY (1),
line3>TUESDAY (2),
line4>WEDNESDAY (3) {public String toString() { return "Good Morning"; } },
line5>THURSDAY (4),
line6>FRIDAY (5),
line7>SATURDAY (6),
line8>SUNDAY (7);
line9>
line10>int dayNumber;
line11>
line12>Day (int dayNumber) {
line13>this.dayNumber = dayNumber;
line14>}
line15>
line16>public static void main (String[] args) {
line17>for (Day d : Day.values())
line18>System.out.println (d);
line19>}
line20>}

Options:
a) Compilation error at line number 4 : Invalid code.
b) Executes OK giving OUTPUT:
MONDAY
TUESDAY
Good Morning
THURSDAY
FRIDAY
SATURDAY
SUNDAY

c) Runtime error : Cannot execute enum 'Day' as a standalone application.
d) Executes OK giving OUTPUT:
MONDAY
TUESDAY
WEDNESDAY
THURSDAY
FRIDAY
SATURDAY
SUNDAY

Ans: b
 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That we need to put semicoln if more code follows
is true at line 8 .where are constants have been declared but more enum code is yet to be done.
but on line 6, its only one constant being declared.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sudha,

The answer to the above question is "b" without making any changes to the code.
MONDAY
TUESDAY
Good Morning
THURSDAY
FRIDAY
SATURDAY
SUNDAY

Thanks,
Meera
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sudha javvadi said


I think we need to put ; after line 4. In K&B book page 65 ,there it mentioned we require ; if more code follows. pls clarify.


You need to put a semicolon when you have enum body, not when you have enum type body.


reply
    Bookmark Topic Watch Topic
  • New Topic