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

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

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

2. 
次のコードで発生する例外の種類として正しいものを選んでください。

x = int("hello")

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

def modify_list(lst):
lst[0] = 100

a = [1, 2, 3]
modify_list(a)
print(a)

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

a = [1, 2, 3]
print(a[-1])

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

try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero")

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

import json
json_data = '{"items": [10, 20, 30]}'
data = json.loads(json_data)
print(data["items"][1])

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

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

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

try:
x = 5 / 0
except ZeroDivisionError as e:
print("Error:", e)

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

def format_text(text, prefix="*", suffix="*"):
return prefix + text + suffix

print(format_text("Hello"))
print(format_text("Hello", prefix="~"))
print(format_text("Hello", suffix="?"))

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

11. 
Pythonの「論理演算子」として使用できないものはどれですか?

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

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

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

13. 
Pythonの「標準ライブラリ」に含まれていないものはどれですか?

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

def subtract(a, b=0):
return a - b

result1 = subtract(10)
result2 = subtract(10, 5)

print(result1, result2)

15. 
Pythonでの「複数行にまたがる文字列」を定義するために使用する方法はどれですか?

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

import json
json_data = '{"key": null}'
data = json.loads(json_data)
print(data["key"] is None)

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

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

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

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

def create_message(greeting, name="Guest", punctuation="."):
return f"{greeting}, {name}{punctuation}"

print(create_message("Hello"))
print(create_message("Hi", "Alice"))
print(create_message("Goodbye", "Bob", "!"))

20. 
次のコードで、数値を2倍にして返す関数doubleを正しく定義する方法はどれでしょうか?

21. 
Pythonにおける「変数のスコープ」として、関数内で宣言された変数が関数の外で使用できない理由はどれですか?

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

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

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

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

x = 5
y = "5"
print(x + int(y))

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

def default_and_keyword(x, y=10, *, z):
return x + y + z

print(default_and_keyword(1, z=5))

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

def greet_many(times=1):
return "Hello! " * times

print(greet_many())
print(greet_many(3))

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

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

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

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

import json
data = {"name": "Eve", "age": 27, "skills": ["coding", "testing"]}
with open("profile.json", "w") as f:
json.dump(data, f)
with open("profile.json", "r") as f:
print(f.read())

29. 
関数greetに名前を渡し、「Hello, 名前!」と出力する関数を正しく定義する方法はどれでしょうか?

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

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

print(divide(6))
print(divide(6, 2))

31. 
「Pythonのバージョン情報」を確認したいとき、対話モードで入力すべきコードはどれですか?

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

def dynamic_args(x, *args, y=10):
return x + sum(args) + y

print(dynamic_args(1, 2, 3))
print(dynamic_args(1, 2, 3, y=5))

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

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

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

import json
json_data = '{"A": 100, "B": 200, "C": 300}'
data = json.loads(json_data)
print(sum(data.values()))

35. 
Pythonが「インタプリタ型言語」として提供される利点として適切なものはどれですか?

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

a = [1, 2, 3]
print(a * 2)

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

def increment(number, step=1):
return number + step

print(increment(5))
print(increment(5, 2))

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

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

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

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

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

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

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