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

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

import json
data = {"name": "David", "age": 40, "city": "Hiroshima"}
json_string = json.dumps(data, indent=4)
print(json_string)

2. 
Pythonでの「四則演算」の順序として最も正しいものはどれですか?

3. 
Pythonで「改行せずに連続した出力」を行うためにprint関数で使用する引数はどれですか?

4. 
対話モードの「プロンプト」として正しい記号はどれですか?

5. 
Pythonの「対話モード」を起動するためのコマンドはどれですか?

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

import json
data = {"name": "Eve", "age": None, "is_active": True}
json_string = json.dumps(data)
print(json_string)

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

def multiply(x, y=3, z=2):
return x * y * z

print(multiply(4))
print(multiply(4, 5))
print(multiply(4, 5, 6))

8. 
次のコードで、ファイルからJSONデータを読み込むために使用する正しい関数を選んでください。

import json
with open("data.json", "r") as f:
data = ???

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

def add_three(a, b, c=1):
return a + b + c

print(add_three(2, 3))
print(add_three(2, 3, 4))

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

try:
d = {"key": "value"}
print(d["missing_key"])
except KeyError:
print("Key not found")

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

try:
result = "text" + 5
except TypeError as e:
print("TypeError occurred:", e)

12. 
Pythonの「duck typing」の概念に基づく特徴として正しいものはどれですか?

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

def add(a, b, *args):
return a + b + sum(args)

print(add(1, 2))
print(add(1, 2, 3, 4))

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

try:
x = 10 / 2
except ZeroDivisionError:
print("Cannot divide by zero")
else:
print("Result:", x)
finally:
print("Execution complete")

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

import json
data = {"name": "Frank", "hobbies": ["reading", "traveling", "coding"]}
with open("hobbies.json", "w") as f:
json.dump(data, f, indent=2)

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

try:
x = int("10.5")
except ValueError:
print("Conversion failed")

17. 
次のコードの出力は何ですか?

a = [1, 2, 3]
b = a
b.append(4)
print(a)

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

try:
result = "5" + 5
except TypeError as e:
print("Error type:", type(e))

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

import json
json_data = '{"name": "Alice", "skills": ["Python", "Machine Learning"]}'
data = json.loads(json_data)
print(data["skills"][0])

20. 
Pythonの対話モードで「10を3で割った余り」を表示したいとき、どのようなコードを入力すべきですか?

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

def factorial(n):
if n == 0:
return 1
return n * factorial(n - 1)

print(factorial(3))
print(factorial(5))

22. 
対話モードで次の行を続けて書く必要があるときに使用する記号はどれですか?

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

try:
x = int("42")
y = int("hello")
print(x + y)
except ValueError as e:
print("Error:", e)

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

import json
data = {"numbers": [10, 20, 30, 40]}
json_data = json.dumps(data, separators=(",", ":"))
print(json_data)

25. 
Pythonが「クロスプラットフォーム」に対応している意味として、正しいものはどれですか?

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

try:
raise AssertionError("Assertion failed")
except AssertionError as e:
print("Caught assertion error:", e)

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

def greet_many(times, name="Guest"):
return (f"Hello, {name}!" * times).strip()

print(greet_many(2))
print(greet_many(3, "Alice"))

28. 
次のコードの出力は何ですか?

a = "Hello"
b = a.replace("H", "J")
print(a)
print(b)

29. 
次のコードの出力は何ですか?

x = [1, 2, 3]
y = x.copy()
y.append(4)
print(x)
print(y)

30. 
次のコードで、関数subtractが呼び出されたときに引数が指定されなければxとyにそれぞれ0が入るように設定する方法はどれでしょうか?

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

try:
lst = [1, 2, 3]
print(lst[5])
except IndexError:
print("Index out of range")

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

10 / 3

33. 
Pythonで「変数の宣言」において、以下の記述のうち正しいものはどれですか?

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

def add(x, y):
return x + y

def calculate_total(a, b, func=add):
return func(a, b)

print(calculate_total(5, 10))
print(calculate_total(5, 10, lambda x, y: x * y))

35. 
Pythonにおける「リスト内包表記」として正しい説明はどれですか?

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

import json
data = {"scores": {"Math": 90, "Science": 85}}
with open("scores.json", "w") as f:
json.dump(data, f, indent=4)
with open("scores.json", "r") as f:
loaded_data = json.load(f)
print(loaded_data["scores"]["Math"])

37. 
Pythonが「オープンソース」であることの利点として、最も正しい説明はどれですか?

38. 
次のコードで、2つの数値を引数に取り、その合計を返す関数addを正しく定義する方法はどれでしょうか?

39. 
Pythonにおける「識別子のルール」として誤っているものはどれですか?

40. 
例外処理におけるtryブロックの役割として正しいものを選んでください。

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