콘텐츠로 이동

파이썬 객관식 심화 — 핵심 SET 12

이름: ____________ 점수: _____ / 100 난이도: ★★★★★

각 코드의 실행 결과 또는 옳은 설명을 보기에서 하나 고르세요. (문항당 10점, 범위: 함수·클래스·모듈/패키지·딕셔너리·리스트·튜플·set 심화)


1. (함수) 다음 코드의 출력은?

def tag(text, prefix="[", suffix="]", repeat=1):
    return (prefix + text + suffix) * repeat

print(tag("alert", suffix="!", repeat=2))

[alert![alert!
[alert!]
alert!alert!
TypeError
[alert!

2. (함수) 다음 코드의 출력은?

count = 0

def increment():
    count = count + 1
    return count

def reset():
    global count
    count = 0
    return count

print(reset())
try:
    print(increment())
except UnboundLocalError:
    print("error")

0 error
0 1
error error
NameError
1 error

3. (클래스) 다음 코드의 출력은?

class Temperature:
    def __init__(self, celsius):
        self.celsius = celsius
    def to_fahrenheit(self):
        return self.celsius * 9 / 5 + 32
    def adjust(self, delta):
        self.celsius += delta
        return self.celsius

t = Temperature(20)
t.adjust(5)
t.adjust(-2)
print(t.to_fahrenheit())

73.4
68.0
75.4
41.4
TypeError

4. (모듈/패키지) 다음 코드의 출력은?

import random

random.seed(7)
results = [random.randint(1, 10) for _ in range(5)]
print(len(results), max(results) <= 10, min(results) >= 1)

5 True True
5 False True
10 True True
Error
5 True False

5. (딕셔너리) 다음 코드의 출력은?

d1 = {"a": 1, "b": 2}
d2 = {"b": 3, "c": 4}
d3 = {"c": 5, "d": 6}
d1.update(d2)
d1.update(d3)
print(d1)

{'a': 1, 'b': 3, 'c': 5, 'd': 6}
{'a': 1, 'b': 2, 'c': 4, 'd': 6}
{'a': 1, 'b': 3, 'c': 4, 'd': 6}
Error
{'b': 3, 'c': 5, 'd': 6}

6. (리스트) 다음 코드의 출력은?

a = [5, 3, 8, 1, 9, 2]
a.sort()
a.reverse()
b = a[:3]
a.sort(reverse=True)
print(b, a)

[9, 8, 5] [9, 8, 5, 3, 2, 1]
[1, 2, 3] [9, 8, 5, 3, 2, 1]
[9, 8, 5] [1, 2, 3, 5, 8, 9]
Error
[5, 8, 9] [9, 8, 5, 3, 2, 1]

7. (튜플) 다음 코드의 출력은?

points = [(1, 2), (3, 1), (2, 5), (0, 4)]
points.sort(key=lambda p: (p[1], p[0]))
print(points)

[(3, 1), (1, 2), (0, 4), (2, 5)]
[(1, 2), (3, 1), (2, 5), (0, 4)]
[(0, 4), (1, 2), (2, 5), (3, 1)]
Error
[(2, 5), (0, 4), (1, 2), (3, 1)]

8. (set) 다음 코드의 출력은?

a = {1, 2, 3}
b = {2, 3, 4}
c = {3, 4, 5}
result = (a ^ b) ^ c
print(result)

{1, 3, 5}
{1, 4, 5}
{2, 3}
{1, 2, 3, 4, 5}
Error

9. (클래스) 다음 코드의 출력은?

class Inventory:
    def __init__(self):
        self.stock = {}
    def add(self, item, qty):
        self.stock[item] = self.stock.get(item, 0) + qty
    def remove(self, item, qty):
        if self.stock.get(item, 0) < qty:
            return False
        self.stock[item] -= qty
        return True

inv = Inventory()
inv.add("apple", 5)
inv.add("apple", 3)
inv.remove("apple", 4)
inv.add("banana", 2)
print(inv.stock)

{'apple': 4, 'banana': 2}
{'apple': 8, 'banana': 2}
{'apple': 4}
Error
{'apple': 8, 'banana': 0}

10. (패키지) 다음 구조에서 from utils.math_utils import add가 정상 동작하려면 반드시 필요한 것은?

my_project/
├── main.py
└── utils/
    └── math_utils.py

main.pymath_utils.py의 이름이 같아야 한다 ② utils 폴더에 __init__.py 파일이 있어야 한다 ③ math_utils.py 안에 if __name__ == "__main__":이 있어야 한다 ④ utils 폴더가 main.py보다 먼저 실행되어야 한다 ⑤ add 함수에 self가 있어야 한다