Python 3エンジニア認定基礎試験~模擬試験~ 2024年11月20日 ailearn 1. 次のコードの実行結果を選んでください。 # パッケージ構造:# package/# ├── __init__.py# ├── module.py# module.pyvalue = 42# main.pyfrom package.module import valuevalue = value + 1print(value) 43 42 Error: value not found エラーが発生する None 2. 次のコードの実行結果を選んでください。 # sample.txt# ---# Python Programming# File Handling# ---with open('sample.txt', 'r') as f:print(f.read(15))print(f.read()) Python Program ming File Handling Python Program File Handling Python Programming File Handling エラーが発生する None 3. 次のコードを実行した後のqueueの内容は何でしょうか? queue = [1, 2, 3]queue.append(4)queue.pop(0) [1, 2, 3] [2, 3, 4] [1, 3, 4] [3, 4] None 4. 次のコードの実行結果を選んでください。 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 5. 次のコードの出力結果は何でしょうか? queue = [5, 10, 15]queue.pop(0)queue.append(20)queue.pop(0)queue.append(25) [10, 20, 25] [15, 20, 25] [5, 20, 25] [10, 15, 25] None 6. 次のコードについて、クラス変数へのアクセス方法として正しいものを選んでください。 class MyClass:class_variable = "shared" MyClass.class_variable self.class_variable MyClass().class_variable MyClass[self.class_variable] None 7. 次のコードを実行した場合、出力は何ですか? lst = [1, 2, 3]print(len(lst)) 1 2 3 エラー None 8. 次のコードを実行した場合、出力結果として正しいものを選んでください。 def calculate_total(price, tax=0.1, discount=0):return price + (price * tax) - discountprint(calculate_total(100, discount=10)) 100.0 110.0 90.0 エラー None 9. 次のコードを実行したときの出力結果は何でしょうか? score = 85result = "優秀" if score >= 90 else "合格" if score >= 60 else "不合格"print(result) 優秀 不合格 エラーが発生します 合格 None 10. 次のコードを実行したときの出力結果は何でしょうか? num = 16result = "4の倍数" if num % 4 == 0 else "4の倍数ではありません"print(result) 4の倍数ではありません エラーが発生します 出力されない 4の倍数 None 11. リストdataがNoneでなく、かつ空でない場合にTrueを、Noneであるか空である場合にFalseを出力するコードはどれでしょうか? print(data is not None or data) print(data is not None and data) print(not data) print(data) None 12. 次のコードにおいて、クラス変数として正しい記述を選んでください。 python コードをコピーする class MyClass:???def __init__(self, value):self.value = value class_variable = "shared" self.class_variable = "shared" def class_variable(): pass None None 13. 次のコードの実行結果として正しいものを選んでください。 try:open("nonexistent_file.txt", "r")except FileNotFoundError:print("File not found")finally:print("Execution complete") File not found Execution complete File not found Execution complete エラーが発生する None 14. 次のコードの実行結果を選んでください。 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 15. 次のコードの実行結果を選んでください。 import jsondata = {"numbers": [10, 20, 30, 40]}json_data = json.dumps(data, separators=(",", ":"))print(json_data) {"numbers": [10, 20, 30, 40]} {"numbers":[10,20,30,40]} {"numbers" : [10, 20, 30, 40]} エラーが発生する None 16. 次のコードの実行結果を選んでください。 # ファイル名: sample.txt# 初期内容:# ---# Line 1# Line 2# ---with open('sample.txt', 'r+') as f:f.seek(5)f.write("Modified")f.seek(0)print(f.read()) Line 1Modified Line 1 Modified Line Modified エラーが発生する None 17. 次のコードの実行結果を選んでください。 # ファイル名: sample.txt# 初期内容:# ---# Line1# Line2# ---with open('sample.txt', 'w') as f:f.writelines(["New1\n", "New2\n", "New3\n"]) New1 New2 New3 Line1 Line2 New1 New2 New3 ファイルの内容が空になる エラーが発生する None 18. 次のコードを実行したときの出力結果は何でしょうか? 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 19. 変数xがNoneである場合にTrueを、そうでない場合にFalseを出力するコードはどれでしょうか? print(x is None) print(x == 0) print(x != None) print(x) None 20. 次のコードの実行結果を選んでください。 # sample.txt は存在しないtry:with open('sample.txt', 'r') as f:print(f.read())except FileNotFoundError:print("File not found") "File not found" 空文字列 エラーが発生する ファイルが作成される None 21. 変数xが正の数であるか、または偶数である場合に「条件を満たします」と表示するコードはどれでしょうか? if x > 0 and x % 2 == 0: print("条件を満たします") if x < 0 or x % 2 == 0: print("条件を満たします") if x > 0 or x % 2 == 0: print("条件を満たします") if x > 0 and x % 2 != 0: print("条件を満たします") None 22. 次のコードの実行結果を選んでください。 class A:passclass B(A):passclass C(A):passobj = B()print(isinstance(obj, C)) True False エラーが発生する None None 23. 次のコードの実行結果を選んでください。 class MyClass:def __init__(self, name):self.name = namedef greet(self):return f"Hello, {self.name}!"obj1 = MyClass("Alice")obj2 = MyClass("Bob")print(obj1.greet())print(obj2.greet()) Hello, Alice! Hello, Bob! Hello, Alice! Hello, Alice! エラーが発生する 何も出力されない None 24. 次のコードを実行したときの出力結果は何でしょうか? def multiply_values(x, y=2, z=3):return x * y * zprint(multiply_values(4))print(multiply_values(4, 5))print(multiply_values(4, 5, 6)) 24 60 120 12 24 36 24 12 10 24 40 48 None 25. 次のコードを実行したときの出力結果は何でしょうか? reverse_concat = lambda s1, s2: s2 + s1print(reverse_concat("Hello", "World")) HelloWorld WorldHello Hello World エラー None 26. 関数greetに名前を渡し、「Hello, 名前!」と出力する関数を正しく定義する方法はどれでしょうか? def greet(name): return "Hello, " + name + "!" def greet(name): print("Hello, name!") def greet(name): print("Hello, " + name + "!") greet(name): print("Hello, " + name + "!") None 27. 次のコードを実行したときの出力結果は何でしょうか? score = 75if score >= 90:print("A")elif score >= 80:print("B")elif score >= 70:print("C")else:print("D") A B C D None 28. 次のコードで、数値を2倍にして返す関数doubleを正しく定義する方法はどれでしょうか? def double(x): return x * 2 def double(x): print(x * 2) double(x): return x * 2 func double(x): return x * 2 None 29. 次のコードを実行したときの出力結果として正しいものを選んでください。 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 30. 次のコードで、with文を使用した場合に発生しない問題を選んでください。 f = open('sample.txt', 'w')f.write("Test content")# プログラムがここで終了した場合 ファイルが閉じられない ファイル内容が不完全になる ファイルが破損する ファイルが正しく閉じられる None 31. 次のコードの実行結果について正しいものを選んでください。 with open('sample.txt', 'w') as f:f.write("First Line\n")raise Exception("An error occurred") ファイルが閉じられ、内容は空のままになる ファイルが閉じられ、内容は"First Line\n"になる ファイルが閉じられず、エラーが発生する ファイルが閉じられず、内容は破損する None 32. 次のコードの実行結果を選んでください。 # ファイル名: sample.txt# 初期内容:# ---# Line1# Line2# ---with open('sample.txt', 'r+') as f:f.write("New")f.seek(6)f.write("Content") New1 Content New Line2 New Content エラーが発生する None 33. 次のコードを実行したときの出力結果は何でしょうか? x = 5def modify_variable():global xx += 10return xprint(modify_variable())print(x) 15 15 5 5 エラー 15 5 None 34. 次のコードを実行したときの出力結果は何でしょうか? x = 10def modify_variable():global xx = "Hello"modify_variable()print(x) 10 "Hello" エラー None None 35. 次のコードの実行結果を選んでください。 # パッケージ構造:# package/# ├── __init__.py# ├── subpackage/# │ ├── __init__.py# │ ├── module2.py# │ ├── module3.py# module2.pyfrom . import module3 as modprint(mod.__file__) module3.pyのファイルパス module3 Error: mod not found エラーが発生する None 36. 次のコードについて、selfを使う理由として適切な説明を選んでください。 class MyClass:def set_name(self, name):self.name = name クラス全体で共有するname属性を保持するため selfは常に固定値で、全インスタンスで共通であるため メソッドに引数がない場合でもselfは省略可能なため インスタンスごとに異なるname属性を保持するため None 37. Pythonの標準ライブラリosモジュールで、現在の作業ディレクトリを変更する関数として正しいものを選んでください。 os.chdir() os.getcwd() os.setdir() os.change_dir() None 38. 次のコードを実行したときの出力結果は何でしょうか? x, y = 3, 4result = "奇数" if x % 2 != 0 and y % 2 != 0 else "どちらかが偶数"print(result) 奇数 どちらかが偶数 エラーが発生します 出力されない None 39. 次のコードの実行結果を選んでください。 import mathprint(math.log(100, 10)) 10.0 2.0 エラー None None 40. 次のコードを実行した場合の出力として正しいものを選んでください。 value = 255formatted = "{:#o}".format(value)print(formatted) 0o377 377 0xFF エラーが発生する None Time's up