Python 3エンジニア認定基礎試験~模擬試験③~ 2024年12月10日2024年12月10日 ailearn 1. 次のコードを実行したときの出力結果は何でしょうか? def calculate_area(length=5, width=3):return length * widthprint(calculate_area(width=4)) 15 12 20 エラー None 2. 次のコードを実行したときの出力結果は何でしょうか? def rectangle_area(length=10, width=2):return length * widthprint(rectangle_area(width=5)) 20 10 50 エラー None 3. 次のコードを実行したときの出力結果は何でしょうか? def repeat_string(s, times=3, separator=" "):return separator.join([s] * times)print(repeat_string("Hi", times=2)) Hi Hi Hi Hi Hi HiHi エラー None 4. 次のコードを実行した場合の出力は何ですか? s = "abcdefghij"print(s[3:-3]) defg def defgh d None 5. 次のコードを実行したときの出力結果は何でしょうか? def divide(a, b=1):return a / bprint(divide(10, b=2)) 5.0 10 2 エラー None 6. abs(-3.7)の結果はどれですか? -3.7 3 0 3.7 None 7. Pythonで文字列s = "Hello World"の最初の5文字を取得するスライスはどれですか? s[0:5] s[:5] s[0:-5] s[0:5] と s[:5] None 8. 次のコードを実行したときの出力結果は何でしょうか? def order_summary(item, quantity=1, price=100):total = quantity * pricereturn f"Order: {item}, Quantity: {quantity}, Total: {total}"print(order_summary("Notebook", quantity=2)) Order: Notebook, Quantity: 2, Total: 200 Order: Notebook, Quantity: 1, Total: 100 Order: Notebook, Quantity: 1, Total: 200 エラー None 9. 次のコードの実行結果を選んでください。 class MyClass:class_variable = 5obj1 = MyClass()obj1.class_variable += 1print(MyClass.class_variable) エラーが発生する None 5 6 None 10. デフォルト値を持つ引数の順序に関する正しい説明はどれですか? デフォルト値を持つ引数は任意の位置に配置できる。 デフォルト値を持たない引数の後に配置する必要がある。 デフォルト値を持つ引数は常に関数の最後に配置する必要がある。 デフォルト値を持つ引数はキーワード引数としてのみ使用可能である。 None 11. pow(4, 3, 5)の出力はどれですか? 4 3 2 1 None 12. 次のコードを実行したときの出力結果は何でしょうか? 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 13. 次のコードに関する正しい説明はどれですか? def greet(name="John", age):return f"{name} is {age} years old." 正常に動作する。 エラーが発生する。 nameに値を指定しない場合、ageが使用されない。 nameとageの順序を入れ替えれば動作する。 None 14. 次のコードを実行したときの出力結果は何でしょうか? def build_profile(first_name, last_name, age=25, country="Japan"):return f"{first_name} {last_name}, {age} years old, from {country}"print(build_profile("Tom", "Smith", country="USA")) Tom Smith, 25 years old, from Japan エラー Tom Smith, None years old, from USA Tom Smith, 25 years old, from USA None 15. 次のコードを実行したときの出力結果は何でしょうか? def customize_greeting(name="Guest", greeting="Welcome"):return f"{greeting}, {name}!"print(customize_greeting(greeting="Hello")) Hello, Guest! Welcome, Guest! Hello, Guest. エラー None 16. 次のコードを実行した場合、結果は何になりますか? def say_hello(name="Guest"):return f"Hello, {name}!"print(say_hello())print(say_hello("Alice")) Hello, Guest! Hello, Guest! Hello, Alice! Hello, Alice! Hello, Guest! Hello, Alice! エラー None 17. 次のコードを実行した場合の出力は何ですか? s = "123456789"print(s[::3]) 147 1245789 159 13579 None 18. 次のコードを実行したときの出力結果は何でしょうか? def sum_numbers(a=1, b=2, c=3):return a + b + cprint(sum_numbers(c=6)) 9 6 3 12 None 19. 引数にデフォルト値を設定する場合の正しい記述はどれですか? def func(a=10, b): def func(a, b=10): def func(a=10, b=20, c): def func(a, b): None 20. 次のコードを実行したときの出力結果は何でしょうか? def add_numbers(a, b, show_sum=False):result = a + bif show_sum:return f"The sum is {result}."return resultprint(add_numbers(7, 3, show_sum=True)) 10 7 3 The sum is 10. エラー None 21. divmod(10, 3)の出力結果はどれですか? (3, 0) (3, 3) (3, 2) (3, 1) None 22. bin(10)の出力はどれですか? 1010 0b10 0b1010 10 None 23. 次のコードを実行したときの出力結果は何でしょうか? def build_profile(name, age=18, country="Japan"):return f"{name}, {age} years old, from {country}"print(build_profile("Tom", country="USA")) Tom, 18 years old, from Japan エラー Tom, None years old, from USA Tom, 18 years old, from USA None 24. int("20")の結果は次のどれですか? "20" 20 2 エラーが発生する None 25. 次のコードの実行結果を選んでください。 class MyClass:class_variable = "shared"obj = MyClass()obj.class_variable = "instance value"print(MyClass.class_variable) "instance value" "shared" エラーが発生する None None 26. hex(255)の出力はどれですか? 0xFF 255 0x100 0xA5 None 27. 次のコードの実行結果を選んでください。 class MyClass:def __init__(self, name):self.name = nameobj = MyClass("Bob")obj.name = "Alice"print(obj.name) None エラーが発生する "Bob" "Alice" None 28. 次のコードの実行結果を選んでください。 class MyClass:def __init__(self, value):self.value = valuedef __eq__(self, other):return self.value == other.valueobj1 = MyClass(10)obj2 = MyClass(10)print(obj1 == obj2) False エラーが発生する True 何も出力されない None 29. 次のコードについて、正しい説明を選んでください。 class MyClass:def __init__(self, name):self.name = name 特殊メソッド__init__はインスタンス作成時に必ずエラーを発生させる コンストラクタでname属性を初期化するクラス クラス定義におけるselfは省略可能 __init__メソッドは手動で呼び出す必要がある None 30. 次のコードの実行結果を選んでください。 class MyClass:class_variable = 0def increment(self):MyClass.class_variable += 1obj1 = MyClass()obj2 = MyClass()obj1.increment()obj2.increment()print(MyClass.class_variable) 1 2 0 エラーが発生する None 31. 次のコードについて、staticmethodの役割として正しいものを選んでください。 class MyClass:@staticmethoddef my_static_method():print("This is a static method") クラス内のすべてのインスタンスで共有されるメソッド クラス属性を変更するメソッド インスタンス属性を初期化するメソッド クラスやインスタンスに依存しない処理を実行するメソッド None 32. 次のコードを実行したときの出力結果は何でしょうか? def generate_report(title="Monthly Report", include_summary=True):summary_text = "Summary included." if include_summary else "No summary."return f"Title: {title}, {summary_text}"print(generate_report(include_summary=False)) エラー Title: Monthly Report, Summary included. Title: Monthly Report, No summary. Title: None, No summary. None 33. 次のコードの実行結果を選んでください。 class MyClass:def __init__(self, name):self.name = nameobj = MyClass("Alice")print(obj.name) "Alice" エラーが発生する None 何も出力されない None 34. 次のコードで、finallyブロックの実行タイミングとして正しいものを選んでください。 try:raise ValueError("An error occurred")except ValueError:print("Caught ValueError")finally:print("Executing cleanup") 例外が発生する前 finallyブロックは実行されない 例外がキャッチされた後 例外がキャッチされる前 None 35. 次のコードを実行した場合の出力は何ですか? s = "abcdefghij"print(s[2:9:3]) cfh cfi ceg ch None 36. 次のコードの実行結果を選んでください。 class MyClass:@staticmethoddef greet():return "Hello from static method"print(MyClass.greet()) 何も出力されない "None" エラーが発生する "Hello from static method" None 37. Pythonで文字列s = "Python Programming"の先頭から10文字を取得するスライスはどれですか? s[0:9] s[10:] s[:10] s[1:10] None 38. 次のコードを実行した場合の出力は何ですか? s = "Python"print(s[:3] + s[-3:]) Pynon Pyon Pyton Python None 39. complex(3, -4).imagの結果はどれですか? 3 4 0 -4 None 40. 次のコードを実行した場合、出力結果として正しいものを選んでください。 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 Time's up