Sample questions
Certified Associate Python Programmer PCAP practice questions
Which TWO of the following are valid ways to raise an exception in Python?
Trap 1: raise
Valid only inside except block to re-raise.
Trap 2: raise Exception
Missing parentheses; should be Exception().
Trap 3: throw ValueError('error')
'throw' is not Python; use 'raise'.
- A
raise
Why wrong: Valid only inside except block to re-raise.
- B
raise Exception
Why wrong: Missing parentheses; should be Exception().
- C
throw ValueError('error')
Why wrong: 'throw' is not Python; use 'raise'.
- D
raise Exception('error')
Correct syntax to raise an exception with message.
- E
raise ValueError('invalid value')
Raises a specific exception.
Match each Python operator to its precedence level (1=highest).
Drag a concept onto its matching description — or click a concept then click the description.
1
3
4
7
8
Match each Python module to its purpose.
Drag a concept onto its matching description — or click a concept then click the description.
Mathematical functions
Generate pseudo-random numbers
Manipulate dates and times
Work with JSON data
Interact with operating system
Drag and drop the steps to create and activate a virtual environment in Python into the correct order.
Drag steps to the numbered slots on the right, or tap a step then tap a slot.
Drag and drop the steps to create a Python package with subpackages into the correct order.
Drag steps to the numbered slots on the right, or tap a step then tap a slot.
Drag and drop the steps to handle an exception in Python using try-except-finally into the correct order.
Drag steps to the numbered slots on the right, or tap a step then tap a slot.
Drag and drop the steps to define and call a function with default arguments in Python into the correct order.
Drag steps to the numbered slots on the right, or tap a step then tap a slot.
Drag and drop the steps to create a simple HTTP server using the http.server module in Python into the correct order.
Drag steps to the numbered slots on the right, or tap a step then tap a slot.
Drag and drop the steps to debug a Python script using pdb into the correct order.
Drag steps to the numbered slots on the right, or tap a step then tap a slot.
Drag and drop the steps to install a third-party package using pip in Python into the correct order.
Drag steps to the numbered slots on the right, or tap a step then tap a slot.
Refer to the exhibit. What is the output of the code?
Exhibit
s = 'Python Programming' print(s[7:])
Trap 1: Program
Would be s[7:14]?
Trap 2: Python Programming
This is the entire string.
Trap 3: Python
Would be s[:6].
- A
Program
Why wrong: Would be s[7:14]?
- B
Programming
Index 7 is the 'P' of 'Programming', slice from there to end.
- C
Python Programming
Why wrong: This is the entire string.
- D
Python
Why wrong: Would be s[:6].
A class 'Point' is defined with __slots__ = ['x', 'y']. A developer creates an instance p = Point() and tries to set p.z = 10. What happens?
Trap 1: The assignment is silently ignored
It raises an error.
Trap 2: Python issues a warning and adds the attribute
No warning; raises AttributeError.
Trap 3: The attribute 'z' is added dynamically
Slots prevents dynamic attributes.
- A
The assignment is silently ignored
Why wrong: It raises an error.
- B
Python issues a warning and adds the attribute
Why wrong: No warning; raises AttributeError.
- C
The attribute 'z' is added dynamically
Why wrong: Slots prevents dynamic attributes.
- D
AttributeError is raised
Slots disallow adding new attributes.
What is the output of the code?
Exhibit
Refer to the exhibit.
```python
class Base:
def __init__(self):
self._x = 10
def get_x(self):
return self._x
class Derived(Base):
def __init__(self):
self._x = 20
obj = Derived()
print(obj.get_x())
```Trap 1: AttributeError: 'Derived' object has no attribute 'get_x'
get_x is inherited from Base.
Trap 2: 10
Base.__init__ is not called, so _x is not set to 10.
Trap 3: AttributeError: 'Derived' object has no attribute '_x'
Derived has _x set in its __init__.
- A
20
Derived.__init__ sets _x to 20, and get_x returns it.
- B
AttributeError: 'Derived' object has no attribute 'get_x'
Why wrong: get_x is inherited from Base.
- C
10
Why wrong: Base.__init__ is not called, so _x is not set to 10.
- D
AttributeError: 'Derived' object has no attribute '_x'
Why wrong: Derived has _x set in its __init__.
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?
Trap 1: Interface Segregation Principle
ISP is about keeping interfaces small.
Trap 2: Single Responsibility Principle
SRP is about each class having one responsibility.
Trap 3: Dependency Inversion Principle
DIP is about high-level modules not depending on low-level modules.
- A
Interface Segregation Principle
Why wrong: ISP is about keeping interfaces small.
- B
Liskov Substitution Principle
LSP ensures that any subclass can replace the base class without breaking the function.
- C
Single Responsibility Principle
Why wrong: SRP is about each class having one responsibility.
- D
Dependency Inversion Principle
Why wrong: DIP is about high-level modules not depending on low-level modules.
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?
Trap 1: print(mypackage)
This prints the module object representation, not its location.
Trap 2: print(__file__)
This shows the path of the currently executing script, not the imported module.
Trap 3: import os; print(os.getcwd())
This shows the current working directory, not the module location.
- A
print(mypackage)
Why wrong: This prints the module object representation, not its location.
- B
print(__file__)
Why wrong: This shows the path of the currently executing script, not the imported module.
- C
print(mypackage.__file__)
This attribute contains the path to the module's file.
- D
import os; print(os.getcwd())
Why wrong: This shows the current working directory, not the module location.
Which THREE methods return a boolean value?
Trap 1: str.upper()
Returns a string.
Trap 2: str.find()
Returns integer index or -1.
- A
str.upper()
Why wrong: Returns a string.
- B
str.startswith()
Returns True or False.
- C
str.islower()
Returns True or False.
- D
str.isalpha()
Returns True or False.
- E
str.find()
Why wrong: Returns integer index or -1.
Match each variable scope to its description.
Drag a concept onto its matching description — or click a concept then click the description.
Inside a function
At module level
In outer function (nested)
Predefined names in Python
Variable from enclosing scope (not global)
Match each exception to its cause.
Drag a concept onto its matching description — or click a concept then click the description.
Operation on incompatible type
Function receives argument with correct type but invalid value
Sequence subscript out of range
Mapping key not found
Attribute reference or assignment fails
Match each list method to its effect.
Drag a concept onto its matching description — or click a concept then click the description.
Adds x to end
Appends elements from iterable
Inserts x at index i
Removes first occurrence of x
Removes and returns last item
Match each code snippet to its output.
Drag a concept onto its matching description — or click a concept then click the description.
8
3
1
15
14
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)Trap 1: 1 1
Incorrect; each object gets its own id.
Trap 2: 0 1
Incorrect; count increments before assignment.
Trap 3: 2 2
Incorrect; count starts at 0, increments before assignment.
- A
1 1
Why wrong: Incorrect; each object gets its own id.
- B
0 1
Why wrong: Incorrect; count increments before assignment.
- C
1 2
Correct.
- D
2 2
Why wrong: Incorrect; count starts at 0, increments before assignment.
Match each Python built-in function to its description.
Drag a concept onto its matching description — or click a concept then click the description.
Returns the length of an object
Generates a sequence of numbers
Returns a sorted list from an iterable
Returns index and value pairs
Aggregates elements from multiple iterables
Match each string method to its purpose.
Drag a concept onto its matching description — or click a concept then click the description.
Returns uppercase copy
Splits into list of substrings
Removes leading/trailing whitespace
Replaces occurrences of a substring
Returns index of first occurrence
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 mismatchTrap 1: It causes the original exception to be ignored.
The original exception is suppressed in output but still occurred.
Trap 2: It causes both exceptions to be raised simultaneously.
Only the second exception is raised.
Trap 3: It replaces the original exception with a new one.
It does not replace; it suppresses the context.
- A
It causes the original exception to be ignored.
Why wrong: The original exception is suppressed in output but still occurred.
- B
It prevents chaining of exceptions, so only the final exception is displayed.
The 'from None' disables exception chaining.
- C
It causes both exceptions to be raised simultaneously.
Why wrong: Only the second exception is raised.
- D
It replaces the original exception with a new one.
Why wrong: It does not replace; it suppresses the context.
Question Discussion
Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.
Sign in to join the discussion.