콘텐츠로 이동

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

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

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


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

def calc(a, b, op="+"):
    if op == "+":
        return a + b
    elif op == "-":
        return a - b
    elif op == "*":
        return a * b
    return None

ops = ["+", "-", "*", "?"]
results = [calc(5, 3, op) for op in ops]
print(results)

[8, 2, 15, None]
[8, 2, 15, 0]
[8, 2, 15]
TypeError
[8, -2, 15, None]

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

def f(lst):
    lst = lst + [99]
    return lst

def g(lst):
    lst.append(100)
    return lst

original = [1, 2, 3]
new1 = f(original)
new2 = g(original)
print(original, new1, new2)

[1, 2, 3, 100] [1, 2, 3, 99] [1, 2, 3, 100]
[1, 2, 3] [1, 2, 3, 99] [1, 2, 3, 100]
[1, 2, 3, 99, 100] [1, 2, 3, 99, 100] [1, 2, 3, 99, 100]
Error
[1, 2, 3, 100] [1, 2, 3, 100] [1, 2, 3, 100]

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

class Employee:
    raise_rate = 1.1
    def __init__(self, salary):
        self.salary = salary
    def raise_salary(self):
        self.salary *= Employee.raise_rate
        return round(self.salary, 1)

e = Employee(1000)
e.raise_salary()
e.raise_salary()
print(e.raise_salary())

1331.0
1210.0
1100.0
1330.0
TypeError

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

import math

a = math.sqrt(16)
b = 4
print(a == b, type(a) == type(b), math.isclose(a, b))

True False True
True True True
False False True
Error
True False False

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

d = {"a": [1, 2], "b": [3, 4]}
d["a"].append(99)
d["c"] = d["a"]
d["c"].append(100)
print(d)

{'a': [1, 2, 99, 100], 'b': [3, 4], 'c': [1, 2, 99, 100]}
{'a': [1, 2, 99], 'b': [3, 4], 'c': [1, 2, 99, 100]}
{'a': [1, 2, 99], 'b': [3, 4], 'c': [100]}
KeyError
Error

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

a = [10, 20, 30]
b = a
c = a[:]
a[0] = 99
b.append(40)
print(a, b, c)

[99, 20, 30, 40] [99, 20, 30, 40] [10, 20, 30]
[99, 20, 30, 40] [99, 20, 30, 40] [99, 20, 30, 40]
[10, 20, 30] [10, 20, 30] [10, 20, 30]
Error
[99, 20, 30] [99, 20, 30, 40] [10, 20, 30]

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

def to_tuple(*args):
    return args

def merge(*tuples):
    result = ()
    for t in tuples:
        result += t
    return result

a = to_tuple(1, 2, 3)
b = to_tuple(4, 5)
print(merge(a, b), type(merge(a, b)))

(1, 2, 3, 4, 5) <class 'tuple'>
[1, 2, 3, 4, 5] <class 'list'>
(1, 2, 3, 4, 5) <class 'list'>
TypeError
(1, 2, 3) <class 'tuple'>

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

text = "Apple banana apple Cherry banana APPLE"
words = text.lower().split()
unique = set(words)
print(len(unique), sorted(unique))

3 ['apple', 'banana', 'cherry']
6 ['apple', 'apple', 'apple', 'banana', 'banana', 'cherry']
3 ['apple', 'banana', 'cherry', 'APPLE']
Error
4 ['apple', 'banana', 'cherry', 'Apple']

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

def validate(age):
    assert age >= 0, "age must be non-negative"
    return age

ages = [5, -3, 10]
results = []
for a in ages:
    try:
        results.append(validate(a))
    except AssertionError:
        results.append(None)
print(results)

[5, None, 10]
[5, -3, 10]
[5, 0, 10]
AssertionError
[None, None, None]

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

# shapes/__init__.py
from .circle import area as circle_area
from .square import area as square_area

# shapes/circle.py
def area(r):
    return 3.14 * r * r

# shapes/square.py
def area(s):
    return s * s
# main.py
import shapes
print(shapes.circle_area(2), shapes.square_area(3))

12.56 9
AttributeError
ModuleNotFoundError
NameError
ImportError