콘텐츠로 이동

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

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

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


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

def summarize(name, *scores, **bonus):
    total = sum(scores)
    if "extra" in bonus:
        total += bonus["extra"]
    avg = total / len(scores)
    return name, avg

print(summarize("Kim", 80, 90, 100, extra=30))

('Kim', 100.0)
('Kim', 90.0)
('Kim', 300)
TypeError
('Kim', 100)

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

def calc(x, y, z=0, *, mode="add"):
    if mode == "add":
        return x + y + z
    elif mode == "mul":
        return x * y * z
    return None

print(calc(2, 3, z=4, mode="mul"))
print(calc(1, z=5, y=2))

24 8
8 24
24 None
TypeError
SyntaxError

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

class Stack:
    def __init__(self):
        self.data = []
    def push(self, x):
        self.data.append(x)
    def pop(self):
        return self.data.pop()
    def peek(self):
        return self.data[-1] if self.data else None

s = Stack()
s.push(1)
s.push(2)
s.push(3)
s.pop()
s.push(4)
print(s.peek(), s.data)

4 [1, 2, 4]
3 [1, 2, 3]
4 [1, 2, 3, 4]
2 [1, 2, 4]
Error

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

import math

def area_ratio(r1, r2):
    a1 = math.pi * r1 ** 2
    a2 = math.pi * r2 ** 2
    return round(a1 / a2, 2)

print(area_ratio(4, 2))

4.0
2.0
16.0
TypeError
0.25

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

words = ["apple", "kiwi", "banana", "fig", "cherry"]
lengths = {w: len(w) for w in words if len(w) > 3}
shortest = min(lengths, key=lengths.get)
print(lengths, shortest)

{'apple': 5, 'kiwi': 4, 'banana': 6, 'cherry': 6} kiwi
{'apple': 5, 'kiwi': 4, 'banana': 6, 'cherry': 6, 'fig': 3} fig
{'apple': 5, 'kiwi': 4, 'banana': 6, 'cherry': 6} apple
Error
{'kiwi': 4} kiwi

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

a = [3, 1, 4, 1, 5, 9, 2, 6]
b = [x for x in a if x > 3]
print(a.index(4), a.count(1), b)

2 2 [4, 5, 9, 6]
2 1 [4, 5, 9, 6]
4 2 [4, 5, 9, 6]
2 2 [3, 4, 5, 9, 6]
Error

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

t = (1, 2, 2, 3, 2, 4)
first, *middle, last = t
print(t.count(2), t.index(3), first, middle, last)

3 3 1 [2, 2, 3, 2] 4
3 3 1 [2, 2, 2, 3] 4
2 3 1 [2, 2, 3, 2] 4
Error
3 3 1 (2, 2, 3, 2) 4

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

a = {x for x in range(6) if x % 2 == 0}
b = {0, 2, 4}
c = a
print(a == b, a is b, a is c, b is c)

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

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

def safe_divide(a, b):
    try:
        return a / b
    except ZeroDivisionError:
        return None

results = [safe_divide(10, 2), safe_divide(5, 0), safe_divide(9, 3)]
total = sum(r for r in results if r is not None)
print(results, total)

[5.0, None, 3.0] 8.0
[5.0, None, 3.0] 8
[5.0, 0, 3.0] 8.0
TypeError
[5.0, None, 3.0] None

10. (패키지) 다음 중 모듈과 패키지의 관계를 옳게 설명한 것은?

① 패키지는 모듈보다 항상 더 작은 단위이다 ② 모듈은 .py 파일 하나, 패키지는 그런 모듈들을 묶은 폴더이다 ③ 패키지 안에는 모듈을 둘 수 없다 ④ 모듈은 폴더 단위로만 정의된다 ⑤ __init__.py는 모듈이 아니라 함수이다