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.