파이썬 객관식 심화 — 핵심 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