트릭4

류한선·2025년 6월 27일

실기연습-2

목록 보기
50/95

🧠 정답 및 상세 해설


✅ 문제 6 정답: B

class A {
    void show() {
        System.out.println("A");
    }
}

class B extends A {
    void show() {
        System.out.println("B");
    }
}

public class Main {
    public static void main(String[] args) {
        A obj = new B();
        obj.show();
    }
}
A obj = new B();  // 업캐스팅
obj.show();       // 오버라이딩된 B의 show() 호출됨 → 동적 바인딩

정답 출력:

B

✅ 문제 7 정답: 2

import java.util.HashSet;

class User {
    String name;
    User(String name) {
        this.name = name;
    }
}

public class Main {
    public static void main(String[] args) {
        HashSet<User> set = new HashSet<>();
        set.add(new User("kim"));
        set.add(new User("kim"));

        System.out.println(set.size());
    }
}
  • HashSetequals()hashCode()를 기준으로 중복 제거
  • User("kim")은 내용은 같아도 다른 객체 → equals, hashCode 미오버라이딩 시 다른 객체로 판단됨

정답 출력:

2

✅ 문제 8 정답:

public class Main {
    public static void main(String[] args) {
        int[] a = {1, 2, 3};
        int[] b = a;
        b[1] = 99;

        int[] c = a.clone();
        c[2] = 100;

        System.out.println(a[1]);
        System.out.println(a[2]);
    }
}
99  
3
  • b = a → 같은 배열 참조 → a[1] = 99
  • c = a.clone() → 깊은 복사 → c[2] = 100a에 영향 없음

정답 출력:

99  
3

✅ 문제 9 정답:

public class Main {
    public static void main(String[] args) {
        int n = 2;
        switch(n) {
            case 1:
                System.out.print("one ");
            case 2:
                System.out.print("two ");
            case 3:
                System.out.print("three ");
                break;
            default:
                System.out.print("default ");
        }
    }
}
two three 
  • case 2:부터 실행됨 → break 없음 → case 3도 실행
  • case 1은 n이 2이므로 건너뜀

정답 출력:

two three 

✅ 문제 10 정답: A

interface A {
    default void show() {
        System.out.println("A");
    }
}

interface B {
    default void show() {
        System.out.println("B");
    }
}

class C implements A, B {
    public void show() {
        A.super.show();
    }
}

public class Main {
    public static void main(String[] args) {
        C obj = new C();
        obj.show();
    }
}
  • 인터페이스 A, B 둘 다 show()를 갖고 있어 충돌 발생
  • 클래스 C에서 직접 오버라이딩A.super.show()로 특정 인터페이스 호출 지정 가능

정답 출력:

A

✅ 전체 요약 정답표

문제 번호주제정답 / 출력
6오버라이딩 + 업캐스팅B
7equals/hashCode 미정의2
8참조 vs 복사99 3
9switch break 누락two three
10인터페이스 default 충돌 + 명시 호출A

📘 정답 및 해설


✅ 문제 11 정답:

public class Main {
    public static void main(String[] args) {
        String str = "abc";
        StringBuilder sb = new StringBuilder("abc");

        str.concat("def");
        sb.append("def");

        System.out.println(str);
        System.out.println(sb);
    }
}
abc  
abcdef
  • String불변(immutable)str.concat("def")는 원본에 영향 없음
  • StringBuilder가변(mutable)sb.append("def")는 원본 변경됨

✅ 문제 12 정답:

class Animal {}
class Dog extends Animal {}

public class Main {
    public static void main(String[] args) {
        Animal a = new Dog();
        if (a instanceof Animal)
            System.out.print("Animal ");
        if (a instanceof Dog)
            System.out.print("Dog ");
    }
}
Animal Dog
  • aDog 타입의 인스턴스이므로 두 조건 다 true

instanceof 연산자의 정체

instanceof객체가 특정 클래스(또는 그 자식)인지 확인할 때 사용하는 논리 연산자야.
결과는 true 또는 false (boolean 값)으로 나와.


📌 기본 문법

