Consider the following program: public int test( ) { int result = 5; try { check
ID: 3932200 • Letter: C
Question
Consider the following program:
public int test( ) {
int result = 5;
try {
checkIt( result );
result += 5;
} catch ( ArithmeticException e ) {
result += 10;
} catch ( RuntimeException e ) {
result += 20;
} catch ( java.io.IOException e ) {
result = 0;
}
return result;
}
What value does the method 'test' return in each of the following situations?
-The method call to 'checkIt' returns successfully.
-The method call to 'checkIt' generates a FileNotFoundException.
-The method call to 'checkIt' generates an ArithmeticException.
-The method call to 'checkIt' generates a ClassCastException.
Explanation / Answer
Question 1: The method call to 'checkIt' returns successfully.
Answer: if checkit returns successfully then test method will return value 10
Question 2: The method call to 'checkIt' generates a FileNotFoundException.
Answer: if checkit generates a FileNotFoundException then test method will return value 0
Becuase that exception will be handled by catch ( java.io.IOException e ) this block here we are setting result = 0. so test method will return 0.
Question 3: The method call to 'checkIt' generates an ArithmeticException.
Answer: if checkit generates a ArithmeticException then test method will return value 15
Becuase that exception will be handled by catch ( ArithmeticException e ) this block here we are setting result = result + 10. so test method will return 15.
Question 4: The method call to 'checkIt' generates a ClassCastException.
Answer: if checkit generates a ClassCastException then test method will return value 25
Becuase that exception will be handled by catch ( RuntimeException e ) this block here we are setting result = result + 20. so test method will return 25.