public static void main(String[] args) {
int ans;
try {
int num = 10;
int div = 0;
ans = num / div;
} catch (ArithmeticException ae) {
ans = 0;
} catch (Exception e) {
System.out.println("Invalid calculation");
}
System.out.println("Ans = " + ans);
}
Answer is D.
To test:
public static void main(String[] args) {
int ans;
try {
int num = 10;
int div = 0;
ans = num / div;
} catch (ArithmeticException ae) {
ans = 0; // line n1
} catch (Exception e) {
System.out.println("Invalid calculation");
}
System.out.println("Answer = " + ans); // line n2
}
The correct answer is the letter D, the compilation fails on line n2 because if you catch the exception "e" the variable "ans" was not started, causing a compilation error.
upvoted 1 times
...
Log in to ExamTopics
Sign in:
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
MPignaProTech
2 months, 3 weeks agojackymak
1 year, 7 months agoTOPPSI
1 year, 12 months agocarloswork
2 years, 2 months agoiSnover
2 years, 3 months ago