Python 3エンジニア認定基礎試験~模擬試験③~

1. 
次のコードを実行したときの出力結果は何でしょうか?

def order_summary(item, quantity=1, price=100):
total = quantity * price
return f"Order: {item}, Quantity: {quantity}, Total: {total}"

print(order_summary("Notebook", quantity=2))

2. 
次のコードについて、obj.class_variable = "new value"の結果として正しい説明を選んでください。

class MyClass:
class_variable = "shared"

obj = MyClass()
obj.class_variable = "new value"
print(MyClass.class_variable)

3. 
次のコードを実行したときの出力結果は何でしょうか?

def introduce(name, age, city="Unknown"):
return f"{name} is {age} years old and lives in {city}."

print(introduce("Sam", 30))

4. 
次のコードの実行結果を選んでください。

class MyClass:
def __init__(self, name):
self.name = name

def greet(self):
return f"Hello, {self.name}!"

obj = MyClass("Alice")
print(obj.greet())

5. 
次のコードを実行した場合、出力結果として正しいものを選んでください。

def calculate_total(price, tax=0.1, discount=0):
return price + (price * tax) - discount

print(calculate_total(100, discount=10))

6. 
次のコードを実行したときの出力結果は何でしょうか?

def calculate_area(length=5, width=3):
return length * width

print(calculate_area(width=4))

7. 
次のコードを実行したときの出力結果は何でしょうか?

def greet(name, message="Welcome", punctuation="!"):
return f"{message}, {name}{punctuation}"

print(greet("Alice", punctuation="!!!"))

8. 
次のコードを実行した場合の出力は何ですか?

s = "abcdefgh"
print(s[1:6:2])

9. 
finallyブロックの主な目的として正しいものを選んでください。

10. 
次のコードを実行した場合の出力は何ですか?

s = "abcdefghij"
print(s[2:9:3])

11. 
次のコードを実行したときの出力結果は何でしょうか?

def set_preferences(theme="Light", font_size=12, language="English"):
return f"Theme: {theme}, Font Size: {font_size}, Language: {language}"

print(set_preferences(theme="Dark", font_size=14))

12. 
次のコードの実行結果を選んでください。

class MyClass:
class_variable = "shared"

MyClass.class_variable = "modified"
print(MyClass.class_variable)

13. 
次のコードの実行結果を選んでください。

class MyClass:
def __init__(self, value):
self.value = value

def __eq__(self, other):
return self.value == other.value

obj1 = MyClass(10)
obj2 = MyClass(10)
print(obj1 == obj2)

14. 
次のコードの実行結果を選んでください。

class MyClass:
class_variable = 0

def increment(self):
MyClass.class_variable += 1

obj1 = MyClass()
obj2 = MyClass()

obj1.increment()
obj2.increment()
print(MyClass.class_variable)

15. 
次のコードにおいて、finallyブロックが実行されない場合として適切な説明を選んでください。

try:
print("Start")
raise Exception("Error occurred")
finally:
print("Cleanup")

16. 
次のコードの実行結果を選んでください。

class MyClass:
class_attribute = "shared"

obj1 = MyClass()
obj2 = MyClass()

obj1.class_attribute = "modified"
print(obj2.class_attribute)

17. 
次のコードに関する正しい説明はどれですか?

def append_item(item, items=[]):
items.append(item)
return items

18. 
hex(255)の出力はどれですか?

19. 
Pythonで文字列s = "Python Programming"の先頭から10文字を取得するスライスはどれですか?

20. 
次のうち、Pythonで整数を定義する正しい文はどれですか?

21. 
次のコードの実行結果を選んでください。

def test_function():
try:
return "Try block result"
finally:
return "Finally block result"

print(test_function())

22. 
次のコードを実行した場合の出力は何ですか?

s = "abcdefghij"
print(s[2:8:2])

23. 
デフォルト値を持つ引数の順序に関する正しい説明はどれですか?

24. 
int("101", 2)の結果はどれですか?

25. 
次のコードの実行結果を選んでください。

class MyClass:
class_variable = "shared"

obj1 = MyClass()
obj2 = MyClass()
MyClass.class_variable = "modified"

print(obj1.class_variable)
print(obj2.class_variable)

26. 
次のコードについて、staticmethodの役割として正しいものを選んでください。

class MyClass:
@staticmethod
def my_static_method():
print("This is a static method")

27. 
次のコードを実行したときの出力結果は何でしょうか?

def summarize(name, hobby="reading", age=18):
return f"{name}, {age} years old, likes {hobby}."

print(summarize("Emily", hobby="swimming"))

28. 
pow(4, 3, 5)の出力はどれですか?

29. 
次のコードの実行結果を選んでください。

class MyClass:
class_variable = 100

obj = MyClass()
del obj.class_variable
print(obj.class_variable)

30. 
次のコードの実行結果を選んでください。

class MyClass:
class_variable = [1, 2, 3]

obj = MyClass()
obj.class_variable = [4, 5, 6]

print(MyClass.class_variable)

31. 
次のコードについて、finallyブロックが実行されるタイミングとして正しいものを選んでください。

try:
raise ValueError("An error occurred")
except ValueError:
print("Caught ValueError")
finally:
print("Executing cleanup")

32. 
次のコードの実行結果を選んでください。

class MyClass:
def __init__(self, x, y):
self.x = x
self.y = y

def add(self):
return self.x + self.y

obj = MyClass(3, 4)
print(obj.add())

33. 
次のコードの実行結果を選んでください。

class MyClass:
def __init__(self, name):
self.name = name

obj = MyClass("Alice")
print(obj.name)

34. 
次のコードを実行したときの出力結果は何でしょうか?

def power(base, exponent=2):
return base ** exponent

print(power(3, exponent=3))

35. 
次のコードを実行した場合、出力結果として正しいものを選んでください。

def multiply(a, b=2):
return a * b

print(multiply(5, b=3))

36. 
次のコードの実行結果を選んでください。

try:
print("Start")
finally:
print("Cleanup complete")

37. 
次のコードを実行したときの出力結果は何でしょうか?

def add_numbers(a, b, show_sum=False):
result = a + b
if show_sum:
return f"The sum is {result}."
return result

print(add_numbers(7, 3, show_sum=True))

38. 
次のコードについて、クラス変数へのアクセス方法として正しいものを選んでください。

class MyClass:
class_variable = "shared"

39. 
次のコードを実行した場合の出力は何ですか?

s = "abcdefghij"
print(s[3:-3])

40. 
5 ** 3 % 4 + 6 // 3の計算結果は次のどれですか?

コメントを残すにはログインしてください。