정보시스템감리사/기출문제_19년_20회

46_java_code분석

론리나잇 2025. 4. 29. 21:57

📌 문제 46. 다음 JAVA 코드를 실행한 결과 중, 가장 적절한 것은?

🔸 코드 분석

import java.util.*;

public class TestB {
    public static int list = 0, map = 0, set = 0, collection = 0;

    public static void collectionCounter(Object abc) {
        if (abc instanceof List) list++;
        if (abc instanceof Map) map++;
        if (abc instanceof Set) set++;
        if (abc instanceof Collection) collection++;
    }

    public static void main(String[] args) {
        collectionCounter(new HashSet<Object>());           // Set, Collection
        collectionCounter(new HashMap<Object, Object>());   // Map
        collectionCounter(new ArrayList<Object>());         // List, Collection
        collectionCounter(new LinkedList<Object>());        // List, Collection
        collectionCounter(new Stack<Object>());             // List, Collection
        collectionCounter(new TreeSet<Object>());           // Set, Collection
        collectionCounter(new TreeMap<Object, Object>());   // Map

        System.out.println("list = " + list + " map = " + map + 
                           " set = " + set + " collection = " + collection);
    }
}

🔸 클래스별 타입 확인

클래스 List Map Set Collection
HashSet
HashMap
ArrayList
LinkedList
Stack
TreeSet
TreeMap

✅ 최종 결과

  • list = 3 → ArrayList, LinkedList, Stack
  • map = 2 → HashMap, TreeMap
  • set = 2 → HashSet, TreeSet
  • collection = 5 → HashSet, ArrayList, LinkedList, Stack, TreeSet

🔹 정답: ③ list = 3 map = 2 set = 2 collection = 5