Python 3エンジニア認定基礎試験~模擬試験②~ 2024年12月10日2024年12月10日 ailearn 1. 次のコードを実行したときの出力結果は何でしょうか? 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 2. ローカル変数が削除されるタイミングとして正しいものはどれですか? プログラムが終了したとき。 ローカル変数がグローバル変数に変換されたとき。 ローカル変数は明示的に削除しなければならない。 関数の実行が完了したとき。 None 3. 次のコードを実行した場合の出力は何ですか? items = ["apple", "banana", "cherry"]output = " - ".join(items)print(output) apple, banana, cherry apple - banana - cherry apple banana cherry ['apple', 'banana', 'cherry'] None 4. 次のコードの出力結果を求めてください。 text = "Line1\nLine2\nLine3"print(text.replace("\n", " | ")) Line1 Line2 Line3 Line1\nLine2\nLine3 Line1 | Line2 | Line3 Line1 - Line2 - Line3 None 5. 次のコードの問題点として正しいものを選んでください。 if Trueprint("This is true") コロン(:)が欠けている インデントが不正である printの括弧が間違っている 構文に問題はない None 6. Pythonの文字列で改行を削除するための適切なメソッドはどれですか? join() strip() replace() split() None 7. 文字列「Hello\nWorld」を一行にまとめて「Hello World」にするために適切な方法はどれですか? "Hello\nWorld".replace("\n", " ") "Hello\nWorld".strip() "Hello\nWorld".join(" ") "Hello\nWorld".split("\n") None 8. 次のコードを実行したときの出力結果は何でしょうか? def outer_function():x = 10def inner_function():nonlocal xx = x + 5return xreturn inner_function()print(outer_function()) 10 5 エラー 15 None 9. 次のコードを実行した場合の出力は何ですか? a = "Python"b = "Programming"result = a + " " + bprint(result) PythonProgramming Python Programming Python-Programming Python + Programming None 10. 次のコードを実行したときの出力結果は何でしょうか? def reset_list(values):values = [0, 0, 0]numbers = [1, 2, 3]reset_list(numbers)print(numbers) [0, 0, 0] [1, 2, 3] [] エラー None 11. グローバル変数を関数内で変更するために使用するキーワードは何ですか? local nonlocal global static None 12. 次のうち、Pythonの文字列で改行を保持したまま文字列を定義する方法として正しいものはどれですか? シングルクォートで囲む ダブルクォートで囲む 三重クォート(""")で囲む バックスラッシュで囲む None 13. 次のコードを実行したときの出力結果は何でしょうか? x = [1, 2, 3]def modify_list():global xx.append(4)modify_list()print(x) [4] エラー [1, 2, 3] [1, 2, 3, 4] None 14. 次のコードの実行結果として正しいものを選んでください。 print("Hello "Hello" SyntaxError: EOL while scanning string literal SyntaxError: invalid syntax 空文字列が出力される None 15. raise文の役割として正しいものを選んでください。 明示的に例外を発生させる 例外をキャッチする 例外を無視する 例外を記録する None 16. 次のコードの出力結果は何でしょうか? x = 0def example_function():global xx = x + 2return xprint(example_function())print(x) 2 0 2 2 0 2 エラー None 17. 次のコードの出力結果を求めてください。 words = ["data", "science", "python"]sentence = " ".join(words)print(sentence) data science python datasciencepython data-science-python ['data', 'science', 'python'] None 18. 次のコードを実行したときの出力結果は何でしょうか? x = 5def outer_function():global xx = x * 2outer_function()print(x) 5 10 エラー None None 19. 次のコードを実行したときの出力結果は何でしょうか? x = 3def modify_variable():global xx = x * 2return xprint(modify_variable())print(x) 6 6 3 6 エラー 6 3 None 20. 変数name = "Alice"とage = 25の内容を使って「Alice is 25 years old」という文字列を生成するために入力すべきコードはどれですか? "{} is {} years old".format(name, age) f"{name} is {age} years old" name + " is " + str(age) + " years old" すべて正しい None 21. 次のコードの出力として正しいものを選んでください。 try:raise Exception("Generic error")except Exception as e:print(type(e).__name__, ":", e) "Generic error" "TypeError : Generic error" Exceptionが発生してプログラムが停止する "Exception : Generic error" None 22. 次のコードで出力を1行にまとめるために使用する引数はどれですか? print("Hello")print("World") end sep start stop None 23. 次のコードの実行結果として正しいものを選んでください。 def check_divisor(y):if y == 0:raise ZeroDivisionError("Divisor cannot be zero")return 10 / ytry:print(check_divisor(0))except ZeroDivisionError as e:print("Error:", e) 0 ZeroDivisionError: Divisor cannot be zero "Error: Divisor cannot be zero" エラーが発生する None 24. 次のコードの問題点として正しいものを選んでください。 if 5 > 3:print("5 is greater than 3") コロン(:)が欠けている print文が間違っている 構文に問題はない インデントが正しくない None 25. 次のコードの実行結果を選んでください。 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 26. 次のコードを実行した場合の出力は何ですか? text = "Line1\nLine2\nLine3"lines = text.splitlines()print(lines) Line1 Line2 Line3 ['Line1', 'Line2', 'Line3'] Line1\nLine2\nLine3 ['Line1\nLine2\nLine3'] None 27. Pythonのsplitlines()メソッドはどのような機能を持っていますか? 文字列を空白ごとに分割する 文字列を改行ごとに分割する 文字列の改行を削除する 文字列の末尾に改行を追加する None 28. Pythonで複数行にわたる文字列を作成するために使う方法として正しいものはどれですか? シングルクォートで囲む ダブルクォートで囲む 三重クォート(""")で囲む セミコロンで区切る None 29. 次のリスト["one", "two", "three"]をスペース区切りで結合し、"one two three"という文字列を生成するコードはどれですか? ",".join(["one", "two", "three"]) "".join(["one", "two", "three"]) " ".join("one", "two", "three") " ".join(["one", "two", "three"]) None 30. 次のコードを実行したときの出力結果は何でしょうか? def extend_list(item, target=None):if target is None:target = []target.append(item)return targetlist1 = extend_list(1)list2 = extend_list(2, [])list3 = extend_list(3)print(list1)print(list2)print(list3) [1] [2] [3] [1, 3] [2] [3] [1] [2] [1, 3] None 31. 次のコードを実行したときの出力結果は何でしょうか? y = 100def modify_variable():global yy = "Changed"modify_variable()print(y) "Changed" 100 エラー None None 32. 次のコードの実行結果を選んでください。 if True print("This will not work") This will not work IndentationError: expected an indented block エラーは発生しない SyntaxError: invalid syntax None 33. 次のコードを実行したときの出力結果は何でしょうか? x = 10def modify_variable():x = 20return xprint(modify_variable())print(x) 20 20 20 10 10 20 エラー None 34. 次のコードを実行したときの出力結果は何でしょうか? 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 35. 次のコードで正しい出力を選んでください。 try:raise RuntimeError("Unexpected error occurred")except RuntimeError as e:print(e) "Unexpected error occurred" "RuntimeError: Unexpected error occurred" RuntimeErrorが発生してプログラムが停止する 何も出力されない None 36. Pythonで文字列を結合するために使用される演算子はどれですか? + * % / None 37. 次のコードを実行したときの出力結果は何でしょうか? def outer_function():x = 10def inner_function():x = x + 5return xreturn inner_function()print(outer_function()) 15 エラー 10 5 None 38. 次のコードの実行結果を選んでください。 if True:print("This is true") "This is true" エラーは発生しない SyntaxError: invalid syntax IndentationError: expected an indented block None 39. 次のコードの実行結果として正しいものを選んでください。 def my_function():y = 7print(y)my_function()print(y) 7 7 7 エラー エラー 7 エラー エラー None 40. 次のコードの出力結果を求めてください。 text = "Hello"result = text + " " * 3 + "World"print(result) Hello World HelloWorld Hello Hello World None Time's up