콘텐츠로 이동

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

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

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


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

def f(a, b, *args):
    return a + b + sum(args)

def g(a, b, *args, **kwargs):
    total = a + b + sum(args)
    if "bonus" in kwargs:
        total += kwargs["bonus"]
    return total

print(f(1, 2, 3, 4, 5), g(1, 2, 3, 4, bonus=10))

15 20
15 10
12 22
TypeError
15 15

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

def stamp(items, tag=None):
    if tag is None:
        tag = []
    tag.append("done")
    return tag

t1 = stamp([1], ["start"])
t2 = stamp([2])
t3 = stamp([3], t2)
print(t1, t2, t3, t2 is t3)

['start', 'done'] ['done', 'done'] ['done', 'done'] True
['start', 'done'] ['done'] ['done', 'done'] False
['start', 'done'] ['done', 'done'] ['done', 'done'] False
Error
['start', 'done'] ['done'] ['done'] True

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

class Shape:
    def __init__(self, name, sides):
        self.name = name
        self.sides = sides
    def __str__(self):
        return f"{self.name}({self.sides})"

shapes = [Shape("Triangle", 3), Shape("Square", 4), Shape("Pentagon", 5)]
summary = ", ".join(str(s) for s in shapes)
print(summary)

Triangle(3), Square(4), Pentagon(5)
<Shape object>, <Shape object>, <Shape object>
Triangle, Square, Pentagon
TypeError
[Triangle(3), Square(4), Pentagon(5)]

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

import math

def hypotenuse(a, b):
    return math.sqrt(a ** 2 + b ** 2)

sides = [(3, 4), (6, 8), (5, 12)]
results = [round(hypotenuse(a, b), 1) for a, b in sides]
print(results)

[5.0, 10.0, 13.0]
[5, 10, 13]
[25, 100, 169]
TypeError
[5.0, 10.0, 13]

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

inventory = {"apple": 10, "banana": 5, "cherry": 0}
for key in inventory:
    inventory[key] += 1
removed = [k for k, v in inventory.items() if v <= 1]
for k in removed:
    del inventory[k]
print(inventory)

{'apple': 11, 'banana': 6}
{'apple': 10, 'banana': 5, 'cherry': 0}
RuntimeError
{'apple': 11, 'banana': 6, 'cherry': 1}
Error

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

a = [1, 2, 3, 4]
for x in a:
    a.append(x)
    if len(a) > 9:
        break
print(a)

[1, 2, 3, 4, 1, 2, 3, 4, 1, 2]
[1, 2, 3, 4]
무한 루프
Error
[1, 2, 3, 4, 1, 2, 3, 4, 1]

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

records = [("Kim", 90), ("Lee", 85), ("Park", 95), ("Choi", 70)]
ranked = sorted(records, key=lambda r: r[1], reverse=True)
top2 = ranked[:2]
print(top2, ranked[-1])

[('Park', 95), ('Kim', 90)] ('Choi', 70)
[('Kim', 90), ('Park', 95)] ('Choi', 70)
[('Park', 95), ('Kim', 90)] ('Park', 95)
Error
[('Choi', 70), ('Lee', 85)] ('Park', 95)

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

allowed = {"read", "write", "execute"}
requested = {"read", "delete", "execute"}
denied = requested - allowed
granted = requested & allowed
print(sorted(denied), sorted(granted))

['delete'] ['execute', 'read']
['read'] ['delete', 'execute']
['delete', 'execute'] ['read']
Error
[] ['delete', 'execute', 'read']

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

class BankAccount:
    def __init__(self, owner, balance=0):
        self.owner = owner
        self.balance = balance
    def __eq__(self, other):
        return self.balance == other.balance

accounts = [BankAccount("Kim", 100), BankAccount("Lee", 100), BankAccount("Park", 50)]
count = 0
for i in range(len(accounts)):
    for j in range(i + 1, len(accounts)):
        if accounts[i] == accounts[j]:
            count += 1
print(count)

1
2
0
3
Error

10. (패키지) 다음 코드의 출력은?

# tools/__init__.py
from .text import shout

# tools/text.py
def shout(s):
    return s.upper()
def whisper(s):
    return s.lower()
# main.py
import tools
print(tools.shout("hi"))
print(tools.whisper("HI"))

HI 출력 후 AttributeError
HI hi
AttributeError AttributeError
hi HI
ImportError