Python 3エンジニア認定基礎試験~模擬試験②~ 2024年12月10日2024年12月10日 ailearn 1. 次のコードの実行結果として正しいものを選んでください。 x = 5def example_function():x = 10print(x)example_function()print(x) 10 5 10 10 5 10 エラー None 2. 次のコードを実行したときの出力結果は何でしょうか? 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 3. 次のコードの出力結果を求めてください。 words = ["data", "science", "python"]sentence = " ".join(words)print(sentence) data science python datasciencepython data-science-python ['data', 'science', 'python'] None 4. 次のコードの実行結果を選んでください。 try:raise TypeError("Invalid type")except Exception as e:print("Caught Exception:", e) 何も出力されない エラーが発生してプログラムが停止する "Caught TypeError: Invalid type" "Caught Exception: Invalid type" None 5. 次のコードの実行結果を選んでください。 for i in range(3)print(i) 0 1 2 エラーは発生しない SyntaxError: expected ':' IndentationError: expected an indented block None 6. 次のコードの出力を1行にするために使うべき引数はどれですか? print("Hello")print("Python") end=" " sep=" " end="\n" sep="\n" None 7. 次のコードを実行したときの出力結果は何でしょうか? x = 10def my_function():x = 20return xprint(my_function())print(x) 20 20 20 10 10 20 エラー None 8. 次のコードを実行したときの出力結果は何でしょうか? x = 0def increment():global xx += 1return xprint(increment())print(increment()) 1 1 1 2 2 2 エラー None 9. 次のコードを実行した場合の出力は何ですか? items = ["apple", "banana", "cherry"]output = " - ".join(items)print(output) apple, banana, cherry apple - banana - cherry apple banana cherry ['apple', 'banana', 'cherry'] None 10. 次のコードを実行した場合の結果として正しいものを選んでください。 try:raise ValueError("Invalid value")except ValueError as e:print("Caught exception:", e) ValueError: Invalid value "Caught exception: Invalid value" エラーが発生する 何も出力されない None 11. 次のコードを実行したときの出力結果は何でしょうか? y = 100def modify_variable():global yy = "Changed"modify_variable()print(y) "Changed" 100 エラー None None 12. "Python3"という文字列から「3」を削除して"Python"にするコードはどれですか? remove("3") "Python3".replace("3", "") delete("3") strip("3") None 13. 次のコードの実行結果を選んでください。 print("Missing parenthesis in print statement" SyntaxError: invalid syntax SyntaxError: unexpected EOF while parsing エラーは発生しない "Missing parenthesis in print statement" None 14. 次のコードの問題点として正しいものを選んでください。 if 5 > 3:print("5 is greater than 3") コロン(:)が欠けている print文が間違っている 構文に問題はない インデントが正しくない None 15. 次のコードの問題点として正しいものを選んでください。 if x = 10:print("x is 10") コロン(:)が間違っている 代入文(=)ではなく比較演算子(==)を使用する必要がある xが未定義 構文に問題はない None 16. 次のコードを実行したときの出力結果は何でしょうか? x = 10def modify_variable():global xx = "Hello"modify_variable()print(x) 10 "Hello" エラー None None 17. 次のコードの出力結果を求めてください。 text = "Python"result = text + " " + text[::-1]print(result) Python Python Python nohtyP Python PythonPython Python None 18. 次のコードで「Hello」と「World」を改行で区切らずに出力するために必要な引数はどれですか? print("Hello")print("World") sep=" " end=" " sep="" end="" None 19. 次のコードの問題点として正しいものを選んでください。 if Trueprint("This is true") コロン(:)が欠けている インデントが不正である printの括弧が間違っている 構文に問題はない None 20. Pythonで文字列を結合するために使用される演算子はどれですか? + * % / None 21. 次のコードを実行したときの出力結果は何でしょうか? 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 22. 次のコードで発生する例外の種類として正しいものを選んでください。 raise KeyError("Key not found") ValueError TypeError IndexError KeyError None 23. 次のコードの実行結果を選んでください。 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 24. 次のコードを実行したときの出力結果は何でしょうか? x = 5def outer_function():global xx = x * 2outer_function()print(x) 5 10 エラー None None 25. 次のコードを実行したときの出力結果は何でしょうか? 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 26. 次のコードを実行したときの出力結果は何でしょうか? x = 5def outer_function():x = 10def inner_function():return xreturn inner_function()print(outer_function()) 10 5 15 エラー 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. 次のコードの実行結果として正しいものを選んでください。 print("Hello "Hello" SyntaxError: EOL while scanning string literal SyntaxError: invalid syntax 空文字列が出力される None 29. 次のコードの実行結果として正しいものを選んでください。 def my_function():y = 7print(y)my_function()print(y) 7 7 7 エラー エラー 7 エラー エラー None 30. 次のコードを実行したときの出力結果は何でしょうか? def my_function():x = 5x += 10return xprint(my_function()) 5 10 15 エラー None 31. 次のコードを実行したときの出力結果は何でしょうか? def outer_function():x = 10def inner_function():nonlocal xx = x + 5return xreturn inner_function()print(outer_function()) 10 5 エラー 15 None 32. 次のうち、文字列"Hello"を5回繰り返した文字列を生成するためのコードはどれですか? "Hello".repeat(5) "Hello".multiply(5) "Hello" * 5 "Hello".join(5) None 33. 次のコードを実行したときの出力結果は何でしょうか? 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 34. 次のコードを実行したときの出力結果は何でしょうか? def reset_list(values):values = [0, 0, 0]numbers = [1, 2, 3]reset_list(numbers)print(numbers) [0, 0, 0] [1, 2, 3] [] エラー None 35. 次のリスト["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 36. 文字列text = "This is a test"から改行を挿入して「This is\na test」にするために必要なコードはどれですか? text.replace(" ", "\n", 1) text.replace(" ", "\n") text.insert(" ", "\n") text.split(" ", 1) None 37. 次のコードを実行したときの出力結果は何でしょうか? x = 10def shadow_variable():x = 5def inner():return xreturn inner()print(shadow_variable())print(x) 5 10 10 5 エラー 5 5 None 38. グローバル変数に関する正しい説明はどれですか? グローバル変数は関数内でのみ使用できます。 グローバル変数はプログラム全体でアクセス可能です。 グローバル変数はglobalキーワードを使用しないと定義できません。 グローバル変数は関数内で自動的にローカル変数に変換されます。 None 39. 次のコードの実行結果を選んでください。 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 40. 次のコードを実行したときの出力結果は何でしょうか? 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 Time's up