Win a copy of Spring Boot in Practice this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

try-catch-finally

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

which of the following are the output
a) 4
b) 5
c) 7
d) 8
e) none
the correct answers are 4 & 5
but to my analysis is none
can any body explain this;
class A
{
public static void main(String argc[])
{
for(int i=0;i<10;++i)
{
try
{
try
{
if(i%3 == 0) throw new Exception("EO");
System.out.println(i);
}
catch(Exception inner)
{
i *= 2;
if(i%3 == 0) throw new Exception("E1");
}
finally { ++i; }
}
catch(Exception outer)
{ i += 3;
}
finally { --i; }
}
}
}
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. i = 0
2. i%3 i.e 0%3 = 0 so the condition is true and it can through an new Exception("EO");
3. i *= 2; i = 0 * 2 = 0;
4. Again i%3 = 0%3 = 0 so the condition again becomes true and it throghs new Exception("E1");
5. Before exiting the first try catch block it has to execute the finally block. Hence now the i becomes ++i = 1
6. Since it had through the Exception(E1) the second catch block can handle that excepion.
7. i += 3; i = 1 + 3 = 4;
8. outter finally block will execute and i will become --i = 3
9. For loop will increment the i and hence i = 4
10. The if statement is false, so it can "print the value of 4" and both the finally blocks always executes.
so ++i = 5 and --i = 4 but still value did not changed.
11. For loop will increment the i and hence i = 5
12. The if statement is false, so it can "print the value of 5" and both the finally blocks always executes.
so ++i = 6 and --i = 5 but still value did not changed.
13. For loop will increment the value and i = 6.
I guess you can proceed further and can find out the other things.
Hope it helps. If you don't understand anything pls. write.
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
These test to see if you can follow the program flow and the easiest way to do this is to put it to paper.
1 for(int i=0;i<10;++i)
2 {
3 try
4 {
5 try
6 {
7 if(i%3 == 0) throw new Exception("EO");
8 System.out.println(i);
9 }
10 catch(Exception inner)
11 {
12 i *= 2;
13 if(i%3 == 0) throw new Exception("E1");
14 }
15 finally { ++i; }
16 }
17 catch(Exception outer)
18 { i += 3;
19 }
20 finally { --i; }
21 }
Ok, pen and paper this
first pass
line 1 i = 0
7 i = 0 i%3 = 0 E0 thrown caught line 10
12 i = 0 i*=2 = 0
13 i = 0 i%3 = 0 E1 thrown caught line 17
15 (finally) i = 0 ++i = 1
18 i = 1 i+=3 = 4
20 (finally) i = 4 --i = 3
second pass
1 i = 3 (increment i)(i<10) i = 4
7 i = 4 i%3 = 1 (no Exception)
8 PRINT 4
15 (finally) i = 4 ++1 = 5
20 (finally) i = 5 --i = 4
third pass
1 i = 4 (increment i)(i<10) i = 5
7 i = 5 i%3 = 2 (no Exception)
8 PRINT 5
15 (finally) i = 5 ++i = 6
20 (finally) i = 6 --i = 5
forth pass
1 i = 5 (increment i)(i<10) i = 6
7 i = 6 i%3 = 0 E0 thrown caught line 10
12 i = 6 i*=2 = 12
13 i = 12 i%3 = 0 E1 thrown caught line 17
15 (finally) i = 12 ++i = 13
18 i = 13 i+=3 = 16
20 (finally) i = 16 --i = 15
fifth pass
1 i = 15 (increment i)(i>10 Exit for)
Pen and paper can be extremely benefical bebugging tool, it can show why a piece of code is giving unexpected results, it's not just to help a person through the exam.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Carl,
Is there any chance of such a question being asked in the exam.
On an average only 2 minutes are given to answer each question.
For this question to be read, digested and put on the paper the way you have done would take at least 10mins. That would make a person appearing in the exam, very nervous to have to spend so much time on one question.
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ajit Deshpande:
Hi Carl,
Is there any chance of such a question being asked in the exam.
On an average only 2 minutes are given to answer each question.
For this question to be read, digested and put on the paper the way you have done would take at least 10mins. That would make a person appearing in the exam, very nervous to have to spend so much time on one question.


I'm just studying now, but they books all say to take a pen/pencil and paper so...
Yes the average time is 2 minutes each but, some will take you 10 minutes and some will take you 30 secs. If you think a question is going to take to long, mark it and go back after you finish the rest and use what time you need/have left.
 
reply
    Bookmark Topic Watch Topic
  • New Topic