Python 3エンジニア認定基礎試験~模擬試験②~ 2024年12月10日2024年12月10日 ailearn 1. 次のコードの実行結果を選んでください。 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 2. 次のコードを実行した場合の出力結果を選んでください。 def set_variable():y = 20return yset_variable()print(y) 20 None エラー 0 None 3. 次のコードの実行結果を選んでください。 for i in range(3)print(i) 0 1 2 エラーは発生しない SyntaxError: expected ':' IndentationError: expected an indented block None 4. 次のコードを実行したときの出力結果は何でしょうか? def counter():count = 0def increment():nonlocal countcount += 1return countreturn incrementincrementer = counter()print(incrementer())print(incrementer()) 1 1 1 2 2 2 エラー None 5. 文字列text = "Line1\nLine2\nLine3"に含まれる改行ごとに分割し、リストにする方法はどれですか? text.splitlines() text.split("\n") text.split() text.splitlines() と text.split("\n") None 6. 次のコードの出力結果は何でしょうか? x = 5def modify_variable():global xx += 10print(x)modify_variable()print(x) 15 15 15 5 エラー 10 15 None 7. 次のコードで正しい出力を選んでください。 try:raise RuntimeError("Unexpected error occurred")except RuntimeError as e:print(e) "Unexpected error occurred" "RuntimeError: Unexpected error occurred" RuntimeErrorが発生してプログラムが停止する 何も出力されない None 8. 次のうち、文字列"Hello"を5回繰り返した文字列を生成するためのコードはどれですか? "Hello".repeat(5) "Hello".multiply(5) "Hello" * 5 "Hello".join(5) None 9. 次のコードの出力結果を求めてください。 text = "Welcome"repeat_text = text * 3print(repeat_text) WelcomeWelcomeWelcome Welcome*3 Welcome 3 Error None 10. 次のコードで発生する例外の種類として正しいものを選んでください。 raise KeyError("Key not found") ValueError TypeError IndexError KeyError None 11. 次のコードの実行結果を選んでください。 x = 10if x > 5print("x is greater than 5") "x is greater than 5" SyntaxError: expected ':' IndentationError: expected an indented block エラーは発生しない None 12. 次のコードの出力として正しいものを選んでください。 try:raise Exception("Generic error")except Exception as e:print(type(e).__name__, ":", e) "Generic error" "TypeError : Generic error" Exceptionが発生してプログラムが停止する "Exception : Generic error" None 13. 次のコードの出力結果を求めてください。 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 14. 次のコードの実行結果を選んでください。 if 5 > 3print("5 is greater than 3") SyntaxError: expected ':' 5 is greater than 3 IndentationError: expected an indented block エラーは発生しない None 15. 次のコードの実行結果を選んでください。 for i in range(3):print(i) 0 1 2 エラーは発生しない IndentationError: expected an indented block SyntaxError: invalid syntax None 16. Pythonのsplitlines()メソッドはどのような機能を持っていますか? 文字列を空白ごとに分割する 文字列を改行ごとに分割する 文字列の改行を削除する 文字列の末尾に改行を追加する None 17. ローカル変数とグローバル変数のスコープを区別するために使用されるキーワードはどれですか? local nonlocal static global None 18. ローカル変数に関する正しい説明はどれですか? ローカル変数は、関数内でのみアクセスできます。 ローカル変数は、すべての関数で共有されます。 ローカル変数は、グローバル変数と同じスコープを持ちます。 ローカル変数は、自動的にグローバル変数に変換されます。 None 19. 次のコードの出力結果を求めてください。 text = "Hello"result = text + " " * 3 + "World"print(result) Hello World HelloWorld Hello Hello World None 20. 次のコードで「Hello」と「World」を改行で区切らずに出力するために必要な引数はどれですか? print("Hello")print("World") sep=" " end=" " sep="" end="" None 21. 文字列text = "This is a test"から改行を挿入して「This is\na test」にするために必要なコードはどれですか? text.replace(" ", "\n", 1) text.replace(" ", "\n") text.insert(" ", "\n") text.split(" ", 1) None 22. 次のコードを実行したときの出力結果は何でしょうか? x = 10def modify_variable():x = x + 5return xprint(modify_variable()) 15 10 エラー 5 None 23. 次のコードの出力結果を求めてください。 text = "Line1\nLine2\nLine3"print(text.replace("\n", " | ")) Line1 Line2 Line3 Line1\nLine2\nLine3 Line1 | Line2 | Line3 Line1 - Line2 - Line3 None 24. 文字列「Hello\nWorld」を一行にまとめて「Hello World」にするために適切な方法はどれですか? "Hello\nWorld".replace("\n", " ") "Hello\nWorld".strip() "Hello\nWorld".join(" ") "Hello\nWorld".split("\n") None 25. 次のコードを実行したときの出力結果は何でしょうか? z = [10, 20, 30]def modify_list():global zz[1] = 99modify_list()print(z) [10, 99, 30] [10, 20, 30] エラー [99, 99, 99] None 26. 次のコードでraise文の動作として正しい説明を選んでください。 if not isinstance(x, int):raise TypeError("x must be an integer") xが整数の場合にTypeErrorを発生させる xが整数でない場合にTypeErrorを発生させる 例外を無視する プログラムが正常に終了する None 27. 次のコードの実行結果を選んでください。 def check_value(x):if x < 0:raise ValueError("Negative value not allowed")return x * 2try:print(check_value(-5))except ValueError as e:print("Error:", e) -10 "Error: Negative value not allowed" エラーが発生してプログラムが停止する 何も出力されない None 28. 次のコードの問題点として正しいものを選んでください。 for i in range(5)print(i) print文が正しくない range関数が間違っている コロン(:)が欠けている 構文に問題はない None 29. 次のコードを実行したときの出力結果は何でしょうか? 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 30. 次のコードの実行結果を選んでください。 try:raise TypeError("Invalid type")except Exception as e:print("Caught Exception:", e) 何も出力されない エラーが発生してプログラムが停止する "Caught TypeError: Invalid type" "Caught Exception: Invalid type" None 31. ローカル変数が削除されるタイミングとして正しいものはどれですか? プログラムが終了したとき。 ローカル変数がグローバル変数に変換されたとき。 ローカル変数は明示的に削除しなければならない。 関数の実行が完了したとき。 None 32. 変数greeting = "Hello"とname = "Alice"を使って、「Hello, Alice!」と出力するためのコードはどれですか? print(f"{greeting}, {name}!") print("{}! {}".format(greeting, name)) print(greeting, ",", name, "!") print(f"{greeting}, {name}!"), "Hello" + name None 33. 次のコードを実行したときの出力結果は何でしょうか? x = 5def multiply_by_two(x):x = x * 2return xprint(multiply_by_two(x))print(x) 10 10 10 5 5 5 エラー None 34. 次のコードの実行結果を選んでください。 def add(a, b)return a + b SyntaxError: expected ':' エラーは発生しない IndentationError: expected an indented block NameError: name 'a' is not defined None 35. 次のコードの問題点として正しいものを選んでください。 if 5 > 3:print("5 is greater than 3") コロン(:)が欠けている print文が間違っている 構文に問題はない インデントが正しくない None 36. 次のうち、Pythonの文字列において改行を削除する方法として正しいものはどれですか? replace("\n", "") strip() split() join() None 37. 次のコードを実行したときの出力結果は何でしょうか? 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 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. 次のコードに関する正しい説明はどれですか? x = 10def example_function():x = 5print(x)example_function()print(x) グローバル変数xが変更されます。 ローカル変数xとグローバル変数xは別物として扱われます。 エラーが発生します。 example_function内のローカル変数xは、グローバル変数xと同期されます。 None 40. グローバル変数を使用する際の注意点として適切なものはどれですか? グローバル変数はなるべく使用を避けるべきである。 グローバル変数はどの関数でも自由に変更してよい。 グローバル変数の値は自動的に保存される。 グローバル変数はglobalキーワードを使うとローカル変数になる。 None Time's up