콘텐츠로 이동

파이썬 객관식 심화 — 핵심 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__": 블록의 존재