객체참조변수 instanceof 클래스명
  • 왼쪽: 실제 객체를 참조하는 참조변수
  • 오른쪽: 확인하고 싶은 타입

🎯 역할과 동작 방식

역할상세 설명
1. 객체의 실제 타입 확인런타임 시점에 객체가 특정 클래스(또는 그 하위 클래스)인지 확인
2. 안전한 다운캐스팅 전 검사강제 형변환((Dog) a) 전에 그 객체가 진짜 Dog인지 확인
3. 타입에 따라 다른 처리조건문으로 객체의 타입에 따라 서로 다른 로직을 처리할 수 있게 해줌

🚨 주의할 점

🔹 1. null은 항상 false

Animal a = null;
System.out.println(a instanceof Animal); // false

→ 이유: null은 어떤 객체도 가리키지 않기 때문에, 타입과 무관하게 무조건 false


🔹 2. 형변환을 잘못하면 ClassCastException

→ 그래서 instanceof로 먼저 확인하고 형변환해야 해

Animal a = new Animal();
Dog d = (Dog) a; // 런타임 오류! Animal은 Dog가 아님!

해결 방법:

if (a instanceof Dog) {
    Dog d = (Dog) a; // 안전하게 캐스팅
}

🔹 3. 상속 구조만 검사됨

instanceof상속 관계 또는 인터페이스 구현 관계가 있어야 검사 가능해.

예:

interface Pet {}
class Cat implements Pet {}

Pet p = new Cat();
System.out.println(p instanceof Cat); // true


🧠 요약

항목설명
역할객체가 특정 타입인지 확인 (true/false)
언제 사용?다운캐스팅 전에 안전성 검사할 때, 타입에 따라 분기할 때
특징런타임 시점 검사, 상속·인터페이스만 가능, null은 항상 false
주의점instanceof 없이 형변환하면 위험, 반드시 체크하고 캐스팅할 것

✅ 문제 13 정답:

public class Main {
    public static void main(String[] args) {
        try {
            int x = 5 / 0;
            System.out.println("Try block");
        } catch (ArithmeticException e) {
            System.out.println("Catch block");
        } finally {
            System.out.println("Finally block");
        }
    }
}
Catch block  
Finally block
  • 5 / 0 예외 발생 → try 블록 종료, catch로 이동 → 그 후 finally는 항상 실행됨

✅ 문제 14 정답:

class Test {
    static {
        System.out.println("Static Block");
    }

    Test() {
        System.out.println("Constructor");
    }
}

public class Main {
    public static void main(String[] args) {
        Test t1 = new Test();
        Test t2 = new Test();
    }
}
Static Block  
Constructor  
Constructor
  • 클래스 최초 로딩 시 static block단 한 번만 실행
  • 이후 생성자 호출은 객체마다 실행됨

✅ 문제 15 정답:

class Parent {
    int x = 10;
}

class Child extends Parent {
    int x = 20;

    void print() {
        System.out.println("x = " + x);
        System.out.println("super.x = " + super.x);
    }
}

public class Main {
    public static void main(String[] args) {
        Child c = new Child();
        c.print();
    }
}
x = 20  
super.x = 10
  • Child 클래스에 x가 다시 정의됨 (shadowing)
  • super.x는 부모의 필드 접근

✅ 문제 16 정답:

class Holder {
    int val = 10;
}

public class Main {
    public static void change(int x, Holder h) {
        x = 20;
        h.val = 30;
    }

    public static void main(String[] args) {
        int a = 5;
        Holder h = new Holder();

        change(a, h);
        System.out.println("a = " + a);
        System.out.println("h.val = " + h.val);
    }
}
a = 5  
h.val = 30
  • int는 기본형 → 함수 내 변경이 원본에 영향 X
  • Holder는 참조형 → 객체 내부 값 변경은 반영됨

🔄 전체 정답 요약

