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

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

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

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

def add_items(item, items=None):
if items is None:
items = []
items.append(item)
return items

print(add_items("apple"))
print(add_items("banana"))

4. 
Pythonの対話モードで「1から10までの整数を降順で表示する」には、どのようなコードを入力すべきですか?

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

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

print(power(3))
print(power(2, 3))

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

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

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

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

8. 
Pythonの「ジェネレータ」として正しい説明はどれですか?

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

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

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

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

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

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

for i in range(1, 5):
if i == 3:
continue
print(i)

12. 
デフォルト引数y=2を持つ関数multiplyを定義し、引数xをy倍にして返す方法はどれでしょうか?

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

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

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

import json
data = {"status": None, "valid": True}
json_string = json.dumps(data)
print(json_string)

15. 
Pythonの「動的型付け」はどのような特徴を持っていますか?

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

def add(a, b=2):
return a + b

result1 = add(5)
result2 = add(5, 3)

print(result1, result2)

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

def add_numbers(x, y=10, z=5):
return x + y + z

print(add_numbers(3))
print(add_numbers(3, 2))
print(add_numbers(3, 2, 1))

18. 
Pythonの標準ライブラリを使用して、JSONデータを読み書きするためにインポートする必要があるモジュールを選んでください。

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

x = 2
y = x ** 3
y

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

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

try:
x = int("123")
except ValueError:
print("Invalid value")
else:
print("Conversion successful:", x)
finally:
print("End of program")

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

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

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

25. 
Pythonの対話モードにおいて、_(アンダースコア)に格納される値は何ですか?

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

def func(x):
x += 5
return x

a = 10
func(a)
print(a)

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

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

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

28. 
次のコードの中で、「Hello, World!」と出力する関数greetを正しく定義する方法はどれでしょうか?

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

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

30. 
2つの文字列を結合して返す関数concatを正しく定義するコードはどれでしょうか?

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

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

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

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

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

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

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

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

try:
result = 10 / 2
except ZeroDivisionError:
print("Cannot divide by zero")
else:
print("Result:", result)

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

import json
data = {"name": "Bob", "age": 30, "city": "Tokyo"}
json_data = json.dumps(data)
print(type(json_data))

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

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

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

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

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

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

def keyword_only_arg(*, x):
return x * 2

print(keyword_only_arg(x=5))

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

try:
raise RuntimeError("Unexpected error")
except RuntimeError as e:
print("Caught runtime error:", e)

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