Back to Certified Associate Python Programmer PCAP questions

Scenario-based practice

Hard Difficulty Questions

Practise Certified Associate Python Programmer PCAP practice questions — original exam-style scenarios covering every exam domain, with detailed explanations, wrong-answer analysis, and common exam traps.

20
scenario questions
PCAP
exam code
Python Institute
vendor

Scenario guide

How to approach hard difficulty questions

These are the questions most candidates get wrong. They require connecting multiple concepts, reading tricky output, or knowing edge-case behaviour that isn't on most study cards. Practising them trains you to operate under uncertainty — a necessary skill on the real exam.

Quick answer

Hard Difficulty Questions questions test whether you can apply the concept in context, not just recognise a definition.

How the topic appears in realistic exam-style scenarios.

Which detail in the question changes the correct answer.

How to eliminate plausible but wrong options.

How to connect the question back to the wider exam objective.

Related practice questions

Related PCAP topic practice pages

Scenario questions usually connect to one or more exam topics. Use these links to review the underlying concepts behind the scenario.

Practice set

Practice scenarios

A Python class 'Shape' defines an abstract method 'area'. Subclasses 'Circle' and 'Square' implement 'area'. A function 'calculate_area(shape)' expects a 'Shape' instance. Which principle ensures that the function works correctly without knowing the specific subclass?

A developer notices that a custom package 'mypackage' is not being found when importing, even though it is installed in the site-packages directory. The developer suspects a conflict with another package of the same name. Which command should the developer run to diagnose the location from which Python is importing the package?

Question 3hardmulti select
Full question →

Which THREE methods return a boolean value?

Question 4hardmultiple choice
Full question →

Refer to the exhibit. What is the output?

Exhibit

class Counter:
    count = 0
    def __init__(self):
        Counter.count += 1
        self.id = Counter.count

c1 = Counter()
c2 = Counter()
print(c1.id, c2.id)
Question 5hardmultiple choice
Full question →

Refer to the exhibit. What does the 'from None' clause do in the second raise statement?

Exhibit

Traceback (most recent call last):
  File "app.py", line 10, in <module>
    raise ValueError('Invalid value')
ValueError: Invalid value

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "app.py", line 12, in <module>
    raise TypeError('Type mismatch') from None
TypeError: Type mismatch
Question 6hardmulti select
Full question →

Refer to the exhibit. Which THREE statements about the class hierarchy are correct?

Exhibit

class A:
    def method(self):
        return 'A'

class B(A):
    def method(self):
        return 'B'

class C(A):
    def method(self):
        return 'C'

class D(B, C):
    pass

What is the output of the Python code after reading the config.txt file?

Exhibit

Refer to the exhibit.

# config.txt
[Settings]
host = localhost
port = 8080
debug = True

# Python code
import configparser

config = configparser.ConfigParser()
config.read('config.txt')
port = config.getint('Settings', 'port')
print(port)
Question 8hardmultiple choice
Full question →

A developer is tasked with validating user input that must be a 10-digit phone number. The input may contain spaces, dashes, and parentheses. Which approach best ensures the input contains exactly 10 digits?

Question 9hardmultiple choice
Full question →

Refer to the exhibit. What is the output?

Exhibit

text = 'Hello, World!'
result = text.replace('Hello', 'Hi').upper()
print(result)
Question 10hardmultiple choice
Full question →

A developer writes a class 'Logger' with a class method 'log(msg)' that writes to a file. Another class 'AppLogger' inherits from 'Logger'. The developer expects both classes to share the same file handle. However, after creating an instance of 'AppLogger', the file handle is different. What is the most likely cause?

Question 11hardmultiple choice
Full question →

A developer defines a class 'A' with a method 'm' that uses 'self.a'. Class 'B' inherits from 'A' and defines __init__ that sets 'self.a = 10'. An instance of B is created and method m is called. What is the output?

Question 12hardmultiple choice
Read the full NAT/PAT explanation →

A developer is implementing a custom context manager to manage database connections. The context manager should automatically roll back the transaction if an exception occurs, but commit if no exception occurs. Which pattern ensures this behavior correctly?

Which THREE of the following are true about Python's exception hierarchy?

Question 14hardmultiple choice
Read the full NAT/PAT explanation →

You are a developer for a data science team. The team uses a shared module 'utilities' located at /team/shared/utilities.py. This module is not part of any package, and they want to import it from various project scripts without copying the file. Some projects are in /home/user/proj_A/ and others in /var/data/proj_B/. Currently, each script manually adds /team/shared/ to sys.path using sys.path.insert(0, '/team/shared/'). This works but is repetitive. The team wants a cleaner solution that also works when the script is run from different working directories. They consider creating a package 'utilities' by adding an __init__.py to the directory and using relative imports. However, the module currently uses absolute imports for some external libraries. What is the best course of action to allow clean imports of utilities from any location while minimizing changes to the module itself?

Question 15hardmulti select
Full question →

Which THREE methods must be implemented to create an immutable object that supports iteration and membership testing?

Question 16hardmultiple choice
Full question →

Which of the following is a correct use of the @property decorator to create a getter and setter for an attribute named 'score' that ensures score stays between 0 and 100?

Question 17hardmultiple choice
Full question →

Refer to the exhibit. What is the output?

Exhibit

class A:
    def method(self):
        print("A", end="")
class B(A):
    def method(self):
        print("B", end="")
        super().method()
class C(A):
    def method(self):
        print("C", end="")
        super().method()
class D(B, C):
    def method(self):
        print("D", end="")
        super().method()
obj = D()
obj.method()

Which of the following best describes the use of the @abstractmethod decorator in Python?

Question 19hardmultiple choice
Full question →

Refer to the exhibit. What is the root cause of the error?

Exhibit

class MyClass:
    def __init__(self):
        self.name = "test"
obj = MyClass()
print(obj.age)

Which THREE of the following escape sequences are valid in a Python string and represent a single character? (Select exactly three.)

These PCAP practice questions are part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style PCAP questions with detailed explanations, topic-based practice, mock exams, readiness tracking, and study analytics.