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

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

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

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

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

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

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

print(introduce("Sam", 30))

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

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

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

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

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

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

7. 
次のコードについて、obj1.class_variable = "changed"の動作として正しいものを選んでください。

class MyClass:
class_variable = "shared"

obj1 = MyClass()
obj2 = MyClass()
obj1.class_variable = "changed"
print(obj2.class_variable)

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

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

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

class MyClass:
class_attribute = "shared"

obj1 = MyClass()
obj2 = MyClass()

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

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

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

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

def say_hello(name="Guest"):
return f"Hello, {name}!"

print(say_hello())
print(say_hello("Alice"))

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

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

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

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

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

print(test_function())

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

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

print(calculate_total(100, discount=10))

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

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

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

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

class MyClass:
class_variable = "shared"

20. 
round(4.56789, 3)の結果はどれですか?

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

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

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

23. 
次のコードの出力結果を求めてください。

s = "PythonProgramming"
print(s[::4])

24. 
0.2 + 0.1 == 0.3の評価結果はどれですか?

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

def describe_person(name, age=30, country="Japan"):
print(f"{name} is {age} years old and lives in {country}.")

describe_person("Bob", country="USA")

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

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

class MyClass:
class_variable = "shared"

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

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

class MyClass:
class_variable = 0

@classmethod
def increment(cls):
cls.class_variable += 1

obj1 = MyClass()
obj2 = MyClass()

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

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

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

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

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

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

print(multiply(5, b=3))

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

class MyClass:
class_variable = 5

def change_variable(self):
self.class_variable += 1

obj1 = MyClass()
obj2 = MyClass()

obj1.change_variable()
print(MyClass.class_variable)

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

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

class MyClass:
def greet(self):
return "Hello!"

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

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

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

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

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

s = "Python Programming"
print(s[7:])

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

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

39. 
Pythonで無限大を表すにはどのようにしますか?

40. 
次のコードについて、クラスメソッドを定義する方法として正しいものを選んでください。

class MyClass:
@???
def my_class_method(cls):
print("This is a class method")

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