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

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

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

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

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

3. 
abs(-3.7)の結果はどれですか?

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

class MyClass:
class_variable = {"key": "value"}

obj = MyClass()
obj.class_variable["new_key"] = "new_value"

print(MyClass.class_variable)

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

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

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

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

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

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

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

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

11. 
次のコードにおいて、finallyブロック内でreturn文を記述した場合の動作として正しいものを選んでください。

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

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

class MyClass:
class_variable = "shared"

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

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

class MyClass:
class_attribute = "shared value"

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

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

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

15. 
3.14159を小数点以下2桁まで表示するにはどれを使いますか?

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

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

class MyClass:
class_variable = "shared"

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

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

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

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

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

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

print(sum_numbers(c=6))

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

class MyClass:
class_variable = "shared"

obj = MyClass()
print(obj.class_variable)

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

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

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

def greet(name, message="Hello"):
print(f"{message}, {name}!")

greet(name="Alice", message="Hi")

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

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

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

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

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

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

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

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

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

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

print(divide(10, b=2))

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

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

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

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

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

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

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

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

s = "Python"
print(s[:3] + s[-3:])

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

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

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

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

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

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

class MyClass:
class_variable = "shared"

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

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

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

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

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

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

print(MyClass.class_variable)

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

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