콘텐츠로 이동

파이썬 객관식 심화 — 핵심 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