This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Wats the output..... Explanations needed.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Wats the output..... Explanations needed...." Watch "Wats the output..... Explanations needed...." New topic
Author

Wats the output..... Explanations needed....

Shiaber Shaam
Ranch Hand

Joined: Jun 16, 2006
Posts: 252
Given:
1. public class Test {
2. public static String output ="";
3.
4. public static void foo(int i) {
5. try {
6. if(i==1) {
7. throw new Exception();
8. }
9. output += "1";
10. }
11. catch(Exception e) {
12. output += "2";
13. return;
14. }
15. finally {
16. output += "3";
17. }
18. output += "4";
19. }
20.
21. public static void main(String args[]) {
22. foo(0);
23. foo(1);
24.
25. }
26. }
What is the value of the variable output at line 23?


$Hi/\bEr
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
output = "134" before calling foo(1);
output = "13423" after calling foo(1);
Wally Valters
Greenhorn

Joined: Jun 08, 2006
Posts: 3
Wouldn't you get a compile error? I thought you could'nt have code in between a try{} and catch{} block.

Or is it ok since all the code between try and catch in this instance is in the same scope?
[ June 16, 2006: Message edited by: Wally Valters ]
Swapnil Trivedi
Ranch Hand

Joined: Jun 06, 2006
Posts: 106
Hi Wally, if u give a closer look to the code you will see that the statement is inside Try block. So there's no problem


Regards
swapnil
[ June 16, 2006: Message edited by: Swapnil Trivedi ]

SCJP 5.0<br />-----------<br />"Help Ever && Hurt Never"
srilatha rao
Greenhorn

Joined: Jun 01, 2006
Posts: 21
Hi,

When foo(0) is executed, the output value is "134", since catch block is not executed.
When main method calls the foo(1) method, the output value continues with the "134" value, since output is static string variable.Only one copy of output is created.
When foo(1) is executed, the output value is "1342", since in the catch block, there is a return statement. 3 and 4 values cannot be appended to the output variable. Here, return statement stops the execution of finally and outside the finally block code and returns to the main method.

So, the value of output variable is "134" before calling foo(1) and "1342" after calling foo(1).

As per my knowledge, the above analysis is correct.
If there is any mistake, sorry for the reply.
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809

Originally posted by srilatha rao:

So, the value of output variable is "134" before calling foo(1) and "1342" after calling foo(1).


What about finally. finally will always be executed even at second time.

Output given by Wise is correct.



Naseem
[ June 16, 2006: Message edited by: Naseem Khan ]

Asking Smart Questions FAQ - How To Put Your Code In Code Tags
srilatha rao
Greenhorn

Joined: Jun 01, 2006
Posts: 21
Oh, I am sorry, yes, the output is "13423".
finally will be executed in the second time also.
Thanks
S Thiyanesh
Ranch Hand

Joined: Mar 19, 2006
Posts: 142
output is 13423
krish bajaj
Ranch Hand

Joined: Jun 19, 2006
Posts: 31
hi all!
tell me srilathawhy the statement afeter finally block not execute
i understand upto 13423 but why not 4 i mean why not 134234
please help me
Shiaber Shaam
Ranch Hand

Joined: Jun 16, 2006
Posts: 252
hi bajaj...
if there occurs a return stmt in catch or finally block... the code in finally gets executed and the code after finally gets skipped.... I hope it will make sense....
krish bajaj
Ranch Hand

Joined: Jun 19, 2006
Posts: 31
hello shiaber

just try this program without using return statement in catch block then after finally block statement execute easily
Do not use number simply use write statement for understanding
i did ur program and somewhere my concept is clear
if u like my answer then well and good otherwise sorry because i am new player in this field
 
 
subject: Wats the output..... Explanations needed....