파이썬 객관식 심화 — 핵심 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.py와 math_utils.py의 이름이 같아야 한다
② utils 폴더에 __init__.py 파일이 있어야 한다
③ math_utils.py 안에 if __name__ == "__main__":이 있어야 한다
④ utils 폴더가 main.py보다 먼저 실행되어야 한다
⑤ add 함수에 self가 있어야 한다