Given:
What is the result?
A.
Area is 6.0
B.
Area is 3.0
C.
Compilation fails at line n1
D.
Compilation fails at line n2.
Given:
What is the result?
A.
Area is 6.0
B.
Area is 3.0
C.
Compilation fails at line n1
D.
Compilation fails at line n2.
D. p is unreachable
Answer is indeed correct.
The reason is that the calculation (line 2) is done outside the if-block and there are the variables b, h and p not initialized.
package q050;
public class Triangle {
static double area;
int b = 2, h = 3;
public static void main(String[] args) {
double p, b, h; //line
if (area == 0) {
b = 3;
h = 4;
p = 0.5;
}
area = p * b * h; //line 2
System.out.println(“Area is “+ area);
}
}
Output:
Exception in thread “main” java.lang.Error: Unresolved compilation problems:
The local variable p may not have been initialized
The local variable b may not have been initialized
The local variable h may not have been initialized
at q050.Triangle.main(Triangle.java:14)
D