Python 3エンジニア認定基礎試験~模擬試験②~ 2024年12月10日2024年12月10日 ailearn 1. 次のコードを実行したときの出力結果は何でしょうか? def reset_list(values):values = [0, 0, 0]numbers = [1, 2, 3]reset_list(numbers)print(numbers) [0, 0, 0] [1, 2, 3] [] エラー None 2. 次のコードの実行結果を選んでください。 def add(a, b)return a + b SyntaxError: expected ':' エラーは発生しない IndentationError: expected an indented block NameError: name 'a' is not defined None 3. 次のコードの結果を選択してください。 x = [1, 2, 3]def modify_list(lst):lst.append(4)modify_list(x)print(x) [1, 2, 3, 4] [1, 2, 3] エラー [] None 4. グローバル変数に関する正しい説明はどれですか? グローバル変数は関数内でのみ使用できます。 グローバル変数はプログラム全体でアクセス可能です。 グローバル変数はglobalキーワードを使用しないと定義できません。 グローバル変数は関数内で自動的にローカル変数に変換されます。 None 5. 次のコードの実行結果を選んでください。 try:raise ValueError("Test error")except Exception as e:print("Caught an exception")raise e "Caught an exception"が出力され、プログラムが正常終了する エラーが発生してプログラムが停止する "Caught an exception"が出力され、例外が再発生する 何も出力されない None 6. 次のコードを実行したときの出力結果は何でしょうか? y = 100def outer_function():y = 200def inner_function():global yy += 50inner_function()print(y)outer_function()print(y) 200 100 250 250 200 150 200 200 None 7. 次のコードを実行したときの出力結果は何でしょうか? x = 5def outer_function():x = 10def inner_function():return xreturn inner_function()print(outer_function()) 10 5 15 エラー None 8. 次のコードを実行したときの出力結果は何でしょうか? y = 100def modify_variable():global yy = "Changed"modify_variable()print(y) "Changed" 100 エラー None None 9. 次のコードを実行した場合の出力は何ですか? a = "Python"b = "Programming"result = a + " " + bprint(result) PythonProgramming Python Programming Python-Programming Python + Programming None 10. 次のコードの出力結果を求めてください。 words = ["data", "science", "python"]sentence = " ".join(words)print(sentence) data science python datasciencepython data-science-python ['data', 'science', 'python'] None 11. 次のコードの実行結果を選んでください。 if 5 > 3print("5 is greater than 3") SyntaxError: expected ':' 5 is greater than 3 IndentationError: expected an indented block エラーは発生しない None 12. 次のコードを実行したときの出力結果は何でしょうか? def create_multiplier(factor):def multiplier(x):return x * factorreturn multipliertimes_three = create_multiplier(3)times_five = create_multiplier(5)print(times_three(10))print(times_five(10)) 10 10 3 5 エラー 30 50 None 13. 次のコードに関する正しい説明はどれですか? x = 10def example_function():print(x)example_function() エラーが発生します。 example_function内のローカル変数xを出力します。 グローバル変数xを出力します。 グローバル変数xはローカル変数として扱われます。 None 14. 次のコードを実行した場合の出力は何ですか? text = "Line1\nLine2\nLine3"lines = text.splitlines()print(lines) Line1 Line2 Line3 ['Line1', 'Line2', 'Line3'] Line1\nLine2\nLine3 ['Line1\nLine2\nLine3'] None 15. 次のコードを実行した場合の出力は何ですか? text = """Line1Line2Line3"""print(text.splitlines()[0]) Line1 Line2 Line3 Line1 Line2 Line3 None 16. 次のコードの実行結果を選んでください。 try:raise RuntimeError("Runtime issue")except RuntimeError as e:print("Caught:", e)raise RuntimeError: Runtime issue プログラムが停止するが、出力はない 何も出力されない "Caught: Runtime issue"が出力され、エラーが再発生する None 17. 変数greeting = "Hello"とname = "Alice"を使って、「Hello, Alice!」と出力するためのコードはどれですか? print(f"{greeting}, {name}!") print("{}! {}".format(greeting, name)) print(greeting, ",", name, "!") print(f"{greeting}, {name}!"), "Hello" + name None 18. 次のコードの実行結果を選んでください。 try:raise TypeError("Custom type error")except KeyError as e:print("Caught KeyError:", e)except Exception as e:print("Caught Exception:", e) "Caught Exception: Custom type error" "Caught KeyError: Custom type error" エラーが発生してプログラムが停止する 何も出力されない None 19. 次のコードを実行したときの出力結果は何でしょうか? def outer_function():x = 10def inner_function():nonlocal xx = x + 5return xreturn inner_function()print(outer_function()) 10 5 エラー 15 None 20. 次のコードを実行したときにエラーが発生する理由として正しいものを選んでください。 def example_function():x += 1print(x)example_function() xがローカル変数として定義されていないから。 globalキーワードを使用していないから。 エラーは発生しません。 グローバル変数xが未定義だから。 None 21. 次のうち、文字列の結合に使われないメソッドはどれですか? join() format() concat() f-string None 22. 次のコードを実行したときの出力結果は何でしょうか? x = 3def modify_variable():global xx = x * 2return xprint(modify_variable())print(x) 6 6 3 6 エラー 6 3 None 23. 次のコードの実行結果を選んでください。 def check_divisible(a, b):if b == 0:raise ZeroDivisionError("Cannot divide by zero")return a / btry:print(check_divisible(10, 0))except ZeroDivisionError as e:print("Error:", e) ZeroDivisionError: Cannot divide by zero 10 エラーが発生してプログラムが停止する "Error: Cannot divide by zero" None 24. "Python3"という文字列から「3」を削除して"Python"にするコードはどれですか? remove("3") "Python3".replace("3", "") delete("3") strip("3") None 25. 次のコードの実行結果を選んでください。 def validate_input(value):if not isinstance(value, int):raise TypeError("Value must be an integer")return value * 2try:print(validate_input("text"))except TypeError as e:print("Error:", e) ValueError: Value must be an integer TypeErrorが発生してプログラムが停止する "Error: Value must be an integer" 何も出力されない None 26. 次のコードの実行結果を選んでください。 try:raise TypeError("Invalid type")except Exception as e:print("Caught Exception:", e) 何も出力されない エラーが発生してプログラムが停止する "Caught TypeError: Invalid type" "Caught Exception: Invalid type" None 27. 次のコードの出力結果は何ですか? x = 10def my_function():x = 5print(x)my_function()print(x) 10 5 5 5 5 10 エラー None 28. 次のコードを実行したときの出力結果は何でしょうか? x = 5def multiply_by_two(x):x = x * 2return xprint(multiply_by_two(x))print(x) 10 10 10 5 5 5 エラー None 29. 次のコードの実行結果を選んでください。 print("Missing parenthesis in print statement" SyntaxError: invalid syntax SyntaxError: unexpected EOF while parsing エラーは発生しない "Missing parenthesis in print statement" None 30. 次のコードの問題点として正しいものを選んでください。 for i in range(5)print(i) print文が正しくない range関数が間違っている コロン(:)が欠けている 構文に問題はない None 31. グローバル変数を関数内で変更するために使用するキーワードは何ですか? local nonlocal global static None 32. 次のコードで正しい出力を選んでください。 try:raise RuntimeError("Unexpected error occurred")except RuntimeError as e:print(e) "Unexpected error occurred" "RuntimeError: Unexpected error occurred" RuntimeErrorが発生してプログラムが停止する 何も出力されない None 33. リスト["Python", "Java", "C++"]を結合して"Python and Java and C++"とするためのコードはどれですか? " and ".join(["Python", "Java", "C++"]) " and ".join("Python", "Java", "C++") " and ".concat(["Python", "Java", "C++"]) "Python", "Java", "C++".join(" and ") None 34. 次のコードの出力結果を求めてください。 names = ["John", "Paul", "George", "Ringo"]result = ", ".join(names)print(result) John Paul George Ringo John, Paul, George, Ringo John-Paul-George-Ringo ['John', 'Paul', 'George', 'Ringo'] None 35. 次のコードを実行したときの出力結果は何でしょうか? def append_item(value, items=None):if items is None:items = []items.append(value)return itemsprint(append_item(1))print(append_item(2)) [1] [1, 2] [1] [2] エラー [1] [1] None 36. 次のコードを実行したときの出力結果は何でしょうか? x = 5def outer_function():global xx = x * 2outer_function()print(x) 5 10 エラー None None 37. 改行を含む文字列"Hello\nWorld"をスペースで区切って一行にまとめるためのコードはどれですか? "Hello\nWorld".split(" ") "Hello\nWorld".replace("\n", " ") "Hello\nWorld".join(" ") "Hello\nWorld".strip() None 38. 次のコードの実行結果を選んでください。 for i in range(3):print(i) 0 1 2 エラーは発生しない SyntaxError: invalid syntax IndentationError: expected an indented block None 39. 次のコードの実行結果を選んでください。 def greet(name):print("Hello,", name) SyntaxError: invalid syntax IndentationError: expected an indented block NameError: name 'name' is not defined エラーは発生しない None 40. 次のコードを実行したときの出力結果は何でしょうか? x = [1, 2, 3]def modify_list():global xx.append(4)modify_list()print(x) [4] エラー [1, 2, 3] [1, 2, 3, 4] None Time's up