Python 3エンジニア認定基礎試験~模擬試験②~ 2024年12月10日2024年12月10日 ailearn 1. 次のコードの実行結果を選んでください。 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 2. 文字列"Hello World"のスペースを削除して"HelloWorld"とするために使用するメソッドはどれですか? remove(" ") replace(" ", "") delete(" ") join(" ") None 3. 次のコードの出力結果を求めてください。 text = "Hello"result = text + " " * 3 + "World"print(result) Hello World HelloWorld Hello Hello World None 4. 次のコードの実行結果を選んでください。 while True print("Infinite loop") SyntaxError: expected ':' "Infinite loop"が無限に出力される IndentationError: expected an indented block エラーは発生しない None 5. 次のコードの実行結果として正しいものを選んでください。 def check_positive(x):if x < 0:raise ValueError("Negative values are not allowed")return xtry:print(check_positive(-10))except ValueError as e:print(e) -10 ValueError: Negative values are not allowed "Negative values are not allowed" エラーが発生する None 6. 次のコードを実行したときの出力結果は何でしょうか? def my_function():x = 5x += 10return xprint(my_function()) 5 10 15 エラー None 7. 次のコードで「Hello」と「World」を改行で区切らずに出力するために必要な引数はどれですか? print("Hello")print("World") sep=" " end=" " sep="" end="" None 8. 次のコードの実行結果を選んでください。 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 9. 次のコードを実行したときの出力結果として正しいものを選んでください。 x = 5def outer_function():x = 10def inner_function():global xx += 5inner_function()print(x)outer_function()print(x) 10 15 15 15 10 10 エラー None 10. 次のコードを実行したときの出力結果は何でしょうか? 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 11. 次のコードを実行したときの出力結果は何でしょうか? x = 5def outer_function():global xx = x * 2outer_function()print(x) 5 10 エラー None None 12. Pythonで複数行にわたる文字列を作成するために使う方法として正しいものはどれですか? シングルクォートで囲む ダブルクォートで囲む 三重クォート(""")で囲む セミコロンで区切る None 13. 次のコードを実行したときの出力結果は何でしょうか? x = 1def outer_function():def inner_function():global xx += 1inner_function()outer_function()print(x) 1 2 3 エラー None 14. 次のコードの問題点として正しいものを選んでください。 for i in range(5)print(i) print文が正しくない range関数が間違っている コロン(:)が欠けている 構文に問題はない None 15. 次のコードを実行したときの出力結果は何でしょうか? x = 10def my_function():x = 20return xprint(my_function())print(x) 20 20 20 10 10 20 エラー None 16. 次のコードを実行した場合の出力は何ですか? text = "Line1\nLine2\nLine3"result = text.split("\n")print(result[1]) Line1 Line2 Line3 ['Line1', 'Line2', 'Line3'] None 17. 次のコードの実行結果を選んでください。 if True:print("This is true") "This is true" エラーは発生しない SyntaxError: invalid syntax IndentationError: expected an indented block None 18. 次のコードの出力を1行にするために使うべき引数はどれですか? print("Hello")print("Python") end=" " sep=" " end="\n" sep="\n" None 19. 次のコードを実行したときの出力結果は何でしょうか? def modify_string(text):text += " World"return textoriginal_text = "Hello"result = modify_string(original_text)print(result)print(original_text) Hello Hello Hello Hello Hello Hello World Hello World Hello World Hello None 20. Pythonの文字列で改行を表すエスケープシーケンスはどれですか? \r \n \t \\ None 21. 次のコードの実行結果を選んでください。 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 22. 次のコードの実行結果を選んでください。 for i in range(3):print(i) 0 1 2 エラーは発生しない IndentationError: expected an indented block SyntaxError: invalid syntax None 23. 次のコードの実行結果として正しいものを選んでください。 x = 5def example_function():x = 10print(x)example_function()print(x) 10 5 10 10 5 10 エラー None 24. 次のコードの実行結果として正しいものを選んでください。 print("Hello "Hello" SyntaxError: EOL while scanning string literal SyntaxError: invalid syntax 空文字列が出力される None 25. 次のコードを実行したときの出力結果は何でしょうか? def counter():count = 0def increment():nonlocal countcount += 1return countreturn incrementincrementer = counter()print(incrementer())print(incrementer()) 1 1 1 2 2 2 エラー None 26. 次のコードの問題点として正しいものを選んでください。 while Trueprint("Looping") print文が正しくない 無限ループが発生する コロン(:)が欠けている 構文に問題はない None 27. 次のコードを実行したときの出力結果は何でしょうか? 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 28. 次のコードを実行したときの出力結果は何でしょうか? def count_calls():count = 0def increment():nonlocal countcount += 1return countreturn incrementcounter = count_calls()print(counter())print(counter()) 1 2 0 1 1 1 エラー None 29. 次のコードの実行結果を選んでください。 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 30. 改行を含む文字列"Hello\nWorld"をスペースで区切って一行にまとめるためのコードはどれですか? "Hello\nWorld".split(" ") "Hello\nWorld".replace("\n", " ") "Hello\nWorld".join(" ") "Hello\nWorld".strip() None 31. グローバル変数を使用する際の注意点として適切なものはどれですか? グローバル変数はなるべく使用を避けるべきである。 グローバル変数はどの関数でも自由に変更してよい。 グローバル変数の値は自動的に保存される。 グローバル変数はglobalキーワードを使うとローカル変数になる。 None 32. 次のコードの実行結果を選んでください。 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 33. 次のコードの実行結果を選んでください。 if 5 > 3print("5 is greater than 3") SyntaxError: expected ':' 5 is greater than 3 IndentationError: expected an indented block エラーは発生しない 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. 次のコードを実行したときの出力結果は何でしょうか? def test_function():y = 5y += 1return yprint(test_function())print(y) 5 5 6 5 エラー エラー 6 エラー None 36. 次のコードの実行結果を選んでください。 for i in range(3):print(i) 0 1 2 エラーは発生しない SyntaxError: invalid syntax IndentationError: expected an indented block None 37. 次のコードの出力結果を求めてください。 text = "Line1\nLine2\nLine3"print(text.replace("\n", " | ")) Line1 Line2 Line3 Line1\nLine2\nLine3 Line1 | Line2 | Line3 Line1 - Line2 - Line3 None 38. グローバル変数に関する正しい説明はどれですか? グローバル変数は関数内でのみ使用できます。 グローバル変数はプログラム全体でアクセス可能です。 グローバル変数はglobalキーワードを使用しないと定義できません。 グローバル変数は関数内で自動的にローカル変数に変換されます。 None 39. 次のコードの実行結果を選んでください。 if True print("This will not work") This will not work IndentationError: expected an indented block エラーは発生しない SyntaxError: invalid syntax None 40. 次のコードの実行結果を選んでください。 try:raise ValueError("Test error")except Exception as e:print("Caught an exception")raise e "Caught an exception"が出力され、プログラムが正常終了する エラーが発生してプログラムが停止する "Caught an exception"が出力され、例外が再発生する 何も出力されない None Time's up