Python 3エンジニア認定基礎試験~模擬試験②~ 2024年12月10日2024年12月10日 ailearn 1. 次のコードを実行したときの出力結果は何でしょうか? def test_function():y = 5y += 1return yprint(test_function())print(y) 5 5 6 5 エラー エラー 6 エラー None 2. 文字列text = "This is a test"から改行を挿入して「This is\na test」にするために必要なコードはどれですか? text.replace(" ", "\n", 1) text.replace(" ", "\n") text.insert(" ", "\n") text.split(" ", 1) None 3. 次のコードの実行結果を選んでください。 x = [1, 2, 3 SyntaxError: invalid syntax エラーは発生しない SyntaxError: unexpected EOF while parsing 空のリストが出力される None 4. 次のコードを実行したときの出力結果は何でしょうか? x = 3def modify_variable():global xx = x * 2return xprint(modify_variable())print(x) 6 6 3 6 エラー 6 3 None 5. リスト["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 6. Pythonの文字列で改行を表すエスケープシーケンスはどれですか? \r \n \t \\ None 7. 次のコードを実行したときの出力結果は何でしょうか? x = 5def outer_function():x = 10def inner_function():return xreturn inner_function()print(outer_function()) 10 5 15 エラー None 8. 次のコードの実行結果を選んでください。 if True print("This will not work") This will not work IndentationError: expected an indented block エラーは発生しない SyntaxError: invalid syntax None 9. 文字列"Hello World"のスペースを削除して"HelloWorld"とするために使用するメソッドはどれですか? remove(" ") replace(" ", "") delete(" ") join(" ") None 10. 次のコードの出力結果を求めてください。 text = "Line1\nLine2\nLine3"print(text.replace("\n", " | ")) Line1 Line2 Line3 Line1\nLine2\nLine3 Line1 | Line2 | Line3 Line1 - Line2 - Line3 None 11. ローカル変数に関する正しい説明はどれですか? ローカル変数は、関数内でのみアクセスできます。 ローカル変数は、すべての関数で共有されます。 ローカル変数は、グローバル変数と同じスコープを持ちます。 ローカル変数は、自動的にグローバル変数に変換されます。 None 12. 次のコードの実行結果を選んでください。 try:raise ValueError("Invalid data provided")except KeyError as e:print("Caught KeyError:", e)except ValueError as e:print("Caught ValueError:", e) "Caught ValueError: Invalid data provided" "Caught KeyError: Invalid data provided" エラーが発生してプログラムが停止する 何も出力されない None 13. 次のコードを実行したときの出力結果は何でしょうか? def outer_function():x = 10def inner_function():nonlocal xx = x + 5return xreturn inner_function()print(outer_function()) 10 5 エラー 15 None 14. 次のコードを実行したときの出力結果は何でしょうか? def outer_function():x = 5def inner_function():nonlocal xx += 10return xreturn inner_function()print(outer_function()) 5 10 15 エラー None 15. 次のコードの出力結果は何でしょうか? x = 5def modify_variable():global xx += 10print(x)modify_variable()print(x) 15 15 15 5 エラー 10 15 None 16. 次のコードを実行したときの出力結果は何でしょうか? def reset_counter():count = 0def increment():nonlocal countcount += 1return countdef reset():nonlocal countcount = 0return increment, resetincrement, reset = reset_counter()print(increment())print(increment())reset()print(increment()) 1 2 1 1 2 2 1 2 0 エラー None 17. 次のコードを実行したときの出力結果は何でしょうか? x = 10def shadow_variable():x = 5def inner():return xreturn inner()print(shadow_variable())print(x) 5 10 10 5 エラー 5 5 None 18. Pythonで文字列を結合するために使用される演算子はどれですか? + * % / None 19. 次のコードのnonlocalキーワードの用途として正しいものはどれですか? def outer_function():x = 10def inner_function():nonlocal xx += 5 グローバル変数を変更する。 内部関数内で新しい変数を作成する。 外側スコープの変数を参照して変更する。 エラーを防ぐために使用される。 None 20. 次のコードの実行結果を選んでください。 for i in range(3):print(i) 0 1 2 エラーは発生しない SyntaxError: invalid syntax IndentationError: expected an indented block None 21. 次のコードの出力結果を求めてください。 text = "Hello"result = text + " " * 3 + "World"print(result) Hello World HelloWorld Hello Hello World None 22. 次のコードの実行結果を選んでください。 try:raise IndexError("Index out of range")except KeyError as e:print("Caught KeyError:", e)except Exception as e:print("Caught Exception:", e) "Caught KeyError: Index out of range" エラーが発生してプログラムが停止する "Caught Exception: Index out of range" 何も出力されない None 23. 次のコードを実行したときの出力結果は何でしょうか? def modify_list(values):values.append(4)numbers = [1, 2, 3]modify_list(numbers)print(numbers) [4] [1, 2, 3] [1, 2, 3, 4] エラー None 24. 次のコードを実行したときの出力結果は何でしょうか? x = 10def modify_variable():x = 20return xprint(modify_variable())print(x) 20 20 20 10 10 20 エラー None 25. 次のうち、文字列の結合に使われないメソッドはどれですか? join() format() concat() f-string None 26. 次のコードの問題点として正しいものを選んでください。 if x = 10:print("x is 10") コロン(:)が間違っている 代入文(=)ではなく比較演算子(==)を使用する必要がある xが未定義 構文に問題はない None 27. "Python3"という文字列から「3」を削除して"Python"にするコードはどれですか? remove("3") "Python3".replace("3", "") delete("3") strip("3") None 28. 次のコードを実行したときの出力結果は何でしょうか? def modify_string(s):s += " World"return smy_string = "Hello"result = modify_string(my_string)print(result)print(my_string) Hello World Hello World Hello World Hello Hello Hello World Hello None 29. 次のコードの実行結果を選んでください。 try:raise ValueError("Test error")except Exception as e:print("Caught an exception")raise e "Caught an exception"が出力され、プログラムが正常終了する エラーが発生してプログラムが停止する "Caught an exception"が出力され、例外が再発生する 何も出力されない None 30. 次のコードを実行したときの出力結果は何でしょうか? x = 5def multiply_by_two(x):x = x * 2return xprint(multiply_by_two(x))print(x) 10 10 10 5 5 5 エラー None 31. 次のコードでraise文の動作として正しい説明を選んでください。 if not isinstance(x, int):raise TypeError("x must be an integer") xが整数の場合にTypeErrorを発生させる xが整数でない場合にTypeErrorを発生させる 例外を無視する プログラムが正常に終了する None 32. 次のうち、Pythonの文字列で改行を保持したまま文字列を定義する方法として正しいものはどれですか? シングルクォートで囲む ダブルクォートで囲む 三重クォート(""")で囲む バックスラッシュで囲む None 33. 次のコードの実行結果を選んでください。 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 34. 次のコードの実行結果を選んでください。 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 35. 次のコードの問題点として正しいものを選んでください。 while Trueprint("Looping") print文が正しくない 無限ループが発生する コロン(:)が欠けている 構文に問題はない None 36. 次のコードで出力を1行にまとめるために使用する引数はどれですか? print("Hello")print("World") end sep start stop None 37. ローカル変数に関する正しい説明はどれですか? ローカル変数は関数の外で使用できます。 ローカル変数は関数の中でのみ有効です。 ローカル変数は他の関数で自動的に共有されます。 ローカル変数はグローバル変数として自動的に変換されます。 None 38. 次のコードを実行したときの出力結果は何でしょうか? z = [10, 20, 30]def modify_list():global zz[1] = 99modify_list()print(z) [10, 99, 30] [10, 20, 30] エラー [99, 99, 99] None 39. 文字列の結合にjoin()メソッドを使用する場合、リスト["apple", "banana", "cherry"]をカンマ区切りの文字列にする正しいコードはどれですか? ", ".join("apple", "banana", "cherry") ", ".join(["apple", "banana", "cherry"]) " ".join(["apple", "banana", "cherry"]) join(",", ["apple", "banana", "cherry"]) None 40. 次のコードの問題点として正しいものを選んでください。 print "Hello, World!" Python 3ではprintは関数として使用されるため、括弧が必要 Hello, World!が正しくない 変数printが未定義 構文に問題はない None Time's up