Python 3エンジニア認定基礎試験~模擬試験②~ 2024年12月10日2024年12月10日 ailearn 1. 次のコードを実行したときの出力結果は何でしょうか? def reset_list(values):values = [0, 0, 0]numbers = [1, 2, 3]reset_list(numbers)print(numbers) [0, 0, 0] [1, 2, 3] [] エラー None 2. 次のコードの実行結果を選んでください。 def validate_input(value):if not isinstance(value, int):raise TypeError("Value must be an integer")return value * 2try:print(validate_input("text"))except TypeError as e:print("Error:", e) ValueError: Value must be an integer TypeErrorが発生してプログラムが停止する "Error: Value must be an integer" 何も出力されない None 3. ローカル変数に関する正しい説明はどれですか? ローカル変数は関数の外で使用できます。 ローカル変数は関数の中でのみ有効です。 ローカル変数は他の関数で自動的に共有されます。 ローカル変数はグローバル変数として自動的に変換されます。 None 4. 次のコードを実行したときの出力結果は何でしょうか? x = [1, 2, 3]def modify_list():global xx.append(4)modify_list()print(x) [4] エラー [1, 2, 3] [1, 2, 3, 4] None 5. 次のコードの実行結果を選んでください。 print("Missing parenthesis in print statement" SyntaxError: invalid syntax SyntaxError: unexpected EOF while parsing エラーは発生しない "Missing parenthesis in print statement" None 6. 次のコードの出力結果を求めてください。 words = ["data", "science", "python"]sentence = " ".join(words)print(sentence) data science python datasciencepython data-science-python ['data', 'science', 'python'] None 7. 次のコードを実行したときの出力結果は何でしょうか? x = 5def outer_function():x = 10def inner_function():return xreturn inner_function()print(outer_function()) 10 5 15 エラー None 8. 文字列「Hello\nWorld」を一行にまとめて「Hello World」にするために適切な方法はどれですか? "Hello\nWorld".replace("\n", " ") "Hello\nWorld".strip() "Hello\nWorld".join(" ") "Hello\nWorld".split("\n") None 9. 次のリスト["apple", "banana", "cherry"]の各要素を改行で区切って表示するために、適切なコードはどれですか? print("\n".join(["apple", "banana", "cherry"])) print("apple\nbanana\ncherry") print("\n".join(["apple", "banana", "cherry"])) と print("apple\nbanana\ncherry") print(["apple", "banana", "cherry"]) None 10. 次のコードの出力結果を求めてください。 text = "Line1\nLine2\nLine3"print(text.replace("\n", " | ")) Line1 Line2 Line3 Line1\nLine2\nLine3 Line1 | Line2 | Line3 Line1 - Line2 - Line3 None 11. 次のコードのnonlocalキーワードの用途として正しいものはどれですか? def outer_function():x = 10def inner_function():nonlocal xx += 5 グローバル変数を変更する。 内部関数内で新しい変数を作成する。 外側スコープの変数を参照して変更する。 エラーを防ぐために使用される。 None 12. 次のコードの実行結果を選んでください。 while True print("Infinite loop") SyntaxError: expected ':' "Infinite loop"が無限に出力される IndentationError: expected an indented block エラーは発生しない None 13. 変数greeting = "Hello"とname = "Alice"を使って、「Hello, Alice!」と出力するためのコードはどれですか? print(f"{greeting}, {name}!") print("{}! {}".format(greeting, name)) print(greeting, ",", name, "!") print(f"{greeting}, {name}!"), "Hello" + name None 14. ローカル変数とグローバル変数のスコープを区別するために使用されるキーワードはどれですか? local nonlocal static global None 15. 次のうち、Pythonの文字列で改行を保持したまま文字列を定義する方法として正しいものはどれですか? シングルクォートで囲む ダブルクォートで囲む 三重クォート(""")で囲む バックスラッシュで囲む None 16. 文字列フォーマットのf-string(フォーマット文字列)の記述方法として正しいものはどれですか? "Hello, {name}" "Hello, f{name}" format("Hello, {name}") f"Hello, {name}" None 17. 次のコードの実行結果を選んでください。 x = [1, 2, 3 SyntaxError: invalid syntax エラーは発生しない SyntaxError: unexpected EOF while parsing 空のリストが出力される None 18. 次のコードで出力を1行にまとめるために使用する引数はどれですか? print("Hello")print("World") end sep start stop None 19. 次のコードに関する正しい説明はどれですか? x = 10def example_function():x = 5print(x)example_function()print(x) グローバル変数xが変更されます。 ローカル変数xとグローバル変数xは別物として扱われます。 エラーが発生します。 example_function内のローカル変数xは、グローバル変数xと同期されます。 None 20. 次のコードで「Hello」と「World」を改行で区切らずに出力するために必要な引数はどれですか? print("Hello")print("World") sep=" " end=" " sep="" end="" None 21. 次のコードを実行したときの出力結果は何でしょうか? y = 100def modify_variable():global yy = "Changed"modify_variable()print(y) "Changed" 100 エラー None None 22. イミュータブルなオブジェクトに該当するものはどれですか? リスト 辞書 タプル セット None 23. 次のコードを実行したときの出力結果は何でしょうか? x = 10def modify_variable():x = 20return xprint(modify_variable())print(x) 20 20 20 10 10 20 エラー None 24. ローカル変数に関する正しい説明はどれですか? ローカル変数は、関数内でのみアクセスできます。 ローカル変数は、すべての関数で共有されます。 ローカル変数は、グローバル変数と同じスコープを持ちます。 ローカル変数は、自動的にグローバル変数に変換されます。 None 25. 次のコードの実行結果を選んでください。 if True print("This will not work") This will not work IndentationError: expected an indented block エラーは発生しない SyntaxError: invalid syntax None 26. 次のコードを実行したときの出力結果は何でしょうか? def my_function():x = 5x += 10return xprint(my_function()) 5 10 15 エラー None 27. 次のコードの問題点として正しいものを選んでください。 while Trueprint("Looping") print文が正しくない 無限ループが発生する コロン(:)が欠けている 構文に問題はない None 28. Pythonで複数行にわたる文字列を作成するために使う方法として正しいものはどれですか? シングルクォートで囲む ダブルクォートで囲む 三重クォート(""")で囲む セミコロンで区切る None 29. 次のコードの出力結果を求めてください。 text = "Welcome"repeat_text = text * 3print(repeat_text) WelcomeWelcomeWelcome Welcome*3 Welcome 3 Error None 30. 次のコードの実行結果を選んでください。 if 5 > 3print("5 is greater than 3") SyntaxError: expected ':' 5 is greater than 3 IndentationError: expected an indented block エラーは発生しない None 31. 次のコードの出力として正しいものを選んでください。 try:raise Exception("Generic error")except Exception as e:print(type(e).__name__, ":", e) "Generic error" "TypeError : Generic error" Exceptionが発生してプログラムが停止する "Exception : Generic error" None 32. 次のコードの実行結果を選んでください。 def add(a, b)return a + b SyntaxError: expected ':' エラーは発生しない IndentationError: expected an indented block NameError: name 'a' is not defined None 33. 次のコードの結果を選択してください。 x = [1, 2, 3]def modify_list(lst):lst.append(4)modify_list(x)print(x) [1, 2, 3, 4] [1, 2, 3] エラー [] None 34. 次のコードの実行結果を選んでください。 for i in range(3):print(i) 0 1 2 エラーは発生しない SyntaxError: invalid syntax IndentationError: expected an indented block None 35. 次のコードを実行したときの出力結果は何でしょうか? def outer_function():x = 10def inner_function():x = x + 5return xreturn inner_function()print(outer_function()) 15 エラー 10 5 None 36. 次のコードを実行したときの出力結果は何でしょうか? z = [10, 20, 30]def modify_list():global zz[1] = 99modify_list()print(z) [10, 99, 30] [10, 20, 30] エラー [99, 99, 99] None 37. 文字列text = "Line1\nLine2\nLine3"に含まれる改行ごとに分割し、リストにする方法はどれですか? text.splitlines() text.split("\n") text.split() text.splitlines() と text.split("\n") None 38. 次のコードを実行したときの出力結果は何でしょうか? x = 5def modify_variable():global xx = x * 2return xprint(modify_variable())print(x) 10 10 5 10 10 5 エラー None 39. Pythonの文字列で改行を削除するための適切なメソッドはどれですか? join() strip() replace() split() None 40. 次のコードのエラー原因として正しいものを選んでください。 def calculate():total = total + 1print(total)calculate() グローバル変数totalが未定義のため。 totalがローカル変数として初期化されていないため。 関数calculateが定義されていないため。 エラーは発生しない。 None Time's up