Q.- Given the code fragment:
public static void main(String [] args) {
int ans;
try {
int num = 10;
int div = 0;
ans = num / div;
}
catch (ArithmeticException ex) {
ans = 0; // line n1
}
catch (Exception e) {
System.out.println("Invalid calculation”);
}
System.out.println ("Answer = " +ans); // line n2
}
What is the result?
A) Answer = 0
B) Compilation fails at line n1 and line n2.
C) Compilation fails only at line n1.
D) Invalid calculation
E) Compilation fails only at line n2.
⧪ please give your answer in a comment section.
public static void main(String [] args) {
int ans;
try {
int num = 10;
int div = 0;
ans = num / div;
}
catch (ArithmeticException ex) {
ans = 0; // line n1
}
catch (Exception e) {
System.out.println("Invalid calculation”);
}
System.out.println ("Answer = " +ans); // line n2
}
What is the result?
A) Answer = 0
B) Compilation fails at line n1 and line n2.
C) Compilation fails only at line n1.
D) Invalid calculation
E) Compilation fails only at line n2.
⧪ please give your answer in a comment section.
6 Comments
E
ReplyDeletec
ReplyDeleteE
ReplyDeleteA
ReplyDeleteE - the local variable ans may not have been initialized
ReplyDeleteE
ReplyDelete