Python 3エンジニア認定基礎試験~模擬試験②~ 2024年12月10日2024年12月10日 ailearn 1. 次のコードで出力を1行にまとめるために使用する引数はどれですか? print("Hello")print("World") end sep start stop None 2. Pythonの文字列で改行を表すエスケープシーケンスはどれですか? \r \n \t \\ None 3. 次のコードを実行したときの出力結果は何でしょうか? x = 10def modify_variable():global xx = "Hello"modify_variable()print(x) 10 "Hello" エラー None None 4. 変数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 5. 次のコードを実行した場合の出力は何ですか? text = """Line1Line2Line3"""print(text.splitlines()[0]) Line1 Line2 Line3 Line1 Line2 Line3 None 6. ミュータブル(変更可能)なオブジェクトに該当するものはどれですか? タプル 文字列 リスト 数値 None 7. 次のコードの出力結果を求めてください。 text = "Hello"result = text + " " * 3 + "World"print(result) Hello World HelloWorld Hello Hello World None 8. 次のコードの実行結果を選んでください。 while True print("Infinite loop") SyntaxError: expected ':' "Infinite loop"が無限に出力される IndentationError: expected an indented block エラーは発生しない None 9. 次のコードを実行したときの出力結果は何でしょうか? def outer_function():x = 10def inner_function():nonlocal xx = x + 5return xreturn inner_function()print(outer_function()) 10 5 エラー 15 None 10. 文字列の結合にjoin()メソッドを使用する場合、リスト["apple", "banana", "cherry"]をカンマ区切りの文字列にする正しいコードはどれですか? ", ".join("apple", "banana", "cherry") ", ".join(["apple", "banana", "cherry"]) " ".join(["apple", "banana", "cherry"]) join(",", ["apple", "banana", "cherry"]) None 11. 文字列の中に改行を含めるにはどの方法を使うべきですか? スペースを追加する バックスラッシュと「n」を使用する ドット(.)を追加する バックスラッシュと「t」を使用する None 12. 次のコードの結果を選択してください。 x = [1, 2, 3]def modify_list(lst):lst.append(4)modify_list(x)print(x) [1, 2, 3, 4] [1, 2, 3] エラー [] None 13. 変数animal = "cat"とsound = "meow"を使って、「The cat says meow」という文字列を生成するために入力すべきコードはどれですか? "The {} says {}".format(animal, sound) f"The {animal} says {sound}" "The " + animal + " says " + sound すべて正しい None 14. 文字列フォーマットのf-string(フォーマット文字列)の記述方法として正しいものはどれですか? "Hello, {name}" "Hello, f{name}" format("Hello, {name}") f"Hello, {name}" None 15. 次のコードの実行結果を選んでください。 x = 10if x > 5print("x is greater than 5") "x is greater than 5" SyntaxError: expected ':' IndentationError: expected an indented block エラーは発生しない None 16. 次のコードを実行したときの出力結果は何でしょうか? def test_function():y = 5y += 1return yprint(test_function())print(y) 5 5 6 5 エラー エラー 6 エラー None 17. 変数greeting = "Hello"とname = "Alice"を使って、「Hello, Alice!」と出力するためのコードはどれですか? print(f"{greeting}, {name}!") print("{}! {}".format(greeting, name)) print(greeting, ",", name, "!") print(f"{greeting}, {name}!"), "Hello" + name None 18. "Python3"という文字列から「3」を削除して"Python"にするコードはどれですか? remove("3") "Python3".replace("3", "") delete("3") strip("3") None 19. 次のコードの実行結果を選んでください。 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 20. 次のコードの問題点として正しいものを選んでください。 if 5 > 3:print("5 is greater than 3") コロン(:)が欠けている print文が間違っている 構文に問題はない インデントが正しくない None 21. 次のコードを実行したときの出力結果は何でしょうか? x = 1def first_function():global xx += 1def second_function():global xx *= 3first_function()second_function()print(x) 2 3 6 9 None 22. 次のコードを実行した場合の出力は何ですか? text = "Line1\nLine2\nLine3"lines = text.splitlines()print(lines) Line1 Line2 Line3 ['Line1', 'Line2', 'Line3'] Line1\nLine2\nLine3 ['Line1\nLine2\nLine3'] None 23. 次のコードの実行結果を選んでください。 try:raise KeyError("Key not found")except KeyError as e:print("Caught KeyError:", e)raise "Caught KeyError: 'Key not found'"が出力され、例外が再発生する "Caught KeyError: Key not found"が出力され、プログラムが正常終了する KeyError: 'Key not found'が発生してプログラムが停止する 何も出力されない None 24. 次のコードの出力結果を求めてください。 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 25. 文字列text = "This is a test"から改行を挿入して「This is\na test」にするために必要なコードはどれですか? text.replace(" ", "\n", 1) text.replace(" ", "\n") text.insert(" ", "\n") text.split(" ", 1) None 26. 次のコードの実行結果を選んでください。 for i in range(3):print(i) 0 1 2 エラーは発生しない SyntaxError: invalid syntax IndentationError: expected an indented block None 27. 次のコードの実行結果を選んでください。 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 28. 次のコードのエラー原因として正しいものを選んでください。 def calculate():total = total + 1print(total)calculate() グローバル変数totalが未定義のため。 totalがローカル変数として初期化されていないため。 関数calculateが定義されていないため。 エラーは発生しない。 None 29. 次のコードを実行したときの出力結果は何でしょうか? 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 30. 次のコードを実行したときの出力結果は何でしょうか? x = 10def modify_variable():global xx = x + 5return xprint(modify_variable())print(x) 15 15 エラー 10 15 15 10 None 31. 次のコードを実行したときの出力結果は何でしょうか? z = [10, 20, 30]def modify_list():global zz[1] = 99modify_list()print(z) [10, 99, 30] [10, 20, 30] エラー [99, 99, 99] None 32. 次のコードの問題点として正しいものを選んでください。 if x = 10:print("x is 10") コロン(:)が間違っている 代入文(=)ではなく比較演算子(==)を使用する必要がある xが未定義 構文に問題はない None 33. 次のコードを実行したときの出力結果は何でしょうか? 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 34. 次のコードでraise文の動作として正しい説明を選んでください。 if not isinstance(x, int):raise TypeError("x must be an integer") xが整数の場合にTypeErrorを発生させる xが整数でない場合にTypeErrorを発生させる 例外を無視する プログラムが正常に終了する None 35. 次のコードを実行したときの出力結果は何でしょうか? x = 10def modify_variable():x = 20return xprint(modify_variable())print(x) 20 20 20 10 10 20 エラー None 36. 次のコードの実行結果を選んでください。 try:raise RuntimeError("Runtime issue")except RuntimeError as e:print("Caught:", e)raise RuntimeError: Runtime issue プログラムが停止するが、出力はない 何も出力されない "Caught: Runtime issue"が出力され、エラーが再発生する None 37. 次のコードの問題点として正しいものを選んでください。 def my_function()return "Hello" return文が間違っている 関数名が正しくない 構文に問題はない コロン(:)が欠けている None 38. 次のコードの出力結果を求めてください。 text = "Line1\nLine2\nLine3"print(text.replace("\n", " | ")) Line1 Line2 Line3 Line1\nLine2\nLine3 Line1 | Line2 | Line3 Line1 - Line2 - Line3 None 39. 変数first = "Hello"とsecond = "World"を使って、「Hello, World!」という文字列を生成するために適切なコードはどれですか? f"{first}, {second}!" first + ", " + second + "!" "{}, {}!".format(first, second) すべて正しい None 40. 次のコードを実行したときの出力結果は何でしょうか? x = 3def modify_variable():global xx = x * 2return xprint(modify_variable())print(x) 6 6 3 6 エラー 6 3 None Time's up