Python 3エンジニア認定基礎試験~模擬試験①~ 2024年12月10日2024年12月10日 ailearn 1. 次のコードの実行結果を選んでください。 import jsondata = {"name": "David", "age": 40, "city": "Hiroshima"}json_string = json.dumps(data, indent=4)print(json_string) JSON形式のデータが1行で表示される Python辞書がそのまま表示される エラーが発生する JSON形式のデータが整形された状態で表示される None 2. Pythonでの「四則演算」の順序として最も正しいものはどれですか? 加算と減算が優先される 乗算と除算が優先される 四則演算は全て左から順に計算される 順序はランダムに決まる None 3. Pythonで「改行せずに連続した出力」を行うためにprint関数で使用する引数はどれですか? sep start newline end None 4. 対話モードの「プロンプト」として正しい記号はどれですか? $ # >>> & None 5. Pythonの「対話モード」を起動するためのコマンドはどれですか? python python -i python interactive python start None 6. 次のコードの実行結果を選んでください。 import jsondata = {"name": "Eve", "age": None, "is_active": True}json_string = json.dumps(data)print(json_string) {"name": "Eve", "age": 0, "is_active": 1} エラーが発生する {"name": "Eve", "age": null, "is_active": true} {"name": "Eve", "age": None, "is_active": True} None 7. 次のコードを実行したときの出力結果は何でしょうか? def multiply(x, y=3, z=2):return x * y * zprint(multiply(4))print(multiply(4, 5))print(multiply(4, 5, 6)) 24 12 10 24 40 120 12 15 24 24 40 48 None 8. 次のコードで、ファイルからJSONデータを読み込むために使用する正しい関数を選んでください。 import jsonwith open("data.json", "r") as f:data = ??? json.loads(f) json.dumps(f) json.load(f) json.dump(f) None 9. 次のコードを実行したときの出力結果は何でしょうか? def add_three(a, b, c=1):return a + b + cprint(add_three(2, 3))print(add_three(2, 3, 4)) 5 6 1 5 6 6 6 9 None 10. 次のコードの実行結果を選んでください。 try:d = {"key": "value"}print(d["missing_key"])except KeyError:print("Key not found") "value" "Key not found" エラーが発生する 何も出力されない None 11. 次のコードの実行結果を選んでください。 try:result = "text" + 5except TypeError as e:print("TypeError occurred:", e) "TypeError occurred" "TypeError occurred: can only concatenate str (not "int") to str" エラーが発生する 何も出力されない None 12. Pythonの「duck typing」の概念に基づく特徴として正しいものはどれですか? 変数の型を常に明示する オブジェクトの型ではなく、振る舞いに注目する メモリ管理を開発者が行う インタプリタを使わずに実行する None 13. 次のコードを実行したときの出力結果は何でしょうか? def add(a, b, *args):return a + b + sum(args)print(add(1, 2))print(add(1, 2, 3, 4)) 3 6 3 10 3 15 3 9 None 14. 次のコードの実行結果を選んでください。 try:x = 10 / 2except ZeroDivisionError:print("Cannot divide by zero")else:print("Result:", x)finally:print("Execution complete") Result: 5.0 Execution complete Cannot divide by zero Execution complete エラーが発生する 何も出力されない None 15. 次のコードの実行結果を選んでください。 import jsondata = {"name": "Frank", "hobbies": ["reading", "traveling", "coding"]}with open("hobbies.json", "w") as f:json.dump(data, f, indent=2) ファイルhobbies.jsonが作成され、整形されたJSONデータが保存される ファイルhobbies.jsonが作成され、1行でJSONデータが保存される エラーが発生する ファイルが作成されない None 16. 次のコードの実行結果を選んでください。 try:x = int("10.5")except ValueError:print("Conversion failed") "Conversion failed" 10 10.5 エラーが発生する None 17. 次のコードの出力は何ですか? a = [1, 2, 3]b = ab.append(4)print(a) [1, 2, 3] [1, 2, 4] エラーが発生する [1, 2, 3, 4] None 18. 次のコードの実行結果を選んでください。 try:result = "5" + 5except TypeError as e:print("Error type:", type(e)) "Error type: TypeError" "Error type: " "Error type: int" エラーが発生する None 19. 次のコードの実行結果を選んでください。 import jsonjson_data = '{"name": "Alice", "skills": ["Python", "Machine Learning"]}'data = json.loads(json_data)print(data["skills"][0]) "Python" "Machine Learning" null エラーが発生する None 20. Pythonの対話モードで「10を3で割った余り」を表示したいとき、どのようなコードを入力すべきですか? 10 % 3 10 // 3 10 / 3 10 mod 3 None 21. 次のコードを実行したときの出力結果は何でしょうか? def factorial(n):if n == 0:return 1return n * factorial(n - 1)print(factorial(3))print(factorial(5)) 5 15 3 5 6 120 15 120 None 22. 対話モードで次の行を続けて書く必要があるときに使用する記号はどれですか? : ; ¥ / None 23. 次のコードの実行結果を選んでください。 try:x = int("42")y = int("hello")print(x + y)except ValueError as e:print("Error:", e) "Error: invalid literal for int() with base 10: '42'" 42 "Error: invalid literal for int() with base 10: 'hello'" エラーが発生する None 24. 次のコードの実行結果を選んでください。 import jsondata = {"numbers": [10, 20, 30, 40]}json_data = json.dumps(data, separators=(",", ":"))print(json_data) {"numbers": [10, 20, 30, 40]} {"numbers":[10,20,30,40]} {"numbers" : [10, 20, 30, 40]} エラーが発生する None 25. Pythonが「クロスプラットフォーム」に対応している意味として、正しいものはどれですか? Pythonは全てのOSで動作しない PythonはWindowsでのみ動作する PythonのコードはOSごとに書き換える必要がある Pythonのコードは異なるOSでも同様に動作する None 26. 次のコードの実行結果を選んでください。 try:raise AssertionError("Assertion failed")except AssertionError as e:print("Caught assertion error:", e) "Caught assertion error: Assertion failed" "Assertion failed" "AssertionError" エラーが発生する None 27. 次のコードを実行したときの出力結果は何でしょうか? def greet_many(times, name="Guest"):return (f"Hello, {name}!" * times).strip()print(greet_many(2))print(greet_many(3, "Alice")) Hello, Guest! Hello, Alice!Hello, Alice!Hello, Alice! Hello, Guest!Hello, Guest! Hello, Alice! Hello, Guest! Hello, Alice! Hello, Guest!Hello, Guest! Hello, Alice!Hello, Alice!Hello, Alice! None 28. 次のコードの出力は何ですか? a = "Hello"b = a.replace("H", "J")print(a)print(b) Hello Hello Jello Hello Jello Jello Hello Jello None 29. 次のコードの出力は何ですか? x = [1, 2, 3]y = x.copy()y.append(4)print(x)print(y) [1, 2, 3] [1, 2, 3] [1, 2, 3, 4] [1, 2, 3, 4] [1, 2, 3] [1, 2, 3, 4] [1, 2, 4] [1, 2, 4] None 30. 次のコードで、関数subtractが呼び出されたときに引数が指定されなければxとyにそれぞれ0が入るように設定する方法はどれでしょうか? def subtract(x=0, y=0): return x - y def subtract(x, y=0): return x - y def subtract(x, y): x = 0 y = 0 return x - y def subtract(x=0, y): return x - y None 31. 次のコードの実行結果を選んでください。 try:lst = [1, 2, 3]print(lst[5])except IndexError:print("Index out of range") 3 エラーが発生する "Index out of range" 何も出力されない None 32. 次のコードを対話モードで実行した場合の出力は何ですか? 10 / 3 3 3.3333 3.0 4 None 33. Pythonで「変数の宣言」において、以下の記述のうち正しいものはどれですか? int x = 5 x: int = 5 x = 5 var x = 5 None 34. 次のコードを実行したときの出力結果は何でしょうか? def add(x, y):return x + ydef 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)) 15 50 15 10 5 50 10 15 None 35. Pythonにおける「リスト内包表記」として正しい説明はどれですか? リストを単純に宣言する方法 文字列をリストに変換する方法 繰り返し処理を利用してリストを効率的に生成する方法 リストをソートする方法 None 36. 次のコードの実行結果を選んでください。 import jsondata = {"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"]) 85 90 {"Math": 90, "Science": 85} エラーが発生する None 37. Pythonが「オープンソース」であることの利点として、最も正しい説明はどれですか? 商用利用が完全に禁止されている 利用に有料のライセンスが必要である すべてのPythonコードは公開しなければならない Pythonのコードを誰でも閲覧、修正、配布できる None 38. 次のコードで、2つの数値を引数に取り、その合計を返す関数addを正しく定義する方法はどれでしょうか? def add(x y): return x + y def add(x, y): print(x + y) add(x, y): return x + y def add(x, y): return x + y None 39. Pythonにおける「識別子のルール」として誤っているものはどれですか? 数字で始めることができる アンダースコアで始めることができる 英字で始めることができる 大文字と小文字が区別される None 40. 例外処理におけるtryブロックの役割として正しいものを選んでください。 発生する可能性がある例外を監視する 発生した例外を処理する 発生した例外を再スローする 例外が発生しないことを保証する None Time's up