파이썬 객관식 심화 — 핵심 SET 14¶
이름: ____________ 점수: _____ / 100 난이도: ★★★★★
각 코드의 실행 결과 또는 옳은 설명을 보기에서 하나 고르세요. (문항당 10점, 범위: 함수·클래스·모듈/패키지·딕셔너리·리스트·튜플·set 심화)
1. (함수) 다음 코드의 출력은?¶
def f(a, b, *args):
return a + b + sum(args)
def g(a, b, *args, **kwargs):
total = a + b + sum(args)
if "bonus" in kwargs:
total += kwargs["bonus"]
return total
print(f(1, 2, 3, 4, 5), g(1, 2, 3, 4, bonus=10))
① 15 20
② 15 10
③ 12 22
④ TypeError
⑤ 15 15
2. (함수) 다음 코드의 출력은?¶
def stamp(items, tag=None):
if tag is None:
tag = []
tag.append("done")
return tag
t1 = stamp([1], ["start"])
t2 = stamp([2])
t3 = stamp([3], t2)
print(t1, t2, t3, t2 is t3)
① ['start', 'done'] ['done', 'done'] ['done', 'done'] True
② ['start', 'done'] ['done'] ['done', 'done'] False
③ ['start', 'done'] ['done', 'done'] ['done', 'done'] False
④ Error
⑤ ['start', 'done'] ['done'] ['done'] True
3. (클래스) 다음 코드의 출력은?¶
class Shape:
def __init__(self, name, sides):
self.name = name
self.sides = sides
def __str__(self):
return f"{self.name}({self.sides})"
shapes = [Shape("Triangle", 3), Shape("Square", 4), Shape("Pentagon", 5)]
summary = ", ".join(str(s) for s in shapes)
print(summary)
① Triangle(3), Square(4), Pentagon(5)
② <Shape object>, <Shape object>, <Shape object>
③ Triangle, Square, Pentagon
④ TypeError
⑤ [Triangle(3), Square(4), Pentagon(5)]
4. (모듈/패키지) 다음 코드의 출력은?¶
import math
def hypotenuse(a, b):
return math.sqrt(a ** 2 + b ** 2)
sides = [(3, 4), (6, 8), (5, 12)]
results = [round(hypotenuse(a, b), 1) for a, b in sides]
print(results)
① [5.0, 10.0, 13.0]
② [5, 10, 13]
③ [25, 100, 169]
④ TypeError
⑤ [5.0, 10.0, 13]
5. (딕셔너리) 다음 코드의 출력은?¶
inventory = {"apple": 10, "banana": 5, "cherry": 0}
for key in inventory:
inventory[key] += 1
removed = [k for k, v in inventory.items() if v <= 1]
for k in removed:
del inventory[k]
print(inventory)
① {'apple': 11, 'banana': 6}
② {'apple': 10, 'banana': 5, 'cherry': 0}
③ RuntimeError
④ {'apple': 11, 'banana': 6, 'cherry': 1}
⑤ Error
6. (리스트) 다음 코드의 출력은?¶
a = [1, 2, 3, 4]
for x in a:
a.append(x)
if len(a) > 9:
break
print(a)
① [1, 2, 3, 4, 1, 2, 3, 4, 1, 2]
② [1, 2, 3, 4]
③ 무한 루프
④ Error
⑤ [1, 2, 3, 4, 1, 2, 3, 4, 1]
7. (튜플) 다음 코드의 출력은?¶
records = [("Kim", 90), ("Lee", 85), ("Park", 95), ("Choi", 70)]
ranked = sorted(records, key=lambda r: r[1], reverse=True)
top2 = ranked[:2]
print(top2, ranked[-1])
① [('Park', 95), ('Kim', 90)] ('Choi', 70)
② [('Kim', 90), ('Park', 95)] ('Choi', 70)
③ [('Park', 95), ('Kim', 90)] ('Park', 95)
④ Error
⑤ [('Choi', 70), ('Lee', 85)] ('Park', 95)
8. (set) 다음 코드의 출력은?¶
allowed = {"read", "write", "execute"}
requested = {"read", "delete", "execute"}
denied = requested - allowed
granted = requested & allowed
print(sorted(denied), sorted(granted))
① ['delete'] ['execute', 'read']
② ['read'] ['delete', 'execute']
③ ['delete', 'execute'] ['read']
④ Error
⑤ [] ['delete', 'execute', 'read']
9. (클래스) 다음 코드의 출력은?¶
class BankAccount:
def __init__(self, owner, balance=0):
self.owner = owner
self.balance = balance
def __eq__(self, other):
return self.balance == other.balance
accounts = [BankAccount("Kim", 100), BankAccount("Lee", 100), BankAccount("Park", 50)]
count = 0
for i in range(len(accounts)):
for j in range(i + 1, len(accounts)):
if accounts[i] == accounts[j]:
count += 1
print(count)
① 1
② 2
③ 0
④ 3
⑤ Error
10. (패키지) 다음 코드의 출력은?¶
# tools/__init__.py
from .text import shout
# tools/text.py
def shout(s):
return s.upper()
def whisper(s):
return s.lower()
# main.py
import tools
print(tools.shout("hi"))
print(tools.whisper("HI"))
① HI 출력 후 AttributeError
② HI hi
③ AttributeError AttributeError
④ hi HI
⑤ ImportError