파이썬 객관식 심화 — 핵심 SET 15¶
이름: ____________ 점수: _____ / 100 난이도: ★★★★★
각 코드의 실행 결과 또는 옳은 설명을 보기에서 하나 고르세요. (문항당 10점, 범위: 함수·클래스·모듈/패키지·딕셔너리·리스트·튜플·set 심화)
1. (함수) 다음 코드의 출력은?¶
def power(base, exp=2, mod=None):
result = base ** exp
if mod is not None:
result %= mod
return result
print(power(3))
print(power(3, 3))
print(power(exp=1, base=5))
print(power(10, 2, mod=7))
① 9 27 5 2
② 9 27 5 100
③ 6 9 5 2
④ TypeError
⑤ 9 9 5 2
2. (함수) 다음 코드의 출력은?¶
def f(n):
result = []
for i in range(n):
if i % 2 == 0:
result.append(i * i)
else:
result.append(-i)
return result
print(f(6))
① [0, -1, 4, -3, 16, -5]
② [0, 1, 4, 9, 16, 25]
③ [0, -1, 2, -3, 4, -5]
④ Error
⑤ [0, -1, 4, -3, 16]
3. (클래스) 다음 코드의 출력은?¶
class Queue:
def __init__(self):
self.items = []
def enqueue(self, x):
self.items.append(x)
def dequeue(self):
return self.items.pop(0)
def is_empty(self):
return len(self.items) == 0
q = Queue()
for v in [1, 2, 3, 4]:
q.enqueue(v)
q.dequeue()
q.dequeue()
q.enqueue(5)
print(q.items, q.is_empty())
① [3, 4, 5] False
② [1, 2, 3, 4, 5] False
③ [3, 4, 5] True
④ Error
⑤ [4, 5] False
4. (모듈/패키지) 다음 설명 중 틀린 것은?¶
① random.seed()로 시드를 고정하면 이후 호출되는 난수 함수들의 결과가 항상 같아진다
② math.floor()와 math.ceil()은 항상 정수를 반환한다
③ random.choice()는 빈 리스트를 넣으면 IndexError가 발생한다
④ math.sqrt()에 음수를 넣으면 ValueError가 발생한다
⑤ random 모듈의 함수들은 항상 정수만 반환한다
5. (딕셔너리) 다음 코드의 출력은?¶
d = {"a": 1, "b": 2, "c": 3, "d": 4}
keys = list(d.keys())
values = list(d.values())
pairs = list(zip(keys, values))
print(keys[0], values[-1], pairs[2])
① a 4 ('c', 3)
② d 1 ('a', 1)
③ a 4 ('c', 4)
④ Error
⑤ a 1 ('c', 3)
6. (리스트) 다음 코드의 출력은?¶
a = [[1, 2], [3, 4], [5, 6]]
b = a.copy()
c = list(a)
b[0].append(99)
c.append([7, 8])
print(a, b, c)
① [[1, 2, 99], [3, 4], [5, 6]] [[1, 2, 99], [3, 4], [5, 6]] [[1, 2, 99], [3, 4], [5, 6], [7, 8]]
② [[1, 2], [3, 4], [5, 6]] [[1, 2, 99], [3, 4], [5, 6]] [[1, 2], [3, 4], [5, 6], [7, 8]]
③ [[1, 2, 99], [3, 4], [5, 6]] [[1, 2, 99], [3, 4], [5, 6]] [[1, 2], [3, 4], [5, 6], [7, 8]]
④ Error
⑤ [[1, 2], [3, 4], [5, 6]] [[1, 2], [3, 4], [5, 6]] [[1, 2], [3, 4], [5, 6]]
7. (튜플) 다음 코드의 출력은?¶
def split_name(full):
parts = full.split()
first, last = parts[0], parts[-1]
return (last, first, len(parts))
print(split_name("Kim Min Su"))
① ('Su', 'Kim', 3)
② ('Kim', 'Su', 3)
③ ('Su', 'Kim', 2)
④ TypeError
⑤ ValueError
8. (set) 다음 코드의 출력은?¶
a = {1, 2, 3}
a.add(2)
a.add(4)
a.discard(1)
a.discard(10)
print(len(a), sorted(a))
① 3 [2, 3, 4]
② 4 [1, 2, 3, 4]
③ 3 [1, 2, 3]
④ KeyError
⑤ 2 [2, 4]
9. (함수) 다음 코드의 출력은?¶
def total_price(price, qty=1, discount=0, tax=0.0):
subtotal = price * qty * (1 - discount)
return round(subtotal * (1 + tax), 2)
print(total_price(1000, discount=0.1, qty=2, tax=0.05))
① 1890.0
② 1800.0
③ 2000.0
④ 1980.0
⑤ TypeError
10. (패키지) 다음 중 패키지를 만들 때 가장 핵심적인 요소는?¶
① main.py 파일의 존재
② 폴더 안에 __init__.py 파일을 두는 것
③ 모든 함수에 self를 붙이는 것
④ 모듈 이름이 영어여야 하는 것
⑤ if __name__ == "__main__": 블록의 존재