+ 4

Can someone explain how to do this quickly?

So Ive been getting questions that look very similar to this: int result = 100; int add = 2; try{ for(int i=10; i>=0; i--){ result/=i; add++; } }catch (Exeption e) { result=5; } finally { result += add; } System.out.println(result); Answer is 17.

18th Dec 2017, 4:06 PM
Evan Martine
3 Answers
+ 6
those questions mostly have a plottwist. Here you are dividing by zero after 10 interations. This means an exception is thrown and result becomes 5. 'add' is increment in every iteration aka 10 which makes it 12. finally is always executed -> 12+5 = 17
18th Dec 2017, 4:09 PM
Jeremy
Jeremy - avatar
0
@Jeremy is correct. However the initial value of “result” is irrelevant. Basically the program: Loop and increment “a” until exception is thrown, then set “result” to 5 and add “a”
18th Dec 2017, 6:52 PM
H Chiang
0
i never said that the value is relevant
18th Dec 2017, 7:04 PM
Jeremy
Jeremy - avatar