Python 3エンジニア認定基礎試験~模擬試験②~ 2024年12月10日2024年12月10日 ailearn 1. 次のコードを実行したときの出力結果は何でしょうか? def modify_string(text):text += " World"return textoriginal_text = "Hello"result = modify_string(original_text)print(result)print(original_text) Hello Hello Hello Hello Hello Hello World Hello World Hello World Hello None 2. 次のコードを実行したときの出力結果は何でしょうか? x = 5def modify_variable():global xx = x * 2return xprint(modify_variable())print(x) 10 10 5 10 10 5 エラー None 3. 次のコードを実行したときの出力結果は何でしょうか? x = 5def outer_function():global xx = x * 2outer_function()print(x) 5 10 エラー None None 4. 次のコードを実行したときの出力結果は何でしょうか? def my_function():x = 5x += 10return xprint(my_function()) 5 10 15 エラー None 5. 次のコードの出力結果は何ですか? x = 10def my_function():x = 5print(x)my_function()print(x) 10 5 5 5 5 10 エラー None 6. 次のコードを実行したときの出力結果は何でしょうか? x = 3def modify_variable():global xx = x * 2return xprint(modify_variable())print(x) 6 6 3 6 エラー 6 3 None 7. ローカル変数に関する正しい説明はどれですか? ローカル変数は関数の外で使用できます。 ローカル変数は関数の中でのみ有効です。 ローカル変数は他の関数で自動的に共有されます。 ローカル変数はグローバル変数として自動的に変換されます。 None 8. 次のコードの出力結果を求めてください。 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 9. 文字列の中に改行を含めるにはどの方法を使うべきですか? スペースを追加する バックスラッシュと「n」を使用する ドット(.)を追加する バックスラッシュと「t」を使用する None 10. 次のコードに関する正しい説明はどれですか? x = 10def example_function():print(x)example_function() エラーが発生します。 example_function内のローカル変数xを出力します。 グローバル変数xを出力します。 グローバル変数xはローカル変数として扱われます。 None 11. 次のコードの実行結果を選んでください。 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 12. 次のコードを実行したときの出力結果は何でしょうか? 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 13. 次のコードの実行結果を選んでください。 try:raise ValueError("Test error")except Exception as e:print("Caught an exception")raise e "Caught an exception"が出力され、プログラムが正常終了する エラーが発生してプログラムが停止する "Caught an exception"が出力され、例外が再発生する 何も出力されない None 14. raise文の役割として正しいものを選んでください。 明示的に例外を発生させる 例外をキャッチする 例外を無視する 例外を記録する None 15. ローカル変数に関する正しい説明はどれですか? ローカル変数は、関数内でのみアクセスできます。 ローカル変数は、すべての関数で共有されます。 ローカル変数は、グローバル変数と同じスコープを持ちます。 ローカル変数は、自動的にグローバル変数に変換されます。 None 16. 次のコードを実行したときの出力結果は何でしょうか? y = 100def modify_variable():global yy = "Changed"modify_variable()print(y) "Changed" 100 エラー None None 17. 文字列の結合にjoin()メソッドを使用する場合、リスト["apple", "banana", "cherry"]をカンマ区切りの文字列にする正しいコードはどれですか? ", ".join("apple", "banana", "cherry") ", ".join(["apple", "banana", "cherry"]) " ".join(["apple", "banana", "cherry"]) join(",", ["apple", "banana", "cherry"]) None 18. 次のコードの実行結果を選んでください。 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 19. Pythonの文字列で改行を表すエスケープシーケンスはどれですか? \r \n \t \\ None 20. 次のコードの実行結果を選んでください。 print("Missing parenthesis in print statement" SyntaxError: invalid syntax SyntaxError: unexpected EOF while parsing エラーは発生しない "Missing parenthesis in print statement" None 21. "Python3"という文字列から「3」を削除して"Python"にするコードはどれですか? remove("3") "Python3".replace("3", "") delete("3") strip("3") None 22. ミュータブル(変更可能)なオブジェクトに該当するものはどれですか? タプル 文字列 リスト 数値 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. 次のコードを実行したときの出力結果は何でしょうか? def test_function():y = 5y += 1return yprint(test_function())print(y) 5 5 6 5 エラー エラー 6 エラー None 25. ローカル変数が削除されるタイミングとして正しいものはどれですか? プログラムが終了したとき。 ローカル変数がグローバル変数に変換されたとき。 ローカル変数は明示的に削除しなければならない。 関数の実行が完了したとき。 None 26. 次のコードを実行したときの出力結果は何でしょうか? x = 10def test_function():x = 20def modify_variable():global xx += 10modify_variable()print(x)test_function()print(x) 20 10 30 30 20 30 20 20 None 27. 次のコードで出力を1行にまとめるために使用する引数はどれですか? print("Hello")print("World") end sep start stop None 28. 次のコードを実行したときの出力結果は何でしょうか? x = 10def modify_variable():x = 20return xprint(modify_variable())print(x) 20 20 20 10 10 20 エラー None 29. ローカル変数とグローバル変数のスコープを区別するために使用されるキーワードはどれですか? local nonlocal static global None 30. 次のコードを実行した場合の出力は何ですか? text = """Line1Line2Line3"""print(text.splitlines()[0]) Line1 Line2 Line3 Line1 Line2 Line3 None 31. 次のコードの問題点として正しいものを選んでください。 if 5 > 3:print("5 is greater than 3") コロン(:)が欠けている print文が間違っている 構文に問題はない インデントが正しくない None 32. 次のコードの出力結果を求めてください。 text = "Line1\nLine2\nLine3"print(text.replace("\n", " | ")) Line1 Line2 Line3 Line1\nLine2\nLine3 Line1 | Line2 | Line3 Line1 - Line2 - Line3 None 33. 次のコードを実行したときの出力結果は何でしょうか? x = 0def increment():global xx += 1return xprint(increment())print(increment()) 1 1 1 2 2 2 エラー None 34. 次のコードを実行したときの出力結果は何でしょうか? 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 35. 変数greeting = "Hello"とname = "Alice"を使って、「Hello, Alice!」と出力するためのコードはどれですか? print(f"{greeting}, {name}!") print("{}! {}".format(greeting, name)) print(greeting, ",", name, "!") print(f"{greeting}, {name}!"), "Hello" + name None 36. 次のコードの実行結果を選んでください。 x = [1, 2, 3 SyntaxError: invalid syntax エラーは発生しない SyntaxError: unexpected EOF while parsing 空のリストが出力される None 37. 次のコードを実行したときの出力結果は何でしょうか? 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 38. 次のコードを実行したときの出力結果は何でしょうか? x = 10def modify_variable():global xx = x + 5return xprint(modify_variable())print(x) 15 15 エラー 10 15 15 10 None 39. 次のコードに関する正しい説明はどれですか? x = 10def example_function():x = 5print(x)example_function()print(x) グローバル変数xが変更されます。 ローカル変数xとグローバル変数xは別物として扱われます。 エラーが発生します。 example_function内のローカル変数xは、グローバル変数xと同期されます。 None 40. 次のコードの実行結果を選んでください。 try:raise RuntimeError("Runtime issue")except RuntimeError as e:print("Caught:", e)raise RuntimeError: Runtime issue プログラムが停止するが、出力はない 何も出力されない "Caught: Runtime issue"が出力され、エラーが再発生する None Time's up