Python 3エンジニア認定基礎試験~模擬試験②~ 2024年12月10日2024年12月10日 ailearn 1. Pythonのsplitlines()メソッドはどのような機能を持っていますか? 文字列を空白ごとに分割する 文字列を改行ごとに分割する 文字列の改行を削除する 文字列の末尾に改行を追加する None 2. 次のコードを実行したときの出力結果は何でしょうか? def outer_function():x = 10def inner_function():nonlocal xx = x + 5return xreturn inner_function()print(outer_function()) 10 5 エラー 15 None 3. 文字列"Hello World"のスペースを削除して"HelloWorld"とするために使用するメソッドはどれですか? remove(" ") replace(" ", "") delete(" ") join(" ") None 4. 次のコードの実行結果を選んでください。 for i in range(3):print(i) 0 1 2 エラーは発生しない IndentationError: expected an indented block SyntaxError: invalid syntax None 5. 次のコードの実行結果を選んでください。 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 6. 次のコードの実行結果を選んでください。 if True print("This will not work") This will not work IndentationError: expected an indented block エラーは発生しない SyntaxError: invalid syntax None 7. Pythonの文字列で改行を表すエスケープシーケンスはどれですか? \r \n \t \\ None 8. 次のコードの実行結果として正しいものを選んでください。 def my_function():y = 7print(y)my_function()print(y) 7 7 7 エラー エラー 7 エラー エラー None 9. 次のコードを実行した場合の結果として正しいものを選んでください。 try:raise ValueError("Invalid value")except ValueError as e:print("Caught exception:", e) ValueError: Invalid value "Caught exception: Invalid value" エラーが発生する 何も出力されない None 10. 次のコードを実行したときの出力結果は何でしょうか? x = 5def outer_function():global xx = x * 2outer_function()print(x) 5 10 エラー None None 11. 次のコードの問題点として正しいものを選んでください。 while Trueprint("Looping") print文が正しくない 無限ループが発生する コロン(:)が欠けている 構文に問題はない None 12. 改行を含む文字列"Hello\nWorld"をスペースで区切って一行にまとめるためのコードはどれですか? "Hello\nWorld".split(" ") "Hello\nWorld".replace("\n", " ") "Hello\nWorld".join(" ") "Hello\nWorld".strip() None 13. 次のコードの実行結果を選んでください。 if True:print("This is true") "This is true" エラーは発生しない SyntaxError: invalid syntax IndentationError: expected an indented block None 14. 次のコードで出力を1行にまとめるために使用する引数はどれですか? print("Hello")print("World") end sep start stop None 15. 次のコードで正しい出力を選んでください。 try:raise RuntimeError("Unexpected error occurred")except RuntimeError as e:print(e) "Unexpected error occurred" "RuntimeError: Unexpected error occurred" RuntimeErrorが発生してプログラムが停止する 何も出力されない None 16. 次のコードに関する正しい説明はどれですか? x = 10def example_function():x = 5print(x)example_function()print(x) グローバル変数xが変更されます。 ローカル変数xとグローバル変数xは別物として扱われます。 エラーが発生します。 example_function内のローカル変数xは、グローバル変数xと同期されます。 None 17. 次のコードの問題点として正しいものを選んでください。 def my_function()return "Hello" return文が間違っている 関数名が正しくない 構文に問題はない コロン(:)が欠けている None 18. Pythonで文字列を結合するために使用される演算子はどれですか? + * % / None 19. 次のコードを実行したときの出力結果は何でしょうか? def outer_function():x = 5def inner_function():nonlocal xx += 10return xreturn inner_function()print(outer_function()) 5 10 15 エラー None 20. 次のコードの実行結果を選んでください。 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 21. 次のコードの実行結果を選んでください。 if 5 > 3print("5 is greater than 3") SyntaxError: expected ':' 5 is greater than 3 IndentationError: expected an indented block エラーは発生しない None 22. 次のコードを実行したときの出力結果は何でしょうか? 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 23. 次のコードの実行結果を選んでください。 while True print("Infinite loop") SyntaxError: expected ':' "Infinite loop"が無限に出力される IndentationError: expected an indented block エラーは発生しない None 24. 次のコードの実行結果を選んでください。 def add(a, b)return a + b SyntaxError: expected ':' エラーは発生しない IndentationError: expected an indented block NameError: name 'a' is not defined None 25. 文字列「Hello\nWorld」を一行にまとめて「Hello World」にするために適切な方法はどれですか? "Hello\nWorld".replace("\n", " ") "Hello\nWorld".strip() "Hello\nWorld".join(" ") "Hello\nWorld".split("\n") None 26. 次のうち、文字列"Hello"を5回繰り返した文字列を生成するためのコードはどれですか? "Hello".repeat(5) "Hello".multiply(5) "Hello" * 5 "Hello".join(5) None 27. 変数animal = "cat"とsound = "meow"を使って、「The cat says meow」という文字列を生成するために入力すべきコードはどれですか? "The {} says {}".format(animal, sound) f"The {animal} says {sound}" "The " + animal + " says " + sound すべて正しい None 28. 次のリスト["one", "two", "three"]をスペース区切りで結合し、"one two three"という文字列を生成するコードはどれですか? ",".join(["one", "two", "three"]) "".join(["one", "two", "three"]) " ".join("one", "two", "three") " ".join(["one", "two", "three"]) None 29. 文字列text = "This is a test"から改行を挿入して「This is\na test」にするために必要なコードはどれですか? text.replace(" ", "\n", 1) text.replace(" ", "\n") text.insert(" ", "\n") text.split(" ", 1) None 30. 次のコードを実行した場合の出力は何ですか? text = "Line1\nLine2\nLine3"result = text.split("\n")print(result[1]) Line1 Line2 Line3 ['Line1', 'Line2', 'Line3'] None 31. 次のコードの出力結果を求めてください。 text = "Hello"result = text + " " * 3 + "World"print(result) Hello World HelloWorld Hello Hello World None 32. 次のコードを実行したときの出力結果は何でしょうか? x = [1, 2, 3]def modify_list():global xx.append(4)modify_list()print(x) [4] エラー [1, 2, 3] [1, 2, 3, 4] None 33. 次のコードを実行したときの出力結果は何でしょうか? z = [10, 20, 30]def modify_list():global zz[1] = 99modify_list()print(z) [10, 99, 30] [10, 20, 30] エラー [99, 99, 99] None 34. 次のコードを実行したときの出力結果は何でしょうか? x = 10def modify_variable():x = 20return xprint(modify_variable())print(x) 20 20 20 10 10 20 エラー None 35. 次のコードの問題点として正しいものを選んでください。 if 5 > 3:print("5 is greater than 3") コロン(:)が欠けている print文が間違っている 構文に問題はない インデントが正しくない None 36. ローカル変数が削除されるタイミングとして正しいものはどれですか? プログラムが終了したとき。 ローカル変数がグローバル変数に変換されたとき。 ローカル変数は明示的に削除しなければならない。 関数の実行が完了したとき。 None 37. 次のコードの実行結果を選んでください。 try:raise RuntimeError("Runtime issue")except RuntimeError as e:print("Caught:", e)raise RuntimeError: Runtime issue プログラムが停止するが、出力はない 何も出力されない "Caught: Runtime issue"が出力され、エラーが再発生する None 38. 次のコードを実行した場合の出力は何ですか? items = ["apple", "banana", "cherry"]output = " - ".join(items)print(output) apple, banana, cherry apple - banana - cherry apple banana cherry ['apple', 'banana', 'cherry'] None 39. 次のコードを実行したときにエラーが発生する理由として正しいものを選んでください。 def example_function():x += 1print(x)example_function() xがローカル変数として定義されていないから。 globalキーワードを使用していないから。 エラーは発生しません。 グローバル変数xが未定義だから。 None 40. 次のコードを実行したときの出力結果は何でしょうか? def outer_function():x = 10def inner_function():x = x + 5return xreturn inner_function()print(outer_function()) 15 エラー 10 5 None Time's up