Python 3エンジニア認定基礎試験~模擬試験②~ 2024年12月10日2024年12月10日 ailearn 1. 次のうち、文字列の結合に使われないメソッドはどれですか? join() format() concat() f-string None 2. 文字列フォーマットのf-string(フォーマット文字列)の記述方法として正しいものはどれですか? "Hello, {name}" "Hello, f{name}" format("Hello, {name}") f"Hello, {name}" None 3. 次のコードを実行したときの出力結果は何でしょうか? x = 10def test_function():x = 20def modify_variable():global xx += 10modify_variable()print(x)test_function()print(x) 20 10 30 30 20 30 20 20 None 4. Pythonの文字列で改行を削除するための適切なメソッドはどれですか? join() strip() replace() split() None 5. 次のコードを実行した場合の出力は何ですか? a = "Python"b = "Programming"result = a + " " + bprint(result) PythonProgramming Python Programming Python-Programming Python + Programming None 6. 次のうち、文字列"Hello"を5回繰り返した文字列を生成するためのコードはどれですか? "Hello".repeat(5) "Hello".multiply(5) "Hello" * 5 "Hello".join(5) None 7. 次のコードの出力として正しいものを選んでください。 try:raise Exception("Generic error")except Exception as e:print(type(e).__name__, ":", e) "Generic error" "TypeError : Generic error" Exceptionが発生してプログラムが停止する "Exception : Generic error" None 8. 文字列の中に改行を含めるにはどの方法を使うべきですか? スペースを追加する バックスラッシュと「n」を使用する ドット(.)を追加する バックスラッシュと「t」を使用する None 9. 次のコードの実行結果を選んでください。 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 10. 次のコードの実行結果を選んでください。 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 11. ローカル変数に関する正しい説明はどれですか? ローカル変数は、関数内でのみアクセスできます。 ローカル変数は、すべての関数で共有されます。 ローカル変数は、グローバル変数と同じスコープを持ちます。 ローカル変数は、自動的にグローバル変数に変換されます。 None 12. 次のコードを実行したときの出力結果は何でしょうか? x = 10def shadow_variable():x = 5def inner():return xreturn inner()print(shadow_variable())print(x) 5 10 10 5 エラー 5 5 None 13. 次のコードを実行したときの出力結果は何でしょうか? x = 10def modify_variable():global xx = x + 5return xprint(modify_variable())print(x) 15 15 エラー 10 15 15 10 None 14. 次のコードを実行したときの出力結果は何でしょうか? x = 10def modify_variable():global xx = "Hello"modify_variable()print(x) 10 "Hello" エラー None None 15. 次のコードの実行結果として正しいものを選んでください。 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 16. 次のコードで発生する例外の種類として正しいものを選んでください。 raise KeyError("Key not found") ValueError TypeError IndexError KeyError None 17. 次のリスト["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 18. 次のコードを実行したときの出力結果は何でしょうか? x = [1, 2, 3]def modify_list():global xx.append(4)modify_list()print(x) [4] エラー [1, 2, 3] [1, 2, 3, 4] None 19. 変数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 20. 次のコードの問題点として正しいものを選んでください。 if 5 > 3:print("5 is greater than 3") コロン(:)が欠けている print文が間違っている 構文に問題はない インデントが正しくない None 21. 次のコードを実行したときの出力結果として正しいものを選んでください。 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 22. 次のコードを実行したときの出力結果は何でしょうか? x = 1def first_function():global xx += 1def second_function():global xx *= 3first_function()second_function()print(x) 2 3 6 9 None 23. 次のコードの実行結果として正しいものを選んでください。 x = 5def example_function():x = 10print(x)example_function()print(x) 10 5 10 10 5 10 エラー None 24. 次のコードの実行結果として正しいものを選んでください。 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 25. raise文の役割として正しいものを選んでください。 明示的に例外を発生させる 例外をキャッチする 例外を無視する 例外を記録する None 26. Pythonのsplitlines()メソッドはどのような機能を持っていますか? 文字列を空白ごとに分割する 文字列を改行ごとに分割する 文字列の改行を削除する 文字列の末尾に改行を追加する None 27. リスト["Python", "Java", "C++"]を結合して"Python and Java and C++"とするためのコードはどれですか? " and ".join(["Python", "Java", "C++"]) " and ".join("Python", "Java", "C++") " and ".concat(["Python", "Java", "C++"]) "Python", "Java", "C++".join(" and ") None 28. 次のうち、Pythonの文字列において改行を削除する方法として正しいものはどれですか? replace("\n", "") strip() split() join() None 29. 次のコードを実行した場合の出力は何ですか? text = """Line1Line2Line3"""print(text.splitlines()[0]) Line1 Line2 Line3 Line1 Line2 Line3 None 30. 次のコードを実行したときの出力結果は何でしょうか? 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 31. 次のコードを実行したときの出力結果は何でしょうか? x = 0def increment():global xx += 1return xprint(increment())print(increment()) 1 1 1 2 2 2 エラー None 32. "Python3"という文字列から「3」を削除して"Python"にするコードはどれですか? remove("3") "Python3".replace("3", "") delete("3") strip("3") None 33. Pythonで「Hello」と「World」を改行付きで結合し、「Hello\nWorld」と表示させるコードはどれですか? "Hello" + "World" "Hello\nWorld" "Hello" + "\n" + "World" "Hello\nWorld" と "Hello" + "\n" + "World" None 34. 次のコードを実行したときの出力結果は何でしょうか? x = 5def modify_variable():global xx = x * 2return xprint(modify_variable())print(x) 10 10 5 10 10 5 エラー None 35. Pythonの文字列で改行を表すエスケープシーケンスはどれですか? \r \n \t \\ None 36. 変数greeting = "Hello"とname = "Alice"を使って、「Hello, Alice!」と出力するためのコードはどれですか? print(f"{greeting}, {name}!") print("{}! {}".format(greeting, name)) print(greeting, ",", name, "!") print(f"{greeting}, {name}!"), "Hello" + name None 37. 次のコードを実行したときの出力結果は何でしょうか? def my_function():x = 5x += 10return xprint(my_function()) 5 10 15 エラー None 38. 次のコードの実行結果を選んでください。 print("Missing parenthesis in print statement" SyntaxError: invalid syntax SyntaxError: unexpected EOF while parsing エラーは発生しない "Missing parenthesis in print statement" None 39. 文字列text = "This is a test"から改行を挿入して「This is\na test」にするために必要なコードはどれですか? text.replace(" ", "\n", 1) text.replace(" ", "\n") text.insert(" ", "\n") text.split(" ", 1) None 40. グローバル変数を関数内で変更するために使用するキーワードは何ですか? local nonlocal global static None Time's up