PCPP-32-101 PCPP1 – Certified Professional Python Programmer Level 1 Practice Questions
Prepare for PCPP-32-101 with more than an answer.
- Exam fee
- $295 USD
- Level
- Professional
- Valid for
- Lifetime
Domains covered on the exam 5
- Advanced Object-Oriented Programming35%
- Coding Conventions, Best Practices, and Standardization12%
- GUI Programming20%
- Network Programming18%
- File Processing and Communicating with a Program's Environment15%
- 1
In the context of PEP 20, The Zen of Python, what does the aphorism "There should be one-- and preferably only one --obvious way to do it" primarily advocate for?
Show answer details
Correct answer: C
This principle, often attributed to Python's design philosophy, favors clarity and predictability. It suggests that for a given task, the language should make the best approach easy to discover and use, reducing ambiguity and cognitive load for developers reading the code. This enhances readability and maintainability.
- 2
A developer needs to design a system with a
Loggerclass that should only ever have one instance throughout the entire application lifecycle. Subsequent attempts to create aLoggerobject should return the existing instance. Which OOP design pattern is best suited for this requirement, and how is it typically implemented in Python?Show answer details
Correct answer: B
The Singleton pattern ensures a class has only one instance and provides a global point of access to it. A common Pythonic implementation involves overriding the
__new__method. This method controls the object creation process. It can check if a class-level attribute holding the instance already exists; if not, it creates it usingsuper().__new__(cls)and stores it, otherwise, it returns the existing instance. - 3
In a client-server application using raw sockets, the client sends a message, and the server must send back a response. After the server sends its response with
socket.sendall(), what is the most reliable way for the server to ensure the client has received the entire message before the server closes the connection?Show answer details
Correct answer: B
Calling
socket.shutdown(socket.SHUT_WR)signals to the client that the server is done sending data (a TCP FIN). The server can then enter arecv()loop. The client's TCP stack will acknowledge the data, and once the client closes its end, the server'srecv()will returnb'', indicating a graceful shutdown. This confirms the client has processed the FIN and, by extension, the preceding data. Simply callingclose()can lead to a TCP RST (reset), potentially causing the client to lose data still in its receive buffer. - 4
You are creating a custom list-like class by subclassing
list. You want to override the append method to only allow integers to be added. Which implementation is correct?Show answer details
Correct answer: A
This is the correct approach. The class
IntListinherits fromlist. It overrides theappendmethod. Inside the new method, it first performs a type check usingisinstance(). If the check fails, it raises an appropriate exception. If it passes, it usessuper().append(value)to call the parent class's (list's) originalappendmethod to perform the actual append operation. - 5
Case Study: GUI Application Configuration
A developer is creating a desktop utility with Tkinter. The application needs a settings window where users can configure various options. One of these options is the application's theme, which can be 'Light', 'Dark', or 'System Default'. The user's choice must be mutually exclusive—only one theme can be active at a time.
The settings window is implemented as a
Toplevelwidget. The developer wants to useRadiobuttonwidgets for the theme selection. To manage the state of these radio buttons, aStringVaris used. When the user clicks the 'Save' button, the application should be able to retrieve the currently selected theme from theStringVar.Below is a simplified diagram of the required widget relationships:
graph TD MainWindow --> SettingsButton SettingsButton -- opens --> SettingsWindow(Toplevel) subgraph SettingsWindow SharedVar(StringVar) RB1(Radiobutton 'Light') RB2(Radiobutton 'Dark') RB3(Radiobutton 'System') SaveButton(Button 'Save') end SharedVar --> RB1 SharedVar --> RB2 SharedVar --> RB3 SaveButton -- reads --> SharedVarWhich code snippet correctly implements this group of mutually exclusive
Radiobuttonwidgets?Show answer details
Correct answer: A
This is the correct implementation. A single
StringVar(theme_var) is created to hold the state. EachRadiobuttonis linked to this same variable via thevariableoption. Thevalueoption for each button specifies the string that will be stored intheme_varwhen that button is selected. Because they all share the same variable, selecting one automatically deselects the others. - 6
A financial services company is developing a framework where different asset classes must be represented. A base
Assetclass is defined.StockandBondclasses inherit fromAsset. APortfolioclass is then created, which holds a collection ofAssetobjects. Which two primary OOP principles are demonstrated by thePortfolioclass's relationship with theAsset,Stock, andBondclasses?Show answer details
Correct answer: B
The
Portfolioclass holding a collection ofAssetobjects is a classic example of Composition (a 'has-a' relationship). Since the collection can contain objects of subclasses likeStockandBond, and thePortfoliocan operate on them through the commonAssetinterface, this demonstrates Polymorphism. Inheritance is shown byStockandBondextendingAsset, but the question specifically asks about thePortfolio's relationship. - 7
A developer needs to create a decorator that accepts an argument, for example,
@retry(attempts=3). Which code structure correctly implements a decorator that takes its own arguments?Show answer details
Correct answer: C
To create a decorator that accepts arguments, you need a factory pattern. This is typically implemented with three levels of nested functions. The outermost function accepts the decorator's arguments (e.g.,
attempts=3) and returns the actual decorator. The middle function is the decorator itself, which accepts the function to be decorated. The innermost function is the wrapper that contains the new logic and calls the original function. - 8
You are reviewing Python code and find the following line inside an
exceptblock:raise ConnectionError("Failed to connect to database") from excWhat is the primary purpose of the
from excsyntax in this context?Show answer details
Correct answer: D
The
raise NewException from OriginalExceptionsyntax is used for explicit exception chaining. It sets the__cause__attribute on the new exception to the original one. This indicates that the new exception was raised in direct response to the original one, and both tracebacks are displayed to aid in debugging. - 9
A developer is writing a REST client using the
requestslibrary to fetch user data from an API endpoint. The API requires a custom headerX-API-Keyfor authentication. Which code snippet correctly sends a GET request tohttps://api.example.com/users/123with the required custom header?Show answer details
Correct answer: B
The
requestslibrary uses theheadersparameter to pass a dictionary of custom HTTP headers. Thedataparameter is used for the request body (typically in POST or PUT),paramsis for URL query parameters, andauthis for specific authentication schemes like Basic or Digest auth. - 10
According to PEP 8, what is the recommended way to check if a list named
my_listis empty?Show answer details
Correct answer: C
PEP 8 recommends using the fact that empty sequences are 'falsy' in a boolean context. Therefore,
if not my_list:is the preferred, more Pythonic way to check for an empty list, dictionary, or other collection. Whilelen(my_list) == 0works, it is considered less idiomatic.
