Browse Python Institute Certification Guides

Python Institute PCPP1 Sample Questions with Explanations

Python Institute PCPP1 sample questions with explanations, traps, topic labels, and IT Mastery route links.

These original sample questions are designed to help you check how the exam topics appear in decision-style prompts. They are not taken from the live exam.

Use these sample questions as a guided self-assessment for Certified Professional Python Programmer 1 (PCPP1) topics such as object-oriented design, descriptors and properties, decorators, exceptions, generators, context managers, packaging, testing, and maintainable Python structure. The prompts emphasize professional code reasoning rather than syntax recall alone.

Where these questions fit in the PCPP1 guide

The sample set below is part of the Python Institute PCPP1 guide path:

PCPP1 professional Python sample questions

Work through each prompt before opening the explanation. PCPP1 questions often ask whether a design is maintainable, testable, and idiomatic, not just whether it runs once.


Question 1

Topic: Property validation

A class stores a percentage value that must stay between 0 and 100. Other code should access it as obj.percent, but every assignment must be validated. Which design is most appropriate?

  • A. Store the value in a public attribute and rely on comments to warn callers.
  • B. Require callers to edit a global variable before constructing the object.
  • C. Override __str__ so invalid values are hidden when printed.
  • D. Use a property with a setter that validates the value before storing it in a private backing attribute.

Best answer: D

Explanation: A property preserves normal attribute syntax while centralizing validation in the setter. The backing attribute stores the actual value, and the setter enforces the invariant whenever code assigns to the public property.

Why the other choices are weaker:

  • A documents the rule but does not enforce it.
  • B introduces unrelated global state and weakens encapsulation.
  • C changes string display but does not prevent invalid state.

What this tests: Encapsulation, properties, validation, invariants, and maintainable class design.

Related topics: Properties; Setters; Encapsulation; Validation; Object-oriented design


Question 2

Topic: Generator behavior

What is printed by this code?

 1def numbers():
 2    print("start")
 3    yield 1
 4    print("middle")
 5    yield 2
 6
 7gen = numbers()
 8print("created")
 9print(next(gen))
10print(next(gen))
  • A. start, created, 1, middle, 2
  • B. created, start, 1, middle, 2
  • C. created, 1, 2, start, middle
  • D. The code raises StopIteration before printing 2.

Best answer: B

Explanation: Calling a generator function creates a generator object but does not execute the function body. The body starts when next(gen) is called. The first next prints start and yields 1; the second resumes after the first yield, prints middle, and yields 2.

Why the other choices are weaker:

  • A assumes the generator body runs during generator creation.
  • C ignores the print statements inside the generator.
  • D would happen only after advancing beyond the second yield.

What this tests: Lazy generator execution, yield, suspension, and resume order.

Related topics: Generators; yield; Iterators; next; Execution order


Question 3

Topic: Context manager cleanup

A file-processing class opens a temporary resource and must close it even if processing raises an exception. Which implementation detail is most important?

  • A. Put all cleanup code in __init__ because initialization always runs first.
  • B. Use a broad except Exception block that silently ignores every failure.
  • C. Implement context-manager cleanup in __exit__ or use a with-compatible resource so cleanup runs when the block exits.
  • D. Define __repr__ so the object displays the file path during debugging.

Best answer: C

Explanation: A context manager expresses resource lifetime directly. __enter__ sets up the resource, and __exit__ runs when the with block exits, including when an exception occurs. That makes cleanup reliable and visible in the calling code.

Why the other choices are weaker:

  • A initializes the object but cannot clean up after later processing.
  • B can hide failures and still may not structure resource lifetime clearly.
  • D helps debugging output but does not manage cleanup.

What this tests: Context managers, deterministic cleanup, exception-safe resource handling, and professional Python patterns.

Related topics: Context managers; with; __enter__; __exit__; Resource cleanup


Question 4

Topic: Decorator metadata

A decorator logs function calls and returns an inner wrapper. After applying it to several functions, documentation tools show every decorated function with the wrapper’s name and docstring. What should the decorator use?

  • A. functools.wraps on the wrapper function to preserve metadata from the wrapped function.
  • B. str() on the wrapped function before calling it.
  • C. A global variable containing the last function name.
  • D. A custom exception so documentation tools skip decorated functions.

Best answer: A

Explanation: functools.wraps copies useful metadata such as __name__, __doc__, and annotations from the original function to the wrapper. That keeps debugging, introspection, and documentation output aligned with the decorated function.

Why the other choices are weaker:

  • B creates a string representation but does not preserve wrapper metadata.
  • C is fragile shared state and does not fix function attributes.
  • D avoids documentation instead of making the decorator well-behaved.

What this tests: Decorator implementation, introspection metadata, and maintainability.

Related topics: Decorators; functools.wraps; Metadata; Introspection; Documentation

Independent study note

Tech Exam Lexicon and IT Mastery are independent study tools. They are not affiliated with, endorsed by, or sponsored by Python Institute or any certification body.

Revised on Sunday, May 10, 2026