Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt in kathy sierra question(Flow Control)?

 
Hennry Smith
Greenhorn
Posts: 26
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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....???
 
Meena Ajay
Ranch Hand
Posts: 38
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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...
 
Henry Wong
author
Marshal
Pie
Posts: 21413
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Riaz ahmed pinjari,

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

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic