PCAP Object-Oriented Programming Practice Question
A programmer uses a class method to create an alternative constructor for a `Point` class. The method should parse a string like "10,20" and return a `Point` instance with x=10, y=20. Which code snippet correctly implements this?
⚠ Common exam trap
The PCAP exam often tests the distinction between `@classmethod` and `@staticmethod` by presenting a method that looks like it should be a static method but actually needs access to the class for proper inheritance, tempting candidates to choose the static version or a plain method without a decorator.
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
✓
`@classmethod\ndef from_string(cls, s):\n parts = s.split(',')\n return cls(int(parts[0]), int(parts[1]))`
It uses the `@classmethod` decorator, which automatically passes the class (`cls`) as the first argument. This allows the method to create an instance of the class using `cls(...)`, making it a proper alternative constructor that works correctly even if the class is subclassed. The method parses the string "10,20" by splitting on the comma and converting the parts to integers.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
`def from_string(self, s):\n parts = s.split(',')\n return Point(int(parts[0]), int(parts[1]))`
Why it's wrong here
This version defines an instance method: the first parameter is bound to the instance on which the method is called, regardless of the name self. Without an existing Point object you cannot invoke it as a constructor from the class itself, and calling Point.from_string(...) raises a TypeError about the missing required argument self. It also hardcodes Point in the body, which makes the method awkward to reuse in subclasses.
- ✗
`@staticmethod\ndef from_string(s):\n parts = s.split(',')\n return Point(int(parts[0]), int(parts[1]))`
Why it's wrong here
@staticmethod turns the function into a plain function that does not receive any automatic first argument. Because the body has no cls reference, it must hardcode Point, so if a subclass inherits from_string it will still return a base Point object rather than an instance of the subclass. This works for the exact base class but silently breaks polymorphic alternative construction in class hierarchies.
- ✗
`def from_string(cls, s):\n parts = s.split(',')\n return cls(int(parts[0]), int(parts[1]))`
Why it's wrong here
Naming the first parameter cls does nothing by itself; without the @classmethod decorator, Python still treats the first argument as a normal positional parameter intended for an instance. When you try to call Point.from_string(...), the method has no instance to bind, so the call fails with a TypeError indicating that self is missing. The identifier cls is merely a naming convention and does not change how the method is bound.
- ✓
`@classmethod\ndef from_string(cls, s):\n parts = s.split(',')\n return cls(int(parts[0]), int(parts[1]))`
Why this is correct
This is the canonical alternative constructor pattern: the @classmethod decorator makes Python bind the actual class object to the cls parameter, so calling Point.from_string(...) passes Point as cls. Using cls(parts[0], parts[1]) instead of Point(...) means the method respects inheritance — a subclass that inherits from_string will construct instances of that subclass, not the base class. This is exactly how standard library methods such as datetime.fromtimestamp and dict.fromkeys work.
Go deeper
Related to this question
About these practice questions
Courseiva writes every PCAP question from scratch — 169 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 →
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.