파이썬 객관식 심화 — 핵심 SET 06¶
이름: ____________ 점수: _____ / 100 난이도: ★★★★★
각 코드의 실행 결과 또는 옳은 설명을 보기에서 하나 고르세요. (문항당 10점, 범위: 함수·클래스·모듈/패키지·딕셔너리·리스트·튜플·set 심화)
1. (함수) 다음 코드의 출력은?¶
def compute(a, b, c=1, *extra, **opts):
total = a - b + c
for e in extra:
total += e
if opts.get("double"):
total *= 2
return total
print(compute(10, 3, 2, 5, 5, double=True))
① 19
② 9
③ 38
④ TypeError
⑤ 18
2. (함수) 다음 코드의 출력은?¶
def classify(n):
if n > 100:
return "huge"
elif n > 10:
if n % 2 == 0:
return "big-even"
return "big-odd"
elif n > 0:
return "small"
results = [classify(150), classify(50), classify(11), classify(5), classify(-3)]
print(results)
① ['huge', 'big-even', 'big-odd', 'small', None]
② ['huge', 'big-odd', 'big-even', 'small', None]
③ ['huge', 'big-even', 'big-odd', 'small', 'None']
④ ['huge', 'big-even', 'big-odd', None, None]
⑤ TypeError
3. (클래스) 다음 코드의 출력은?¶
class Account:
bank_fee = 5
def __init__(self, balance):
self.balance = balance
self.history = []
def withdraw(self, amt):
total = amt + Account.bank_fee
if total > self.balance:
self.history.append("fail")
return False
self.balance -= total
self.history.append("ok")
return True
a = Account(100)
a.withdraw(50)
a.withdraw(40)
a.withdraw(10)
print(a.balance, a.history)
① 45 ['ok', 'ok', 'fail']
② 0 ['ok', 'fail', 'fail']
③ -10 ['ok', 'ok', 'ok']
④ 0 ['ok', 'ok', 'fail']
⑤ Error
4. (모듈/패키지) 다음 설명 중 틀린 것은?¶
① 모듈은 함수·변수·클래스를 담은 하나의 .py 파일이며, 같은 모듈을 여러 파일에서 import해도 코드는 한 번만 실행된다
② import module as alias로 가져오면 원래 이름 module은 현재 네임스페이스에 존재하지 않는다
③ 패키지로 인식되기 위해 폴더 안에 있어야 하는 __init__.py는 내용이 비어 있어도 무방하다
④ from module import *를 사용하면 module이라는 이름도 함께 현재 네임스페이스에 만들어진다
⑤ 패키지 안의 서브모듈은 import pkg.sub처럼 점(.) 표기로 접근 가능한 계층 구조를 가진다
5. (딕셔너리) 다음 코드의 출력은?¶
raw = [("a", 1), ("b", 2), ("a", 3), ("c", 4), ("b", 5)]
d = dict(raw)
d["d"] = d.get("a", 0) + d.get("c", 0)
del d["b"]
print(d)
① {'a': 1, 'c': 4, 'd': 5}
② {'a': 3, 'c': 4, 'd': 7}
③ {'a': 3, 'b': 5, 'c': 4, 'd': 7}
④ {'a': 3, 'c': 4, 'd': 4}
⑤ KeyError
6. (리스트) 다음 코드의 출력은?¶
a = [5, 3, 8, 1, 9, 2]
b = sorted(a)[1:4]
a.sort(reverse=True)
c = a[-3:]
print(b, c)
① [2, 3, 5] [3, 2, 1]
② [3, 5, 8] [1, 2, 3]
③ [2, 3, 5] [1, 2, 3]
④ [1, 2, 3] [3, 2, 1]
⑤ Error
7. (튜플) 다음 코드의 출력은?¶
points = [(1, 2), (3, 4), (5, 6)]
total_x, total_y = 0, 0
for x, y in points:
total_x += x
total_y += y
result = (total_x, total_y, total_x + total_y)
print(result)
① (1, 2, 21)
② (21, 9, 12)
③ (9, 12, 21)
④ (9, 12, 9)
⑤ Error
8. (set) 다음 코드의 출력은?¶
a = {1, 2, 3, 4, 5}
b = {4, 5, 6, 7}
c = {5, 6, 8}
result = (a & b) | (b - c)
print(sorted(result))
① [4, 5, 7]
② [4, 5]
③ [4, 7]
④ [4, 5, 6, 7]
⑤ Error
9. (클래스) 다음 코드의 출력은?¶
class Employee:
def __init__(self, name, salary):
self.name = name
self.salary = salary
def bonus(self, rate):
return self.salary * rate
def __repr__(self):
return f"{self.name}:{self.salary}"
team = [Employee("Kim", 3000), Employee("Lee", 4000), Employee("Park", 2000)]
total_bonus = sum(e.bonus(0.1) for e in team)
top = max(team, key=lambda e: e.salary)
print(total_bonus, top)
① 900 Lee:4000
② 900.0 Lee:4000
③ 900.0 Kim:3000
④ 900.0 <Employee object>
⑤ TypeError
10. (패키지) 다음 코드의 출력은?¶
# tools/format.py
def bold(s):
return f"**{s}**"
# tools/__init__.py
from .format import bold
COUNT = 0
# main.py
import tools
tools.COUNT += 1
tools.COUNT += 1
print(tools.bold("hi"), tools.COUNT)
① **hi** 2
② hi 2
③ **hi** 0
④ AttributeError
⑤ NameError