Part A. 다음 프로그램의 출력을 정확히 쓰시오 (각 10점)
공백·줄바꿈·소수점까지 정확히. 컴파일 오류가 나면 "컴파일 오류"라고 쓰고 이유를 적으시오.
A-1. 정수 나눗셈과 연산자 우선순위 [10점]
public class Q1 {
public static void main(String[] args) {
System.out.println(5 / 2);
System.out.println(5.0 / 2);
System.out.println(1 + 2 * 3 - 4);
System.out.println(2 + 3 + "x" + 2 + 3);
}
}
정답
2
2.5
3
5x23
정수/정수=2(나머지 버림), 5.0/2=2.5(실수 나눗셈), 1+(2*3)-4=3, 문자열 \"x\"를 만난 뒤로는 모두 연결 → 5x23.
A-2. 반복문 — 누적합 출력 [10점]
public class Q2 {
public static void main(String[] args) {
int sum = 0;
for (int i = 0; i < 10; i++) {
System.out.print(sum + " ");
sum += i + 1;
}
}
}
정답
0 1 3 6 10 15 21 28 36 45
매 반복마다 sum을 먼저 출력한 뒤 (i+1)을 더한다. 누적합 0,1,3,6,…,45. (덧셈 전 값이 출력됨에 주의)
A-3. 메서드 오버로딩 [10점]
public class Q3 {
public static void main(String[] args) {
System.out.println(max(3, 4));
System.out.println(max(3.0, 5.4));
System.out.println(max(2, 2.5));
}
static int max(int a, int b) { return a > b ? a : b; }
static double max(double a, double b) { return a > b ? a : b; }
}
정답
4
5.4
2.5
max(3,4)→max(int,int)(4). max(3.0,5.4)→max(double,double)(5.4). max(2,2.5)는 int 2가 double 2.0으로 자동 변환되어 max(double,double) 호출 → 2.5.
A-4. [교재 TestSum 변형] float vs double 정밀도 [10점]
public class Q4 {
public static void main(String[] args) {
float fsum = 0;
for (float i = 0.01f; i <= 1.0f; i = i + 0.01f) fsum += i;
System.out.println(fsum);
double dsum = 0;
for (double i = 0.01; i <= 1.0; i = i + 0.01) dsum += i;
System.out.println(dsum);
}
}
정답
50.499985
49.50000000000003
float는 0.01을 정확히 저장 못 해 오차 누적 → 50.499985. double로 바꿔도 50.5가 아니다: 누적 오차로 마지막 i가 1.0보다 살짝 커져 마지막 항이 빠짐 → 49.50000000000003. (교재가 강조한 함정)
Part B. 다음 코드의 오류를 찾아 이유를 쓰고 바르게 고치시오 (각 10점)
컴파일되지 않는 줄과 그 이유, 올바른 코드를 함께 쓰시오.
B-1. 자료형·형 변환 오류 [10점]
public class Q5 {
public static void main(String[] args) {
float rate = 0.05;
int count = 100.0;
System.out.println(rate + " " + count);
}
}
정답
두 줄 모두 incompatible types: possible lossy conversion 컴파일 오류.
① float rate = 0.05; → 0.05는 double. float rate = 0.05f; (f 접미사) 로 수정.
② int count = 100.0; → 100.0은 double. int count = 100; 로 수정.
① float rate = 0.05; → 0.05는 double. float rate = 0.05f; (f 접미사) 로 수정.
② int count = 100.0; → 100.0은 double. int count = 100; 로 수정.
B-2. 문법·static 오류 [10점]
public class Q6 {
public static void main(string[] args) {
greet();
}
void greet() {
System.out.println("Hello");
}
}
정답
오류 2곳.
① string[] → String의 S는 대문자. String[] args 로 수정('cannot find symbol').
② static인 main에서 인스턴스 메서드 greet()를 직접 호출 → non-static method ... cannot be referenced from a static context. static void greet() 로 고치면 됨.
① string[] → String의 S는 대문자. String[] args 로 수정('cannot find symbol').
② static인 main에서 인스턴스 메서드 greet()를 직접 호출 → non-static method ... cannot be referenced from a static context. static void greet() 로 고치면 됨.
Part C. 단답·개념 (각 10점)
C-1. 다음 코드의 출력과, x가 그렇게 되는 이유(전달 방식)를 쓰시오. [10점]
public class Q7 {
public static void main(String[] args) {
int x = 1;
increment(x);
System.out.println(x);
}
static void increment(int n) { n = n + 10; }
}
정답
1
자바는 pass by value(값에 의한 전달). 인수 x의 값(1)을 복사해 n에 전달하므로, 메서드 안에서 n을 바꿔도 원본 x는 그대로 1.
C-2. 다음 두 메서드를 한 클래스에 함께 정의하면 어떻게 되는지와 그 이유를 쓰시오. [10점]
public static int f(int x) { return x; }
public static double f(int x) { return x; }
정답
컴파일 오류 (method f(int) is already defined). 오버로딩은 매개변수 목록(개수·타입)으로만 구분되며, 반환 타입만 다른 것은 서로 다른 메서드로 인정되지 않는다(시그니처가 동일).