문제 번호개념/트릭 주제정답 / 출력
11String 불변성, StringBuilder 가변성abc / abcdef
12instanceof 다형성 조건 검사Animal Dog
13예외 처리 흐름 + finallyCatch block / Finally block
14static block vs constructor 순서Static Block / Constructor x2
15변수 숨김 (shadowing)20 / 10
16기본형 vs 참조형 인자 차이5 / 30

💡 마무리 요약: 이렇게 정리해두면 실기에서 틀릴 일 없음

구분개념 요약
String vs StringBuilderString은 immutable, append는 영향 없음
instanceof상속 받은 객체는 부모와 자식 모두 만족
try-catch-finallyfinally는 무조건 실행됨
static vs constructorstatic block은 클래스 로딩 시 1회
변수 shadowing같은 이름이면 자식 변수 우선, super로 부모 접근
기본형 vs 참조형기본형은 값 복사, 참조형은 주소 복사 (내부 변화 반영됨)

좋아! 이번엔 지금까지 나온 걸 제외한 새로운 트랩 유형 위주로 문제를 구성할게.
실제 정보처리기사 실기 시험이나 객관식/단답식/코드 실행 결과 예측 문제로 나올 수 있는 스타일로 낼게.


✅ 이번 주제의 포인트

유형 번호개념 트랩 주제
17배열을 파라미터로 넘길 때 주의할 점
18배열 초기화 안 한 상태에서의 값
19조건문의 중첩과 else 붙는 위치
20char와 int의 연산
21자동 형변환 vs 명시적 형변환
22전위/후위 연산자의 동작 순서

🧠 정답 + 해설


public class Main {
    public static void modify(int[] arr) {
        arr[0] = 999;
    }

    public static void main(String[] args) {
        int[] nums = {1, 2, 3};
        modify(nums);
        System.out.println(nums[0]);
    }
}

✅ 문제 17 정답: 999

  • 배열은 참조형 → 함수 내에서 arr[0] = 999 하면 원본도 바뀜

✅ 문제 18 정답: 0

public class Main {
    public static void main(String[] args) {
        int[] a = new int[3];
        System.out.println(a[1]);
    }
}
  • 자바의 int[]는 선언 시 모든 값이 0으로 초기화됨

✅ 문제 19 정답: B

public class Main {
    public static void main(String[] args) {
        int x = 5;

        if (x > 3)
            if (x > 10)
                System.out.println("A");
            else
                System.out.println("B");
    }
}
  • else는 가장 가까운 if에 붙음
if (x > 3) // true
   if (x > 10) // false
       ...
   else         // ← 이 else는 위의 if(x > 10)에 붙음

✅ 문제 20 정답: 66

public class Main {
    public static void main(String[] args) {
        char ch = 'A';
        int val = ch + 1;
        System.out.println(val);
    }
}
  • 'A'는 유니코드 65
  • 65 + 1 = 66

✅ 문제 21 정답: -118

public class Main {
    public static void main(String[] args) {
        byte b = 10;
        int i = b;
        byte c = (byte)(i + 128);
        System.out.println(c);
    }
}
  • i + 128 = 10 + 128 = 138

  • (byte)138 → 바이트 오버플로우:

    138 - 256 = -118

✅ 문제 22 정답: 5 + 7 = 12

public class Main {
    public static void main(String[] args) {
        int a = 5;
        int b = a++ + ++a;
        System.out.println(b);
    }
}
  • a++ → 5 (후위, 사용 후 증가)
  • ++a → 7 (앞에서 이미 6 되었음, 전위 증가로 7)

정답: 12


✅ 전체 정답 요약표

문제 번호개념 주제정답
17배열 참조999
18배열 초기값0
19중첩 if-elseB
20char + int 연산66
21형변환 overflow-118
22전위/후위 연산 순서12

🧠 요약으로 암기하자!

트릭 구분핵심 요약 설명
배열 전달참조형이므로 내부 변경 = 외부 변경
int[] 선언자동으로 0으로 초기화
else 위치가장 가까운 if와 짝을 이룸
char + intchar는 숫자처럼 계산됨
byte 형변환128 이상이면 오버플로우
a++ + ++a후위 먼저, 그다음 전위 증가

0개의 댓글