What is the result?

Given the code fragment:

What is the result?

Given the code fragment:

What is the result?

A.
Answer = 0

B.
Invalid calculation

C.
Compilation fails only at line n1.

D.
Compilation fails only at line n2.

E.
Compilation fails only at line n1 and line2.



Leave a Reply 4

Your email address will not be published. Required fields are marked *


v10

v10

this is a tricky one, if you compile it the way it is it fails at n1, if you fix n1 with ans = 0; then compile again it fails at n1 and n2.

renko

renko

Answer: C
package q038;

public class DivideBy0 {
//static int ans;
public static void main(String[] args) {
try {
int num = 10;
int div = 0;
int ans = num / div; //ans is local variable (in this try-block)
} catch (ArithmeticException ae) {
ans = 0; //line 1; variable ans is unknown here -> compile error! => C
} catch (Exception e) {
System.out.println(“Invalid calculation”);
}
System.out.println(“Answer = “+ ans);
}
}
Output:
Exception in thread “main” java.lang.Error: Unresolved compilation problems:
ans cannot be resolved to a variable
ans cannot be resolved to a variable

at q038.DivideBy0.main(DivideBy0.java:11)

renko

renko

Answer E is correct!

jeronimo

jeronimo

La respuesta correcta es la E