콘텐츠로 이동

파이썬 객관식 심화 — 핵심 SET 11

이름: ____________ 점수: _____ / 100 난이도: ★★★★★

각 코드의 실행 결과 또는 옳은 설명을 보기에서 하나 고르세요. (문항당 10점, 범위: 함수·클래스·모듈/패키지·딕셔너리·리스트·튜플·set 심화)


1. (함수) 다음 코드의 출력은?

def greet(name, msg="Hello", punct="!"):
    return f"{msg}, {name}{punct}"

print(greet("Kim"))
print(greet("Lee", "Hi"))
print(greet("Park", punct="?"))

Hello, Kim! Hi, Lee! Hello, Park?
Kim, Hello! Lee, Hi! Park, Hello?
Hello, Kim! Lee, Hi! Hello, Park?
TypeError
Hello, Kim! Hi, Kim! Hello, Park?

2. (함수) 다음 코드의 출력은?

def f():
    pass

def g():
    return None

results = [f() == g(), f() is g(), type(f()) == type(g())]
print(results)

[True, True, True]
[False, False, True]
[True, False, True]
Error
[None, None, True]

3. (클래스) 다음 코드의 출력은?

class Node:
    def __init__(self, value, next=None):
        self.value = value
        self.next = next

n3 = Node(3)
n2 = Node(2, n3)
n1 = Node(1, n2)
total = 0
cur = n1
while cur:
    total += cur.value
    cur = cur.next
print(total, n1.next.next.value)

6 3
6 2
3 6
AttributeError
Error

4. (모듈/패키지) 다음 코드의 출력은?

import random

random.seed(5)
choices = [random.choice(["a", "b", "c"]) for _ in range(4)]
print(len(choices), set(choices).issubset({"a", "b", "c"}))

4 True
4 False
3 True
Error
1 True

5. (딕셔너리) 다음 코드의 출력은?

scores = {"kim": 90, "lee": 85}
for name in ["kim", "lee", "park", "choi"]:
    scores[name] = scores.get(name, 0) + 10
print(scores)

{'kim': 100, 'lee': 95, 'park': 10, 'choi': 10}
{'kim': 90, 'lee': 85, 'park': 10, 'choi': 10}
KeyError
{'kim': 100, 'lee': 95}
Error

6. (리스트) 다음 코드의 출력은?

a = [1, 2, 3]
b = [1, 2, 3]
c = a
d = a.copy()
print(a == b, a is b, a is c, a is d)

True False True False
True True True True
False False True False
True False False True
Error

7. (튜플) 다음 코드의 출력은?

def minmax(nums):
    return (min(nums), max(nums), sum(nums))

low, high, total = minmax((4, 2, 9, 1, 7))
print(low + high, total)

10 23
23 10
9 23
1 23
TypeError

8. (set) 다음 코드의 출력은?

fruits = ["apple", "banana", "apple", "cherry", "banana", "fig", "apple"]
unique = set(fruits)
counts = {f: fruits.count(f) for f in unique}
print(len(unique), counts["apple"])

4 3
5 3
4 2
7 3
Error

9. (함수) 다음 코드의 출력은?

def process(data, *, mode="default", verbose=False):
    result = f"{data}-{mode}"
    if verbose:
        result += "!"
    return result

print(process("x", mode="fast", verbose=True))
print(process("y"))

x-fast! y-default
x-fast y-default
x-default! y-default
TypeError
SyntaxError

10. (패키지) 다음 중 import pkg.subfrom pkg import sub의 차이를 옳게 설명한 것은?

① 둘은 완전히 동일하게 동작하며 차이가 없다 ② import pkg.subsub라는 이름을 직접 만들지만 from pkg import sub는 만들지 않는다 ③ from pkg import subsub라는 이름을 직접 만들지만 import pkg.subpkg라는 이름만 만든다 ④ import pkg.sub는 패키지를 가져올 수 없다 ⑤ from pkg import sub는 항상 에러가 발생한다