티스토리 뷰

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)

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
글 보관함