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

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

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

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

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

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

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

class MyClass:
class_variable = 5

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

obj1 = MyClass()
obj2 = MyClass()

obj1.change_variable()
print(MyClass.class_variable)

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

def show_info(name="John", age=25):
print(f"{name} is {age} years old.")

show_info(age=30)

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

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

def double(self):
self.value *= 2

obj = MyClass(5)
obj.double()
print(obj.value)

7. 
3進数でint("102", 3)の結果は次のどれですか?

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

class MyClass:
class_variable = "shared"

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

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

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

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

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

def format_text(text, uppercase=False, exclamation=False):
if uppercase:
text = text.upper()
if exclamation:
text += "!"
return text

print(format_text("hello", uppercase=True, exclamation=True))

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

class MyClass:
class_variable = 100

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

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

def sum_numbers(a=1, b=2, c=3):
return a + b + c

print(sum_numbers(c=6))

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

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

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

16. 
次のコードを実行した場合、エラーが発生する理由として正しいものを選んでください。

def example_function(a, b, c):
print(a, b, c)

example_function(a=1, 2, c=3)

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

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

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

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

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

def describe_item(name, price=100, stock=10):
return f"Item: {name}, Price: {price}, Stock: {stock}"

print(describe_item("Notebook", stock=5))

20. 
文字列s = "abcdefg"の偶数番目の文字を取得するスライスはどれですか?

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

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

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

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

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

print(test_function())

24. 
次のコードについて、正しい説明を選んでください。

class MyClass:
def instance_method(self):
print("This is an instance method")

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

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

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

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)

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

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

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

class MyClass:
class_variable = 10

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

print(MyClass.class_variable)

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

class MyClass:
class_variable = 5

obj1 = MyClass()
obj1.class_variable += 1

print(MyClass.class_variable)

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

31. 
次のコードについて、クラス名として適切なものを選んでください。

class ???:
pass

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

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

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

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

def __str__(self):
return f"MyClass with value {self.value}"

obj = MyClass(42)
print(obj)

34. 
次のコードを実行したときの結果として正しいものを選んでください。

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

print(multiply(5))

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

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

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

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

print(calculate_total(100, discount=10))

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

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

print(multiply(5, b=3))

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

class MyClass:
class_variable = 0

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

obj1 = MyClass()
obj2 = MyClass()

obj1.increment()
print(obj2.class_variable)

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

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

def build_profile(name, age=18, country="Japan"):
return f"{name}, {age} years old, from {country}"

print(build_profile("Tom", country="USA"))

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