What is the output of this Python code?
def greet(**kwargs): name = kwargs.get('name', 'Guest') greeting = kwargs.get('greeting', 'Hello') return f"{greeting}, {name}!" print(greet(name='Alice'))
Look at how kwargs.get() provides default values if the key is missing.
The function uses kwargs.get('name', 'Guest') to get the 'name' key or default to 'Guest'. Since 'name' is provided as 'Alice', it returns 'Hello, Alice!'.
What will be printed by this code?
def count_keys(**kwargs): return len(kwargs) print(count_keys(a=1, b=2, c=3))
Count how many keyword arguments are passed to the function.
The function returns the number of keys in kwargs. Since three keyword arguments are passed, it returns 3.
What error does this code raise when run?
def show_info(**kwargs): print(kwargs['age']) info = {'name': 'Bob', 'age': 25} show_info(info)
Check how the dictionary is passed to the function expecting keyword arguments.
The function expects keyword arguments, but info is passed as a single positional argument, causing a TypeError.
Given this function, which call will print all keyword arguments as expected?
def print_all(**kwargs): for key, value in kwargs.items(): print(f"{key}: {value}")
Remember how to pass keyword arguments to a function expecting **kwargs.
Only option D passes keyword arguments correctly. Option D passes a dict as a positional argument, B passes a list as a positional argument, and C tries to unpack a dict as positional args.
Consider this code snippet. What will be the output?
def func(a, **kwargs): print(a) print(kwargs) func(1, a=2, b=3)
Think about how Python handles duplicate argument names in function calls.
Python raises a TypeError because the argument 'a' is given both positionally and as a keyword argument.