Python 3エンジニア認定基礎試験-関数(引数のデフォルト値)-

1. 
引数にデフォルト値を設定する場合の正しい記述はどれですか?

2. 
次のコードを実行した場合、結果は何になりますか?

def add_numbers(a, b=10, c=5):
return a + b + c

print(add_numbers(1, c=2))

3. 
次のコードを実行した場合、結果は何になりますか?

def say_hello(name="Guest"):
return f"Hello, {name}!"

print(say_hello())
print(say_hello("Alice"))

4. 
次のコードを実行したときの出力結果は何でしょうか?

def customize_greeting(name="Guest", greeting="Welcome"):
return f"{greeting}, {name}!"

print(customize_greeting(greeting="Hello"))

5. 
次のコードを実行したときの出力結果は何でしょうか?

def sum_numbers(a=1, b=2, c=3):
return a + b + c

print(sum_numbers(c=6))

6. 
次のコードを実行したときの出力結果は何でしょうか?

def set_preferences(theme="Light", font_size=12, language="English"):
return f"Theme: {theme}, Font Size: {font_size}, Language: {language}"

print(set_preferences(theme="Dark", font_size=14))

7. 
次のコードを実行したときの出力結果は何でしょうか?

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))

8. 
次のコードに関する正しい説明はどれですか?

def greet(name="John", age):
return f"{name} is {age} years old."

9. 
デフォルト値を持つ引数の順序に関する正しい説明はどれですか?

10. 
次のコードを実行したときの出力結果は何でしょうか?

def calculate_total(price, tax=0.05, discount=0):
return price + (price * tax) - discount

print(calculate_total(100, discount=10))

11. 
次のコードを実行したときの出力結果は何でしょうか?

def format_message(message, prefix="Info: ", suffix="."):
return f"{prefix}{message}{suffix}"

print(format_message("System update completed", suffix="!"))

12. 
次のコードを実行したときの出力結果は何でしょうか?

def calculate_area(length=5, width=3):
return length * width

print(calculate_area(width=4))

13. 
次のコードを実行したときの出力結果は何でしょうか?

def greet(name, message="Hello"):
return f"{message}, {name}!"

print(greet("Alice", "Hi"))

14. 
デフォルト値として設定できるオブジェクトに該当しないものはどれですか?

15. 
次のコードを実行したときの出力結果は何でしょうか?

def describe_item(name, price=100, stock=10):
return f"Item: {name}, Price: {price}, Stock: {stock}"

print(describe_item("Notebook", stock=5))

16. 
次のコードを実行したときの出力結果は何でしょうか?

def divide(a, b=1):
return a / b

print(divide(10, b=2))

17. 
次のコードを実行したときの出力結果は何でしょうか?

def build_profile(name, age=18, country="Japan"):
return f"{name}, {age} years old, from {country}"

print(build_profile("Tom", country="USA"))

18. 
次のコードを実行したときの出力結果は何でしょうか?

def repeat_string(s, times=3, separator=" "):
return separator.join([s] * times)

print(repeat_string("Hi", times=2))

19. 
次のコードを実行したときの結果として正しいものを選んでください。

def multiply(a, b=2):
return a * b

print(multiply(5))

20. 
次のコードに関する正しい説明はどれですか?

def append_item(item, items=[]):
items.append(item)
return items

コメントを残すにはログインしてください。