Python 3エンジニア認定基礎試験~模擬試験②~ 2024年12月10日2024年12月10日 ailearn 1. 次のコードの実行結果を選んでください。 try:raise NameError("Variable not defined")except ValueError as e:print("Caught ValueError:", e)except NameError as e:print("Caught NameError:", e) "Caught ValueError: Variable not defined" "Caught NameError: Variable not defined" エラーが発生してプログラムが停止する 何も出力されない None 2. 次のコードを実行した場合の出力は何ですか? text = "Line1\nLine2\nLine3"lines = text.splitlines()print(lines) Line1 Line2 Line3 ['Line1', 'Line2', 'Line3'] Line1\nLine2\nLine3 ['Line1\nLine2\nLine3'] None 3. Pythonで文字列を結合するために使用される演算子はどれですか? + * % / None 4. 次のコードのnonlocalキーワードの用途として正しいものはどれですか? def outer_function():x = 10def inner_function():nonlocal xx += 5 グローバル変数を変更する。 内部関数内で新しい変数を作成する。 外側スコープの変数を参照して変更する。 エラーを防ぐために使用される。 None 5. 次のコードで発生する例外の種類として正しいものを選んでください。 raise KeyError("Key not found") ValueError TypeError IndexError KeyError None 6. 次のうち、Pythonの文字列で改行を保持したまま文字列を定義する方法として正しいものはどれですか? シングルクォートで囲む ダブルクォートで囲む 三重クォート(""")で囲む バックスラッシュで囲む None 7. 次のコードを実行したときの出力結果は何でしょうか? 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 8. 次のコードの問題点として正しいものを選んでください。 while Trueprint("Looping") print文が正しくない 無限ループが発生する コロン(:)が欠けている 構文に問題はない None 9. 次のコードの出力結果は何でしょうか? x = 0def example_function():global xx = x + 2return xprint(example_function())print(x) 2 0 2 2 0 2 エラー None 10. ローカル変数に関する正しい説明はどれですか? ローカル変数は関数の外で使用できます。 ローカル変数は関数の中でのみ有効です。 ローカル変数は他の関数で自動的に共有されます。 ローカル変数はグローバル変数として自動的に変換されます。 None 11. 次のコードを実行したときの出力結果は何でしょうか? 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 12. 次のコードに関する正しい説明はどれですか? x = 10def example_function():x = 5print(x)example_function()print(x) グローバル変数xが変更されます。 ローカル変数xとグローバル変数xは別物として扱われます。 エラーが発生します。 example_function内のローカル変数xは、グローバル変数xと同期されます。 None 13. 次のコードの出力結果を求めてください。 text = "Line1\nLine2\nLine3"print(text.replace("\n", " | ")) Line1 Line2 Line3 Line1\nLine2\nLine3 Line1 | Line2 | Line3 Line1 - Line2 - Line3 None 14. 次のコードを実行したときの出力結果は何でしょうか? 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 15. 次のコードを実行したときの出力結果は何でしょうか? def modify_list(lst):lst.append(10)my_list = [1, 2, 3]modify_list(my_list)print(my_list) [1, 2, 3] エラー [1, 2, 3, 10] [10] None 16. 次のコードの結果を選択してください。 x = 10def my_function():global xx += 5my_function()print(x) 15 10 エラー 5 None 17. 変数animal = "cat"とsound = "meow"を使って、「The cat says meow」という文字列を生成するために入力すべきコードはどれですか? "The {} says {}".format(animal, sound) f"The {animal} says {sound}" "The " + animal + " says " + sound すべて正しい None 18. 次のリスト["one", "two", "three"]をスペース区切りで結合し、"one two three"という文字列を生成するコードはどれですか? ",".join(["one", "two", "three"]) "".join(["one", "two", "three"]) " ".join("one", "two", "three") " ".join(["one", "two", "three"]) None 19. 次のコードを実行したときの出力結果は何でしょうか? x = [1, 2, 3]def modify_list():global xx.append(4)modify_list()print(x) [4] エラー [1, 2, 3] [1, 2, 3, 4] None 20. 次のうち、文字列"Hello"を5回繰り返した文字列を生成するためのコードはどれですか? "Hello".repeat(5) "Hello".multiply(5) "Hello" * 5 "Hello".join(5) None 21. 次のコードで「Hello」と「World」を改行で区切らずに出力するために必要な引数はどれですか? print("Hello")print("World") sep=" " end=" " sep="" end="" None 22. 次のコードの実行結果として正しいものを選んでください。 x = 5def example_function():x = 10print(x)example_function()print(x) 10 5 10 10 5 10 エラー None 23. 次のコードを実行したときの出力結果は何でしょうか? def count_calls():count = 0def increment():nonlocal countcount += 1return countreturn incrementcounter = count_calls()print(counter())print(counter()) 1 2 0 1 1 1 エラー None 24. 次のコードを実行した場合の出力は何ですか? items = ["apple", "banana", "cherry"]output = " - ".join(items)print(output) apple, banana, cherry apple - banana - cherry apple banana cherry ['apple', 'banana', 'cherry'] None 25. 次のコードを実行したときの出力結果は何でしょうか? def outer_function():x = 10def inner_function():x = x + 5return xreturn inner_function()print(outer_function()) 15 エラー 10 5 None 26. 次のコードの出力結果を求めてください。 words = ["data", "science", "python"]sentence = " ".join(words)print(sentence) data science python datasciencepython data-science-python ['data', 'science', 'python'] None 27. 次のコードの出力結果を求めてください。 text = "Welcome"repeat_text = text * 3print(repeat_text) WelcomeWelcomeWelcome Welcome*3 Welcome 3 Error None 28. 次のコードを実行したときにエラーが発生する理由として正しいものを選んでください。 def example_function():x += 1print(x)example_function() xがローカル変数として定義されていないから。 globalキーワードを使用していないから。 エラーは発生しません。 グローバル変数xが未定義だから。 None 29. ローカル変数が削除されるタイミングとして正しいものはどれですか? プログラムが終了したとき。 ローカル変数がグローバル変数に変換されたとき。 ローカル変数は明示的に削除しなければならない。 関数の実行が完了したとき。 None 30. 次のコードを実行したときの出力結果は何でしょうか? 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 31. 次のコードの実行結果として正しいものを選んでください。 def check_positive(x):if x < 0:raise ValueError("Negative values are not allowed")return xtry:print(check_positive(-10))except ValueError as e:print(e) -10 ValueError: Negative values are not allowed "Negative values are not allowed" エラーが発生する None 32. グローバル変数に関する正しい説明はどれですか? グローバル変数は関数内でのみ使用できます。 グローバル変数はプログラム全体でアクセス可能です。 グローバル変数はglobalキーワードを使用しないと定義できません。 グローバル変数は関数内で自動的にローカル変数に変換されます。 None 33. 文字列「Hello\nWorld」を一行にまとめて「Hello World」にするために適切な方法はどれですか? "Hello\nWorld".replace("\n", " ") "Hello\nWorld".strip() "Hello\nWorld".join(" ") "Hello\nWorld".split("\n") None 34. 次のコードを実行したときの出力結果は何でしょうか? x = 5def outer_function():global xx = x * 2outer_function()print(x) 5 10 エラー None None 35. ミュータブル(変更可能)なオブジェクトに該当するものはどれですか? タプル 文字列 リスト 数値 None 36. 次のコードの実行結果として正しいものを選んでください。 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 37. 次のコードの実行結果を選んでください。 try:raise RuntimeError("Runtime issue")except RuntimeError as e:print("Caught:", e)raise RuntimeError: Runtime issue プログラムが停止するが、出力はない 何も出力されない "Caught: Runtime issue"が出力され、エラーが再発生する None 38. リスト["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 39. 改行を含む文字列"Hello\nWorld"をスペースで区切って一行にまとめるためのコードはどれですか? "Hello\nWorld".split(" ") "Hello\nWorld".replace("\n", " ") "Hello\nWorld".join(" ") "Hello\nWorld".strip() None 40. 次のコードを実行したときの出力結果は何でしょうか? x = 10def my_function():x = 20return xprint(my_function())print(x) 20 20 20 10 10 20 エラー None Time's up