파이썬 객관식 심화 — 핵심 SET 08¶
이름: ____________ 점수: _____ / 100 난이도: ★★★★★
각 코드의 실행 결과 또는 옳은 설명을 보기에서 하나 고르세요. (문항당 10점, 범위: 함수·클래스·모듈/패키지·딕셔너리·리스트·튜플·set 심화)
1. (함수) 다음 코드의 출력은?¶
def double(n):
return n * 2
def triple(n):
return n * 3
def apply_chain(funcs, val):
for f in funcs:
val = f(val)
return val
print(apply_chain([double, triple, double], 2))
① 12
② 24
③ 36
④ TypeError
⑤ 8
2. (함수) 다음 코드의 출력은?¶
counter = 0
def increment(step):
global counter
counter += step
return counter
print(increment(3))
print(increment(5))
print(counter)
① 3 8 8
② 3 5 8
③ 0 0 0
④ UnboundLocalError
⑤ 3 8 0
3. (클래스) 다음 코드의 출력은?¶
class Counter:
total_count = 0
def __init__(self, start=0):
self.value = start
Counter.total_count += 1
def increment(self, by=1):
self.value += by
return self.value
c1 = Counter()
c2 = Counter(10)
c1.increment()
c1.increment(5)
c2.increment(2)
print(c1.value, c2.value, Counter.total_count)
① 6 12 2
② 6 12 0
③ 1 12 2
④ 6 10 2
⑤ Error
4. (모듈/패키지) 다음 코드의 출력은?¶
import random
random.seed(10)
nums = [random.randint(1, 5) for _ in range(3)]
print(len(nums), all(1 <= n <= 5 for n in nums))
① 3 True
② 3 False
③ 5 True
④ Error
⑤ 1 True
5. (딕셔너리) 다음 코드의 출력은?¶
d = {"a": 10, "b": 20, "c": 30}
total = 0
for k in d:
if d[k] >= 20:
total += d[k]
d[k] = 0
print(d, total)
① {'a': 10, 'b': 0, 'c': 0} 50
② {'a': 10, 'b': 20, 'c': 30} 50
③ {'a': 0, 'b': 0, 'c': 0} 60
④ RuntimeError
⑤ {'a': 10, 'b': 0, 'c': 0} 30
6. (리스트) 다음 코드의 출력은?¶
a = [10, 20, 30, 40, 50, 60, 70]
b = a[1:6:2]
c = a[::-1][:3]
print(b, c)
① [20, 40, 60] [70, 60, 50]
② [20, 30, 40] [10, 20, 30]
③ [20, 40, 60] [10, 20, 30]
④ [10, 30, 50] [70, 60, 50]
⑤ Error
7. (튜플) 다음 코드의 출력은?¶
data = [(1, "a"), (3, "b"), (2, "c")]
data_sorted = sorted(data, key=lambda t: t[0], reverse=True)
first, second, third = data_sorted
print(first, second[1], third[0])
① (3, 'b') c 1
② (1, 'a') a 3
③ (3, 'b') b 1
④ Error
⑤ (3, 'b') c 3
8. (set) 다음 코드의 출력은?¶
a = {1, 2, 3, 4}
b = {3, 4, 5}
c = {5, 6, 7}
result = a - b | b & c
print(sorted(result))
① [1, 2, 5]
② [1, 2, 4, 5]
③ [3, 4, 5]
④ [1, 2]
⑤ Error
9. (클래스) 다음 코드의 출력은?¶
class Item:
def __init__(self, name, price):
self.name = name
self.price = price
def discount(self, rate):
self.price *= (1 - rate)
return round(self.price, 1)
def __repr__(self):
return f"{self.name}:{self.price}"
cart = [Item("pen", 1000), Item("cup", 2000)]
for item in cart:
item.discount(0.5)
print(cart)
① [pen:500.0, cup:1000.0]
② [pen:500, cup:1000]
③ [pen:1000, cup:2000]
④ TypeError
⑤ [pen:500.0, cup:1000]
10. (패키지) 다음 코드의 출력은?¶
# shop/__init__.py
from .cart import Cart
VERSION = "1.0"
# shop/cart.py
class Cart:
def __init__(self):
self.items = []
def add(self, item):
self.items.append(item)
# main.py
import shop
c = shop.Cart()
c.add("apple")
print(shop.VERSION, c.items)
① 1.0 ['apple']
② AttributeError
③ 1.0 []
④ ImportError
⑤ ModuleNotFoundError