+ 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.
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
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â
0
i never said that the value is relevant