Courseiva

PCAP Object-Oriented Programming Practice Question

A developer is implementing a simple counter class. The class should start at 0 and increment by 1 each time the 'increment' method is called. Which implementation is correct?

⚠ Common exam trap

Python Institute often tests the distinction between local variables and instance variables in methods, trapping candidates who forget to prefix `self` when accessing or modifying object attributes.

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

✓

class Counter:\n def __init__(self):\n self.count = 0\n def increment(self):\n self.count += 1

It properly initializes an instance variable `self.count` to 0 in the `__init__` method and then uses `self.count += 1` in the `increment` method to modify the instance variable in place. The `+=` operator is a shorthand for `self.count = self.count + 1`, which correctly updates the counter each time `increment` is called.

Answer analysis

Option-by-option breakdown

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

  • ✗

    class Counter:\n def __init__(self):\n self.count = 0\n def increment(self):\n count = self.count + 1

    Why it's wrong here

    Inside increment, the expression count = self.count + 1 creates a new local variable named count that holds the current value of self.count plus one. The instance attribute self.count is never assigned, so the object's stored value remains unchanged after each call. Every invocation recomputes the same value based on the still-zero self.count, meaning the counter never progresses and the new count is discarded when the method returns.

  • ✓

    class Counter:\n def __init__(self):\n self.count = 0\n def increment(self):\n self.count += 1

    Why this is correct

    This is the correct implementation because self.count += 1 is syntactic sugar for self.count = self.count + 1, which rebinds the instance attribute to its own current value plus one. The method receives the instance through the mandatory self parameter, so each call persists the updated count on the object. This follows the standard pattern for mutating instance state and produces the expected cumulative behavior across multiple calls.

  • ✗

    class Counter:\n def __init__(self):\n count = 0\n def increment(self):\n count += 1

    Why it's wrong here

    In __init__, the line count = 0 assigns to a local variable that disappears when the constructor finishes, so no count attribute is ever stored on the instance. Then in increment, count += 1 tries to read and rebind a local variable named count that has not been defined in that method's local scope. This raises an UnboundLocalError at runtime, and even if it were defined, it would not survive between method calls because it is not tied to the instance.

  • ✗

    class Counter:\n def __init__(self):\n self.count = 0\n def increment():\n self.count += 1

    Why it's wrong here

    The increment method is declared with no parameters, but when it is called on an instance as counter.increment(), Python automatically passes that instance as the first positional argument. Since the method signature does not have self, the call raises a TypeError about the number of arguments, and inside the method body self is an undefined name. A class method that accesses instance attributes must include self as its first parameter; otherwise, the method cannot work as an instance method.

About these practice questions

Courseiva writes every PCAP question from scratch — 421 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. 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 PCAP 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 PCAP exam.