Python 3エンジニア認定基礎試験~模擬試験②~ 2024年12月10日2024年12月10日 ailearn 1. 文字列text = "Line1\nLine2\nLine3"に含まれる改行ごとに分割し、リストにする方法はどれですか? text.splitlines() text.split("\n") text.split() text.splitlines() と text.split("\n") None 2. 次のコードの問題点として正しいものを選んでください。 while Trueprint("Looping") print文が正しくない 無限ループが発生する コロン(:)が欠けている 構文に問題はない None 3. 次のコードを実行したときの出力結果は何でしょうか? x = 5def outer_function():global xx = x * 2outer_function()print(x) 5 10 エラー None None 4. 次のコードの出力結果を求めてください。 text = "Python"result = text + " " + text[::-1]print(result) Python Python Python nohtyP Python PythonPython Python None 5. 次のコードを実行したときの出力結果は何でしょうか? 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 6. 次のコードを実行したときの出力結果は何でしょうか? x = 10def modify_variable():global xx = x + 5return xprint(modify_variable())print(x) 15 15 エラー 10 15 15 10 None 7. ローカル変数に関する正しい説明はどれですか? ローカル変数は、関数内でのみアクセスできます。 ローカル変数は、すべての関数で共有されます。 ローカル変数は、グローバル変数と同じスコープを持ちます。 ローカル変数は、自動的にグローバル変数に変換されます。 None 8. 次のコードの実行結果を選んでください。 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 9. 次のうち、Pythonの文字列において改行を削除する方法として正しいものはどれですか? replace("\n", "") strip() split() join() None 10. 次のコードを実行したときの出力結果は何でしょうか? def reset_counter():count = 0def increment():nonlocal countcount += 1return countdef reset():nonlocal countcount = 0return increment, resetincrement, reset = reset_counter()print(increment())print(increment())reset()print(increment()) 1 2 1 1 2 2 1 2 0 エラー None 11. ローカル変数が削除されるタイミングとして正しいものはどれですか? プログラムが終了したとき。 ローカル変数がグローバル変数に変換されたとき。 ローカル変数は明示的に削除しなければならない。 関数の実行が完了したとき。 None 12. 次のコードの問題点として正しいものを選んでください。 print "Hello, World!" Python 3ではprintは関数として使用されるため、括弧が必要 Hello, World!が正しくない 変数printが未定義 構文に問題はない None 13. 次のコードを実行したときの出力結果は何でしょうか? def outer_function():x = 5def inner_function():nonlocal xx += 10return xreturn inner_function()print(outer_function()) 5 10 15 エラー None 14. 次のコードの問題点として正しいものを選んでください。 for i in range(5)print(i) print文が正しくない range関数が間違っている コロン(:)が欠けている 構文に問題はない None 15. 次のコードの問題点として正しいものを選んでください。 def my_function()return "Hello" return文が間違っている 関数名が正しくない 構文に問題はない コロン(:)が欠けている None 16. 次のコードの実行結果を選んでください。 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 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. 次のコードの実行結果として正しいものを選んでください。 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 19. 次のコードを実行したときの出力結果は何でしょうか? x = 5def modify_variable():global xx = x * 2return xprint(modify_variable())print(x) 10 10 5 10 10 5 エラー None 20. 次のコードの出力結果を求めてください。 text = "Welcome"repeat_text = text * 3print(repeat_text) WelcomeWelcomeWelcome Welcome*3 Welcome 3 Error None 21. 次のコードの問題点として正しいものを選んでください。 if 5 > 3:print("5 is greater than 3") コロン(:)が欠けている print文が間違っている 構文に問題はない インデントが正しくない None 22. 次のうち、文字列の結合に使われないメソッドはどれですか? join() format() concat() f-string None 23. Pythonの文字列で改行を削除するための適切なメソッドはどれですか? join() strip() replace() split() None 24. 次のコードを実行したときの出力結果は何でしょうか? x = 10def modify_variable():x = x + 5return xprint(modify_variable()) 15 10 エラー 5 None 25. 次のコードに関する正しい説明はどれですか? x = 10def example_function():x = 5print(x)example_function()print(x) グローバル変数xが変更されます。 ローカル変数xとグローバル変数xは別物として扱われます。 エラーが発生します。 example_function内のローカル変数xは、グローバル変数xと同期されます。 None 26. ローカル変数に関する正しい説明はどれですか? ローカル変数は関数の外で使用できます。 ローカル変数は関数の中でのみ有効です。 ローカル変数は他の関数で自動的に共有されます。 ローカル変数はグローバル変数として自動的に変換されます。 None 27. 次のコードを実行した場合の出力結果を選んでください。 def set_variable():y = 20return yset_variable()print(y) 20 None エラー 0 None 28. 次のコードを実行したときの出力結果は何でしょうか? def reset_list(values):values = [0, 0, 0]numbers = [1, 2, 3]reset_list(numbers)print(numbers) [0, 0, 0] [1, 2, 3] [] エラー None 29. 次のコードの出力結果を求めてください。 words = ["data", "science", "python"]sentence = " ".join(words)print(sentence) data science python datasciencepython data-science-python ['data', 'science', 'python'] None 30. 次のコードの出力結果を求めてください。 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 31. 文字列"Hello World"のスペースを削除して"HelloWorld"とするために使用するメソッドはどれですか? remove(" ") replace(" ", "") delete(" ") join(" ") None 32. 次のコードの実行結果を選んでください。 x = 10if x > 5print("x is greater than 5") "x is greater than 5" SyntaxError: expected ':' IndentationError: expected an indented block エラーは発生しない None 33. 次のうち、文字列"Hello"を5回繰り返した文字列を生成するためのコードはどれですか? "Hello".repeat(5) "Hello".multiply(5) "Hello" * 5 "Hello".join(5) None 34. 次のコードの実行結果として正しいものを選んでください。 def my_function():y = 7print(y)my_function()print(y) 7 7 7 エラー エラー 7 エラー エラー None 35. 次のコードを実行したときの出力結果は何でしょうか? x = 10def modify_variable():x = 20return xprint(modify_variable())print(x) 20 20 20 10 10 20 エラー None 36. イミュータブルなオブジェクトに該当するものはどれですか? リスト 辞書 タプル セット None 37. 次のコードで発生する例外の種類として正しいものを選んでください。 raise KeyError("Key not found") ValueError TypeError IndexError KeyError None 38. 次のコードの出力を1行にするために使うべき引数はどれですか? print("Hello")print("Python") end=" " sep=" " end="\n" sep="\n" None 39. 次のコードの実行結果を選んでください。 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 40. 次のコードの実行結果を選んでください。 if 5 > 3print("5 is greater than 3") SyntaxError: expected ':' 5 is greater than 3 IndentationError: expected an indented block エラーは発生しない None Time's up