• 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

from mock exam

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
This code is from a mock exam
please read the code below and tell me why it gives an output as 5,8 i thought it would be 2,5,8.
I think i am not able to understand the flow of the program, can anyone please explain this to me

Thankx in advance

****I added UBB code to make this more readable, Bill ****
[This message has been edited by bill bozeman (edited December 12, 2000).]
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Again, step through the code with a pen and paper.
1. i=0, first if statement is true, so it goes to outer catch blocks, where i becomes 3, then the finally so i is 4, then back through the loop so i is now 5.
2. i=5, first if is false, second if is false, so 5 gets printed. Goes through both finally statements, so i becomes 7, then back to the loop so i is now 8.
3. i=8, first if is false, second if is false, so 8 gets printed. Goes through both finally statements, so i becomes 10, back to the loop, which is now false, so loop ends.
4. Final answer, 5,8
Bill
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay,
A tricky one, but once you get your head around it, you'll see...
Let's go through this step-by-step...
Beginning of Loop.
i = 0
if i % 3 == 0 then throw exception. RESULT : TRUE - EXCEPTION THROWN
i += 3 RESULT : i = 3
execute finally block
++i : RESULT : i = 4
End of Loop.
Beginning of loop. i is incremented to 5
i = 5
if i % 3 == 0 : RESULT : FALSE
if i % 3 == 1 : RESULT : FALSE (i % 3 == 2 since i == 5)
System.out.println(i) : RESULT : 5 is output to screen
Execute Finally Block
++i : RESULT : i = 6
Here's the catch. The *FINALLY* in the outer block *ALWAYS*
executes, so:
++i : RESULT : i = 7
End Of Loop.
Beginning Of Loop. i is incremented to 8
i = 8
if i % 3 == 0 : RESULT : FALSE
if i % 3 == 1 : RESULT : FALSE(8 / 3 = 3 remainder 2)
System.out.println(i) : RESULT : 8 is output to the screen
Execute the finally block
++i : RESULT : i = 9
Execute the *outer* finally block
++i : RESULT : i = 10
Loop Terminates (since i = 10)

Confusing! But I hope this helps somewhat...

David Harrigan
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic