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

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

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

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

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

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

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

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

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

obj = MyClass(10)
obj.value = 20
print(obj.value)

5. 
文字列s = "abcdefghij"の最初の5文字を逆順で取得するスライスはどれですか?

6. 
int("20")の結果は次のどれですか?

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

def greet(name="John", age):
return f"{name} is {age} years old."

8. 
引数にデフォルト値を設定する場合の正しい記述はどれですか?

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

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

def generate_report(title="Monthly Report", include_summary=True):
summary_text = "Summary included." if include_summary else "No summary."
return f"Title: {title}, {summary_text}"

print(generate_report(include_summary=False))

11. 
次のコードで、finallyブロックが実行される理由として正しいものを選んでください。

try:
print("Try block")
except:
print("Except block")
finally:
print("Finally block")

12. 
次のうち、Pythonで乱数を生成するのに使用されるライブラリはどれですか?

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

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

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

class MyClass:
class_attribute = "shared"

obj1 = MyClass()
obj2 = MyClass()

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

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

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

obj1 = MyClass()
obj1.class_variable.append(4)

print(MyClass.class_variable)

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

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))

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

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

class MyClass:
class_variable = 100

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

19. 
次のコードにおいて、finallyブロックの役割として正しいものを選んでください。

try:
x = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero")
finally:
print("Execution complete")

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

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)

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

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

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

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

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

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

23. 
bin(10)の出力はどれですか?

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

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

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

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

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))

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

class MyClass:
class_variable = 10

obj1 = MyClass()
obj2 = MyClass()
obj1.class_variable = 20

print(MyClass.class_variable)

27. 
Pythonで文字列s = "Python Programming"の最後の3文字を取得するスライスはどれですか?

28. 
complex(3, -4).imagの結果はどれですか?

29. 
次のコードを実行した場合、結果は何になりますか?

def add_numbers(a, b=10, c=5):
return a + b + c

print(add_numbers(1, c=2))

30. 
Pythonで文字列s = "Hello World"の最初の5文字を取得するスライスはどれですか?

31. 
round(2.6)の結果はどれですか?

32. 
キーワード引数を使用する際のルールとして正しいものはどれですか?

33. 
デフォルト値として設定できるオブジェクトに該当しないものはどれですか?

34. 
次のコードで、finallyブロックが実行されるかどうかを選んでください。

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

35. 
文字列s = "abcdefghij"のインデックス1から8までを逆順で取得するために使用するスライスはどれですか?

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

def repeat_string(s, times=3, separator=" "):
return separator.join([s] * times)

print(repeat_string("Hi", times=2))

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

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

print(calculate_total(100, discount=10))

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

39. 
文字列s = "abcdefghij"の最初から最後までの文字を取得するスライスはどれですか?

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

def divide(a, b=1):
return a / b

print(divide(10, b=2))

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