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

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

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

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

class MyClass:
class_variable = 5

obj1 = MyClass()
obj1.class_variable += 1

print(MyClass.class_variable)

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

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

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

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

print(introduce("Sam", 30))

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

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

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

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

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

print(greet("Alice", "Hi"))

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

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

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

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

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

class MyClass:
pass

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

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

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

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

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

def power(base, exponent=2):
return base ** exponent

print(power(3, exponent=3))

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

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

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

14. 
divmod(10, 3)の出力結果はどれですか?

15. 
キーワード引数に関する正しい説明はどれですか?

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

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

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

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

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

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

print(MyClass.class_variable)

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

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

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

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

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

print(calculate_total(100, discount=10))

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

def calculate_area(length=5, width=3):
return length * width

print(calculate_area(width=4))

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

class MyClass:
class_variable = "shared"

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

s = "1234567890"
print(s[-4:])

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

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

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

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

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

print(customize_greeting(greeting="Hello"))

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

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

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

print(MyClass.class_variable)

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

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

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

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

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

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

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

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

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

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

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

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

35. 
次のコードにおいて、クラス変数として正しい記述を選んでください。

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

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

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

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

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

s = "123456789"
print(s[::3])

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

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

print(test_function())

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

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

class MyClass:
class_variable = "shared"

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

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