Python 3エンジニア認定基礎試験~模擬試験③~ 2024年12月10日2024年12月10日 ailearn 1. 次のコードの実行結果を選んでください。 class MyClass:class_variable = "shared"MyClass.class_variable = "modified"print(MyClass.class_variable) エラーが発生する "shared" "modified" None None 2. divmod(10, 3)の出力結果はどれですか? (3, 0) (3, 3) (3, 2) (3, 1) None 3. 次のコードについて、正しい説明を選んでください。 class MyClass:class_variable = 10 クラス変数class_variableはインスタンスごとに異なる値を持つ クラス変数class_variableはインスタンスからアクセスできない クラス変数class_variableはインスタンス変数の値を上書きする クラス変数class_variableはすべてのインスタンスで共有される None 4. 文字列s = "abcdefghij"の最初の5文字を逆順で取得するスライスはどれですか? s[5::-1] s[:5:-1] s[4::-1] s[0:5:-1] None 5. 次のコードを実行した場合の出力は何ですか? s = "abcdefghij"print(s[2:9:3]) cfh cfi ceg ch None 6. 次のコードを実行したときの出力結果は何でしょうか? def calculate_area(length=5, width=3):return length * widthprint(calculate_area(width=4)) 20 15 12 エラー None 7. 文字列s = "abcdefghij"のインデックス1から8までを逆順で取得するために使用するスライスはどれですか? s[8:1:-1] s[8:0:-1] s[7:0:-1] s[-8:-1:-1] None 8. 次のうち、Pythonで整数を定義する正しい文はどれですか? num = "10" num = 10 num = int num = float(10) None 9. 次のコードの実行結果を選んでください。 try:print("Start")finally:print("Cleanup complete") Cleanup complete Start Cleanup complete Start エラーが発生する None 10. 次のコードを実行した場合、出力結果として正しいものを選んでください。 def show_info(name="John", age=25):print(f"{name} is {age} years old.")show_info(age=30) John is 30 years old. エラー 30 is 25 years old. John is 25 years old. None 11. 次のコードの実行結果を選んでください。 class MyClass:class_attribute = "shared"obj1 = MyClass()obj2 = MyClass()obj1.class_attribute = "modified"print(obj2.class_attribute) "modified" "shared" None エラーが発生する None 12. int("101", 2)の結果はどれですか? 2 10 101 5 None 13. 次のコードの実行結果を選んでください。 class MyClass:class_variable = 0@classmethoddef increment(cls):cls.class_variable += 1obj1 = MyClass()obj2 = MyClass()obj1.increment()obj2.increment()print(MyClass.class_variable) 2 1 0 エラーが発生する None 14. 次のコードで、finallyブロックが実行される理由として正しいものを選んでください。 try:print("Try block")except:print("Except block")finally:print("Finally block") tryブロックが正常に終了したため exceptブロックがスキップされたため tryブロックで例外が発生しなかったため 例外の発生有無にかかわらず必ず実行されるため None 15. 次のコードを実行した場合の出力は何ですか? s = "abcdefgh"print(s[1:6:2]) ace bdf bdfh abcdef None 16. デフォルト値として設定できるオブジェクトに該当しないものはどれですか? 数値 リスト 辞書 関数呼び出しの結果 None 17. 次のコードの実行結果を選んでください。 class MyClass:class_variable = 10obj1 = MyClass()obj2 = MyClass()obj1.class_variable = 20print(MyClass.class_variable) 10 20 エラーが発生する None None 18. bin(10)の出力はどれですか? 1010 0b10 0b1010 10 None 19. 次のコードを実行したときの出力結果は何でしょうか? def divide(a, b=1):return a / bprint(divide(10, b=2)) 10 2 エラー 5.0 None 20. 次のコードに関する正しい説明はどれですか? def append_item(item, items=[]):items.append(item)return items コードは正しく動作し、itemsは常に初期化される。 デフォルト値にリストを使用するのは安全である。 itemsが共有され、意図しない動作を引き起こす可能性がある。 エラーが発生する。 None 21. 次のコードを実行したときの出力結果は何でしょうか? def greet(name, message="Welcome", punctuation="!"):return f"{message}, {name}{punctuation}"print(greet("Alice", punctuation="!!!")) Welcome, Alice!!! Welcome, Alice! Welcome Alice!!! エラー None 22. int("20")の結果は次のどれですか? "20" 20 2 エラーが発生する None 23. 次のコードについて、正しい説明を選んでください。 class MyClass:class_attribute = "shared value" class_attributeはインスタンスごとに異なる値を持つ クラス定義において属性を直接定義することはできない class_attributeは全てのインスタンスで共有されるクラス属性 クラス内で定義された属性はインスタンスメソッドからアクセスできない None 24. 以下のコードの出力はどれですか? num = 1_000_000print(num) 1000000 1_000_000 エラーが発生する 1,000,000 None 25. 次のコードを実行した場合の出力は何ですか? s = "1234567890"print(s[-4:]) 67890 567890 7890 890 None 26. 次のコードについて、正しい説明を選んでください。 class MyClass:def instance_method(self):print("This is an instance method") instance_methodはクラス全体で共有されるクラスメソッド instance_methodは静的メソッド instance_methodはインスタンスを作成しなくても呼び出せる instance_methodはインスタンスごとに呼び出されるインスタンスメソッド None 27. 次のコードを実行したときの出力結果は何でしょうか? def introduce(name, age, city="Unknown"):return f"{name} is {age} years old and lives in {city}."print(introduce("Sam", 30)) エラー Sam is 30 years old and lives in None. Sam is 30 years old and lives in Unknown. Sam is 30 years old and lives in Default. None 28. hex(255)の出力はどれですか? 0xFF 255 0x100 0xA5 None 29. 次のコードを実行したときの出力結果は何でしょうか? def power(base, exponent=2):return base ** exponentprint(power(3, exponent=3)) 9 27 3 エラー None 30. 次のコードについて、staticmethodの役割として正しいものを選んでください。 class MyClass:@staticmethoddef my_static_method():print("This is a static method") クラス内のすべてのインスタンスで共有されるメソッド クラス属性を変更するメソッド インスタンス属性を初期化するメソッド クラスやインスタンスに依存しない処理を実行するメソッド None 31. 次のコードを実行した場合、結果は何になりますか? def add_numbers(a, b=10, c=5):return a + b + cprint(add_numbers(1, c=2)) 13 18 16 エラーが発生する。 None 32. Pythonで文字列s = "Hello World"の最初の5文字を取得するスライスはどれですか? s[0:5] s[:5] s[0:-5] s[0:5] と s[:5] None 33. 次のコードの実行結果を選んでください。 class MyClass:class_variable = "shared"obj = MyClass()obj.class_variable = "instance value"print(MyClass.class_variable) "instance value" "shared" エラーが発生する None None 34. 次のコードの実行結果を選んでください。 class MyClass:def greet(self):return "Hello!"obj = MyClass()print(obj.greet()) "Hello!" エラーが発生する 何も出力されない "None" None 35. 次のコードについて、クラス変数へのアクセス方法として正しいものを選んでください。 class MyClass:class_variable = "shared" MyClass.class_variable self.class_variable MyClass().class_variable MyClass[self.class_variable] None 36. 次のコードを実行したときの出力結果は何でしょうか? def customize_greeting(name="Guest", greeting="Welcome"):return f"{greeting}, {name}!"print(customize_greeting(greeting="Hello")) Hello, Guest! Welcome, Guest! Hello, Guest. エラー None 37. 次のコードの実行結果を選んでください。 class MyClass:class_variable = {"key": "value"}obj = MyClass()obj.class_variable["new_key"] = "new_value"print(MyClass.class_variable) {'key': 'value'} {'key': 'value', 'new_key': 'new_value'} エラーが発生する None None 38. 次のコードを実行したときの出力結果は何でしょうか? def format_message(message, prefix="Info: ", suffix="."):return f"{prefix}{message}{suffix}"print(format_message("System update completed", suffix="!")) Info: System update completed! System update completed! Info: System update completed. エラー None 39. 次のコードについて、obj1.class_variable = "changed"の動作として正しいものを選んでください。 class MyClass:class_variable = "shared"obj1 = MyClass()obj2 = MyClass()obj1.class_variable = "changed"print(obj2.class_variable) "changed" "shared" None エラーが発生する None 40. 次のコードの実行結果を選んでください。 class MyClass:class_variable = 5def change_variable(self):self.class_variable += 1obj1 = MyClass()obj2 = MyClass()obj1.change_variable()print(MyClass.class_variable) None エラーが発生する 6 5 None Time's up