Python 3エンジニア認定基礎試験~模擬試験②~ 2024年12月10日2024年12月10日 ailearn 1. グローバル変数を使用する際の注意点として適切なものはどれですか? グローバル変数はなるべく使用を避けるべきである。 グローバル変数はどの関数でも自由に変更してよい。 グローバル変数の値は自動的に保存される。 グローバル変数はglobalキーワードを使うとローカル変数になる。 None 2. 次のコードを実行したときの出力結果は何でしょうか? x = 10def modify_variable():x = 20return xprint(modify_variable())print(x) 20 20 20 10 10 20 エラー None 3. 次のコードの実行結果として正しいものを選んでください。 print("Hello "Hello" SyntaxError: EOL while scanning string literal SyntaxError: invalid syntax 空文字列が出力される None 4. 次のコードの問題点として正しいものを選んでください。 def my_function()return "Hello" return文が間違っている 関数名が正しくない 構文に問題はない コロン(:)が欠けている None 5. Pythonで文字列を結合するために使用される演算子はどれですか? + * % / None 6. 次のコードを実行したときの出力結果は何でしょうか? 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 7. 次のコードを実行したときの出力結果は何でしょうか? 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 8. 次のコードの出力を1行にするために使うべき引数はどれですか? print("Hello")print("Python") end=" " sep=" " end="\n" sep="\n" None 9. 次のコードを実行した場合の結果として正しいものを選んでください。 try:raise ValueError("Invalid value")except ValueError as e:print("Caught exception:", e) ValueError: Invalid value "Caught exception: Invalid value" エラーが発生する 何も出力されない None 10. 次のコードを実行した場合の出力は何ですか? a = "Python"b = "Programming"result = a + " " + bprint(result) PythonProgramming Python Programming Python-Programming Python + Programming None 11. 次のコードの出力結果は何でしょうか? x = 0def example_function():global xx = x + 2return xprint(example_function())print(x) 2 0 2 2 0 2 エラー None 12. 次のコードの結果を選択してください。 x = 10def my_function():global xx += 5my_function()print(x) 15 10 エラー 5 None 13. 次のコードの出力結果を求めてください。 text = "Line1\nLine2\nLine3"print(text.replace("\n", " | ")) Line1 Line2 Line3 Line1\nLine2\nLine3 Line1 | Line2 | Line3 Line1 - Line2 - Line3 None 14. 次のコードの出力結果を求めてください。 text = "Python"result = text + " " + text[::-1]print(result) Python Python Python nohtyP Python PythonPython Python None 15. 次のコードの実行結果を選んでください。 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 16. 次のコードを実行したときの出力結果は何でしょうか? x = 10def modify_variable():x = x + 5return xprint(modify_variable()) 15 10 エラー 5 None 17. リスト["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 18. Pythonの文字列で改行を削除するための適切なメソッドはどれですか? join() strip() replace() split() None 19. 次のコードの実行結果として正しいものを選んでください。 def my_function():y = 7print(y)my_function()print(y) 7 7 7 エラー エラー 7 エラー エラー None 20. 次のコードを実行したときの出力結果は何でしょうか? x = [1, 2, 3]def modify_list():global xx.append(4)modify_list()print(x) [4] エラー [1, 2, 3] [1, 2, 3, 4] None 21. 次のコードを実行した場合の出力は何ですか? text = "Line1\nLine2\nLine3"lines = text.splitlines()print(lines) Line1 Line2 Line3 ['Line1', 'Line2', 'Line3'] Line1\nLine2\nLine3 ['Line1\nLine2\nLine3'] None 22. 次のコードを実行したときの出力結果は何でしょうか? x = 5def modify_variable():global xx = x * 2return xprint(modify_variable())print(x) 10 10 5 10 10 5 エラー None 23. 次のコードの実行結果を選んでください。 try:raise TypeError("Invalid type")except Exception as e:print("Caught Exception:", e) 何も出力されない エラーが発生してプログラムが停止する "Caught TypeError: Invalid type" "Caught Exception: Invalid type" None 24. 次のコードを実行したときの出力結果は何でしょうか? 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 25. 次のコードでraise文の動作として正しい説明を選んでください。 if not isinstance(x, int):raise TypeError("x must be an integer") xが整数の場合にTypeErrorを発生させる xが整数でない場合にTypeErrorを発生させる 例外を無視する プログラムが正常に終了する None 26. 次のコードを実行したときの出力結果は何でしょうか? x = 5def multiply_by_two(x):x = x * 2return xprint(multiply_by_two(x))print(x) 10 10 10 5 5 5 エラー None 27. 次のコードの実行結果を選んでください。 try:raise ValueError("This is a value error")except ValueError as e:print("Caught error:", e) "Caught error: This is a value error" ValueError: This is a value error エラーが発生してプログラムが停止する 何も出力されない None 28. Pythonで複数行にわたる文字列を作成するために使う方法として正しいものはどれですか? シングルクォートで囲む ダブルクォートで囲む 三重クォート(""")で囲む セミコロンで区切る None 29. 次のコードの実行結果を選んでください。 print("Missing parenthesis in print statement" SyntaxError: invalid syntax SyntaxError: unexpected EOF while parsing エラーは発生しない "Missing parenthesis in print statement" None 30. 次のコードの実行結果を選んでください。 try:raise RuntimeError("Runtime issue")except RuntimeError as e:print("Caught:", e)raise RuntimeError: Runtime issue プログラムが停止するが、出力はない 何も出力されない "Caught: Runtime issue"が出力され、エラーが再発生する None 31. 次のリスト["one", "two", "three"]をスペース区切りで結合し、"one two three"という文字列を生成するコードはどれですか? ",".join(["one", "two", "three"]) "".join(["one", "two", "three"]) " ".join("one", "two", "three") " ".join(["one", "two", "three"]) None 32. 文字列text = "Line1\nLine2\nLine3"に含まれる改行ごとに分割し、リストにする方法はどれですか? text.splitlines() text.split("\n") text.split() text.splitlines() と text.split("\n") None 33. 変数animal = "cat"とsound = "meow"を使って、「The cat says meow」という文字列を生成するために入力すべきコードはどれですか? "The {} says {}".format(animal, sound) f"The {animal} says {sound}" "The " + animal + " says " + sound すべて正しい None 34. 次のコードの出力結果は何でしょうか? x = 5def modify_variable():global xx += 10print(x)modify_variable()print(x) 15 15 15 5 エラー 10 15 None 35. Pythonの文字列で改行を表すエスケープシーケンスはどれですか? \r \n \t \\ None 36. 次のコードの問題点として正しいものを選んでください。 if x = 10:print("x is 10") コロン(:)が間違っている 代入文(=)ではなく比較演算子(==)を使用する必要がある xが未定義 構文に問題はない None 37. 次のコードの問題点として正しいものを選んでください。 print "Hello, World!" Python 3ではprintは関数として使用されるため、括弧が必要 Hello, World!が正しくない 変数printが未定義 構文に問題はない None 38. 次のコードを実行したときの出力結果は何でしょうか? def create_multiplier(factor):def multiplier(x):return x * factorreturn multipliertimes_three = create_multiplier(3)times_five = create_multiplier(5)print(times_three(10))print(times_five(10)) 10 10 3 5 エラー 30 50 None 39. 次のコードの出力結果を求めてください。 text = "Hello"result = text + " " * 3 + "World"print(result) Hello World HelloWorld Hello Hello World None 40. 次のコードを実行したときの出力結果は何でしょうか? def my_function():x = 5x += 10return xprint(my_function()) 5 10 15 エラー None Time's up