파이썬 객관식 심화 — 핵심 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는 모듈이 아니라 함수이다