ocajp 자격증 (Oracle Certified Associate Ja
자바 ocjap 문제 해설 - 변수 43
xemaker
2020. 3. 1. 09:02
Given:
public class Type43 {
static double area;
int b=2, h=3;
public static void main(String[] args) {
double p,b,h; //line n1
if(area==0){
b=3;
h=4;
p=0.5;
}
area=p*b*h; //line n2
System.out.println("Area is "+area);
}
}
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,b,h)이 초기화가 되지 않았는데 계산을 하려고 해서 컴파일 에러가 난다. 마치 if 에서 초기화 된것 처럼 보이지만 fake이다. 계산은 if 문 밖에서 하고 있다.
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 Type43.main(Type43.java:11)