nerdexam
Python_Institute

PCEP-30-02 · Question #351

What is the expected output of the following code? ``python def runner(brand, model="", year=2021, convertible=True): return brand + model + str(convertible) print(runner("Volts", "Tension", 2019)…

The correct answer is C. e. Calling runner("Volts", "Tension", 2019) binds brand="Volts", model="Tension", year=2019, and leaves convertible at its default True. The function returns "Volts" + "Tension" + str(True), producing the string "VoltsTensionTrue". Applying [-1] (Python's last-character index) to…

Question

What is the expected output of the following code?
def runner(brand, model="", year=2021, convertible=True):
 return brand + model + str(convertible)

print(runner("Volts", "Tension", 2019) [-1])

Options

  • ATrue
  • BThe code raises an unhandled exception.
  • Ce
  • DVolta Tension

How the community answered

(33 responses)
  • A
    3% (1)
  • B
    9% (3)
  • C
    73% (24)
  • D
    15% (5)

Explanation

Calling runner("Volts", "Tension", 2019) binds brand="Volts", model="Tension", year=2019, and leaves convertible at its default True. The function returns "Volts" + "Tension" + str(True), producing the string "VoltsTensionTrue". Applying [-1] (Python's last-character index) to that string yields "e" - the final character of "True".

Why the distractors fail:

  • A (True) - The return value is the string "VoltsTensionTrue", not the boolean True; indexing it with [-1] extracts a character, not the whole value.
  • B (exception) - No exception occurs; all arguments are valid and string indexing is legal.
  • D ("Volta Tension") - The brand is "Volts" (not "Volta"), no spaces are concatenated, and [-1] returns a single character anyway.

Memory tip: Whenever you see str(bool) in Python, remember str(True) == "True" and str(False) == "False" - both end in "e" - so negative indexing on either will quietly return "e".

Community Discussion

No community discussion yet for this question.

Full PCEP-30-02 Practice