Python 3エンジニア認定基礎試験~模擬試験②~ 2024年12月10日2024年12月10日 ailearn 1. 次のコードの実行結果を選んでください。 if 5 > 3print("5 is greater than 3") SyntaxError: expected ':' 5 is greater than 3 IndentationError: expected an indented block エラーは発生しない None 2. 次のコードを実行したときの出力結果は何でしょうか? def outer_function():x = 10def inner_function():x = x + 5return xreturn inner_function()print(outer_function()) 15 エラー 10 5 None 3. 次のコードを実行したときの出力結果は何でしょうか? x = 10def shadow_variable():x = 5def inner():return xreturn inner()print(shadow_variable())print(x) 5 10 10 5 エラー 5 5 None 4. 次のコードの問題点として正しいものを選んでください。 print "Hello, World!" Python 3ではprintは関数として使用されるため、括弧が必要 Hello, World!が正しくない 変数printが未定義 構文に問題はない None 5. 次のコードの実行結果を選んでください。 if True print("This will not work") This will not work IndentationError: expected an indented block エラーは発生しない SyntaxError: invalid syntax None 6. 次のコードの実行結果を選んでください。 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 7. 次のコードを実行したときの出力結果として正しいものを選んでください。 x = 5def outer_function():x = 10def inner_function():global xx += 5inner_function()print(x)outer_function()print(x) 10 15 15 15 10 10 エラー None 8. raise文の役割として正しいものを選んでください。 明示的に例外を発生させる 例外をキャッチする 例外を無視する 例外を記録する None 9. 次のコードを実行した場合の出力は何ですか? a = "Python"b = "Programming"result = a + " " + bprint(result) PythonProgramming Python Programming Python-Programming Python + Programming None 10. 次のコードを実行したときの出力結果は何でしょうか? x = 10def modify_variable():x = 20return xprint(modify_variable())print(x) 20 20 20 10 10 20 エラー None 11. 次のうち、Pythonの文字列において改行を削除する方法として正しいものはどれですか? replace("\n", "") strip() split() join() None 12. Pythonの文字列で改行を表すエスケープシーケンスはどれですか? \r \n \t \\ None 13. 次のコードの実行結果として正しいものを選んでください。 print("Hello "Hello" SyntaxError: EOL while scanning string literal SyntaxError: invalid syntax 空文字列が出力される None 14. 次のコードを実行したときの出力結果は何でしょうか? x = 10def modify_variable():global xx = x + 5return xprint(modify_variable())print(x) 15 15 エラー 10 15 15 10 None 15. 次のコードでraise文の動作として正しい説明を選んでください。 if not isinstance(x, int):raise TypeError("x must be an integer") xが整数の場合にTypeErrorを発生させる xが整数でない場合にTypeErrorを発生させる 例外を無視する プログラムが正常に終了する None 16. 次のコードを実行したときにエラーが発生する理由として正しいものを選んでください。 def example_function():x += 1print(x)example_function() xがローカル変数として定義されていないから。 globalキーワードを使用していないから。 エラーは発生しません。 グローバル変数xが未定義だから。 None 17. ローカル変数が削除されるタイミングとして正しいものはどれですか? プログラムが終了したとき。 ローカル変数がグローバル変数に変換されたとき。 ローカル変数は明示的に削除しなければならない。 関数の実行が完了したとき。 None 18. 文字列の結合にjoin()メソッドを使用する場合、リスト["apple", "banana", "cherry"]をカンマ区切りの文字列にする正しいコードはどれですか? ", ".join("apple", "banana", "cherry") ", ".join(["apple", "banana", "cherry"]) " ".join(["apple", "banana", "cherry"]) join(",", ["apple", "banana", "cherry"]) None 19. 次のリスト["one", "two", "three"]をスペース区切りで結合し、"one two three"という文字列を生成するコードはどれですか? ",".join(["one", "two", "three"]) "".join(["one", "two", "three"]) " ".join("one", "two", "three") " ".join(["one", "two", "three"]) None 20. 次のコードで発生する例外の種類として正しいものを選んでください。 raise KeyError("Key not found") ValueError TypeError IndexError KeyError None 21. 次のコードの問題点として正しいものを選んでください。 while Trueprint("Looping") print文が正しくない 無限ループが発生する コロン(:)が欠けている 構文に問題はない None 22. 次のコードを実行したときの出力結果は何でしょうか? x = 5def multiply_by_two(x):x = x * 2return xprint(multiply_by_two(x))print(x) 10 10 10 5 5 5 エラー None 23. 次のコードの出力結果を求めてください。 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 24. 次のコードの実行結果を選んでください。 if True:print("This is true") "This is true" エラーは発生しない SyntaxError: invalid syntax IndentationError: expected an indented block None 25. 次のコードで出力を1行にまとめるために使用する引数はどれですか? print("Hello")print("World") end sep start stop None 26. 次のコードの出力として正しいものを選んでください。 try:raise Exception("Generic error")except Exception as e:print(type(e).__name__, ":", e) "Generic error" "TypeError : Generic error" Exceptionが発生してプログラムが停止する "Exception : Generic error" None 27. 次のコードを実行した場合の結果として正しいものを選んでください。 try:raise ValueError("Invalid value")except ValueError as e:print("Caught exception:", e) ValueError: Invalid value "Caught exception: Invalid value" エラーが発生する 何も出力されない None 28. 次のコードを実行したときの出力結果は何でしょうか? x = 10def outer_function():x = 20def inner_function():return x + 5return inner_function()print(outer_function())print(x) 25 10 30 20 エラー 25 25 None 29. 次のコードを実行した場合の出力は何ですか? text = "Line1\nLine2\nLine3"result = text.split("\n")print(result[1]) Line1 Line2 Line3 ['Line1', 'Line2', 'Line3'] None 30. ローカル変数に関する正しい説明はどれですか? ローカル変数は、関数内でのみアクセスできます。 ローカル変数は、すべての関数で共有されます。 ローカル変数は、グローバル変数と同じスコープを持ちます。 ローカル変数は、自動的にグローバル変数に変換されます。 None 31. リスト["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 32. 次のコードのnonlocalキーワードの用途として正しいものはどれですか? def outer_function():x = 10def inner_function():nonlocal xx += 5 グローバル変数を変更する。 内部関数内で新しい変数を作成する。 外側スコープの変数を参照して変更する。 エラーを防ぐために使用される。 None 33. 文字列text = "This is a test"から改行を挿入して「This is\na test」にするために必要なコードはどれですか? text.replace(" ", "\n", 1) text.replace(" ", "\n") text.insert(" ", "\n") text.split(" ", 1) None 34. 次のコードの出力結果は何でしょうか? x = 0def example_function():global xx = x + 2return xprint(example_function())print(x) 2 0 2 2 0 2 エラー None 35. 次のコードを実行したときの出力結果は何でしょうか? def my_function():x = 5x += 10return xprint(my_function()) 5 10 15 エラー None 36. 次のコードを実行したときの出力結果は何でしょうか? x = 3def modify_variable():global xx = x * 2return xprint(modify_variable())print(x) 6 6 3 6 エラー 6 3 None 37. Pythonで「Hello」と「World」を改行付きで結合し、「Hello\nWorld」と表示させるコードはどれですか? "Hello" + "World" "Hello\nWorld" "Hello" + "\n" + "World" "Hello\nWorld" と "Hello" + "\n" + "World" None 38. 次のコードの実行結果を選んでください。 try:raise TypeError("Invalid type")except Exception as e:print("Caught Exception:", e) 何も出力されない エラーが発生してプログラムが停止する "Caught TypeError: Invalid type" "Caught Exception: Invalid type" None 39. 次のコードの問題点として正しいものを選んでください。 if 5 > 3:print("5 is greater than 3") コロン(:)が欠けている print文が間違っている 構文に問題はない インデントが正しくない None 40. 次のコードの実行結果を選んでください。 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 Time's up