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

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

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

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

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

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

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

def format_message(message, prefix="Info: ", suffix="."):
return f"{prefix}{message}{suffix}"

print(format_message("System update completed", suffix="!"))

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

def build_profile(first_name, last_name, age=25, country="Japan"):
return f"{first_name} {last_name}, {age} years old, from {country}"

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

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

class MyClass:
class_variable = "shared"

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

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

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

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

print(MyClass.class_variable)

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

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

class MyClass:
class_attribute = "shared value"

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

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

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

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

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

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

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

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

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

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

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

print(test_function())

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

17. 
次のコードのクラス定義として正しいものを選んでください。

class MyClass:
pass

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

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

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

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

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

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

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

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

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

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

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

obj = MyClass(42)
print(obj)

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

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

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

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

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

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

def customize_greeting(name="Guest", greeting="Welcome"):
return f"{greeting}, {name}!"

print(customize_greeting(greeting="Hello"))

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

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

class MyClass:
class_variable = "shared"

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

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

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

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

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

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

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

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

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

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

print(MyClass.class_variable)

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

class MyClass:
class_variable = 0

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

obj1 = MyClass()
obj2 = MyClass()

obj1.increment()
print(obj2.class_variable)

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

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

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

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

class MyClass:
class_variable = 5

obj1 = MyClass()
obj1.class_variable += 1

print(MyClass.class_variable)

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

class MyClass:
class_variable = 10

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

print(MyClass.class_variable)

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

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

print(introduce("Sam", 30))

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

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

print(add_numbers(1, c=2))

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

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

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

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