aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt in kathy sierra question(Flow Control)? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt in kathy sierra question(Flow Control)?" Watch "doubt in kathy sierra question(Flow Control)?" New topic
Author

doubt in kathy sierra question(Flow Control)?

Hennry Smith
Greenhorn

Joined: Jan 21, 2011
Posts: 26


What is the result?
A. 9 10 d
B. 8 9 10 d
C. 9 10 10 d
D. 9 10 10 d 13
E. 8 9 10 10 d 13
F. 8 9 10 9 10 10 d 13
G. Compilation fails

Answer:
D is correct. Did you catch the static initializer block? Remember that switches work on
"fall-thru" logic, and that fall-thru logic also applies to the default case, which is used when
no other case matches.
A, B, C, E, F, and G are incorrect based on the above.


Please tell how D is correct...???


According to me in line 3 we have declared a static variable x=7, then we have used a for loop,so according to the for loop it will run thrice,so the value of x will be increased 3 times, as we run the loop first time the value is increased from 7 to 8 and matched with switch (case 8) so first 8 should be printed....but the answer is 9 10 10 d 13....please explain where i am wrong....also tell why 10 is printed twice....???
Meena Ajay
Ranch Hand

Joined: May 28, 2008
Posts: 38

Hi Hennry ,

As given in the answer from K&B,Did you catch the static initializer block given at the end of the code - line 19.?

After the line 4. executes, the value of x is set to 7.Since there is a static initializer block ( given in line number 19.), this would be the next thing to run. So, the value of x would be incremented to 8 after the static initializer block executes.

Now, the code enters the for loop with value of x having 8.After the x++ at line 8. is executed, the value of x becomes 9 and this enters the switch and case 9: would be executed. As per the looping logic, D is the answer"9 10 10 d 13 "

Hope this helps.


Cheers, Meena
OCPJP 6
Hennry Smith
Greenhorn

Joined: Jan 21, 2011
Posts: 26
Meena Ajay wrote:Hi Hennry ,

As given in the answer from K&B,Did you catch the static initializer block given at the end of the code - line 19.?

After the line 4. executes, the value of x is set to 7.Since there is a static initializer block ( given in line number 19.), this would be the next thing to run. So, the value of x would be incremented to 8 after the static initializer block executes.

Now, the code enters the for loop with value of x having 8.After the x++ at line 8. is executed, the value of x becomes 9 and this enters the switch and case 9: would be executed. As per the looping logic, D is the answer"9 10 10 d 13 "

Hope this helps.



Thank you so much....i got your answer...
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19070
    
  40

Riaz ahmed pinjari,

Please don't hijack topics. Your post was moved to a new topic.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt in kathy sierra question(Flow Control)?