All sample questions / Python

What does print(hello('Sam')) print? Python Iterators, Generators and Decorators

Block 6, Iterators, Generators and Decorators, question 3 of the Python sample

from functools import partial

def greet(greeting, name):
    return f'{greeting}, {name}'

hello = partial(greet, 'Hello')
print(hello('Sam'))

What does this print?

  1. A. Sam, Hello
  2. B. Hello, Hello
  3. C. greeting, Sam
  4. D. Hello, Sam
Show the answer and why each option is right or wrong

Answer, D. Hello, Sam

A. Sam, Hello
Swaps the order of the fixed argument and the one supplied at call time.
B. Hello, Hello
Assumes the argument supplied at call time is ignored and the fixed value fills both slots.
C. greeting, Sam
Assumes the parameter name itself, rather than its fixed value, appears in the output.
D. Hello, Sam
Correct. partial fixes the first positional argument to 'Hello', so the argument supplied at call time fills the remaining name slot.

partial fixes the leading positional argument to Hello, so any further positional argument passed to hello fills the next open slot, which is name. The order of the fixed and remaining arguments is preserved exactly as declared in the original function signature.

The answer above is what the CPython interpreter printed when the snippet was run, not what someone expected it to do.

This question comes from a bank of 600 Python interview questions, all built the same way, sold as one PDF with the full answer key.

Get the 600 Python questions, 9 euros

Or SQL, Python, pandas and NumPy together, 1,636 questions, 19 euros

Or play the free Python sample, twenty questions in the browser, also as a free PDF.

Previous question Next question

Did this save you a lookup? Buy me a coffee on Ko-fi.