Courseiva

PCEP Function definition syntax Practice Question

Which of the following is the correct way to define a function that takes no arguments and returns the value 42?

⚠ Common exam trap

The trap here is that option B looks almost correct but is missing the mandatory colon. Candidates may overlook the colon and incorrectly select B as well. Only D is valid.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

✓

def f(): return 42

The syntax for defining a function in Python requires the def keyword, followed by the function name, parentheses (even if no arguments), a colon, and the indented body. Option B is missing the colon after the parentheses, making it syntactically incorrect.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✗

    function f(): return 42

    Why it's wrong here

    Python has no 'function' keyword; definitions use 'def', so this raises a SyntaxError. It is tempting because 'function' matches JavaScript and other languages' syntax, but Python's def statement is the only valid way to declare a function.

  • ✗

    def f() return 42

    Why it's wrong here

    Missing the colon after the parameter list makes this a syntax error, so the function never defines. The tempting shape mimics a valid one-line body, but Python requires 'def f():' before any statement; a colon-less header only works in contexts like lambda, which cannot contain 'return'.

  • ✗

    def f: return 42

    Why it's wrong here

    Incorrect because it is missing the parentheses after the function name.

  • ✓

    def f(): return 42

    Why this is correct

    The empty parameter list satisfies the no-arguments constraint, and the bare `return 42` supplies the required value. Python permits a single-line suite after the colon, so this definition is syntactically valid and returns the integer 42 when called as `f()`.

About these practice questions

One of 482 original PCEP practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This PCEP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCEP exam.