• 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

doubt in kathy sierra question(Flow Control)?

 
Greenhorn
Posts: 26
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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....???
 
Ranch Hand
Posts: 38
Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Hennry Smith
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...
 
author
Posts: 23949
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
Riaz ahmed pinjari,

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

Henry
 
reply
    Bookmark Topic Watch Topic
  • New Topic