• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Need help to trace java code

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I am studying for SCJP 6 Exam, and came across this question which shows code as asks for the output




The answer says 9 10 10 d 13 will be output. However, when I hand-traced the program, I keep getting 9 10 d 13. Can you please help? Below is my trace:

step 1: x is set to 7

step 2: x is incremented to 8 (because of static x++)

step 3: string s is initialized to ""

step 4: for loop begins, y is set to 0

step 5: x is incremented to 9

step 6: case 9 appends 9 to string s

step 7: for loop, y is set to 1

step 8: x is incremented to 10

step 9: case 10 appends 10 to string s and breaks out of switch, y is set to 2

step 10: x is incremented to 11

step 11: case default appends d to string s

step 12: case 13 appends 13 to string s, y is set to 3

step 13: loop exits, and prints 9 10 d 13
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
step 6.5: fall through to case 10 which appends 10 to string s and breaks out of switch
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"jhoom barabar jhoom",
Welcome to the ranch ! Please check your Private Messages for an important administrative matter.
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:step 6.5: fall through to case 10 which appends 10 to string s and breaks out of switch



Now it seems so obvious - I was staring at this problem for three days!!!
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sandra Bachan wrote:... and came across this question ...


Welcome to JavaRanch.

Please note that if you copy a question from a book, mock exam or other source, we require you to QuoteYourSources. So, please tell us where you copied this question from.
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper Young wrote:

Sandra Bachan wrote:... and came across this question ...


Welcome to JavaRanch.

Please note that if you copy a question from a book, mock exam or other source, we require you to QuoteYourSources. So, please tell us where you copied this question from.



Of course:

SCJP Sun® Certified Programmer for Java™ 6 Study Guide Exam (310-065) (9780071591065)
Kathy Sierra; Bert Bates.

This particular question from Chapter 5, Question 8
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic