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
What is the output of the following code snippet involving shallow and deep copying?
import copy list_a = [1, 2, [3, 4]] list_b = copy.copy(list_a) list_c = copy.deepcopy(list_a) list_a[2][0] = 99 print(list_b[2][0], list_c[2][0])Show answer details
Correct answer: B
copy.copy()performs a shallow copy. It creates a new listlist_b, but the elements inside are references to the same objects as inlist_a. Therefore, the nested list[3, 4]is shared. Whenlist_a[2][0]is modified, the change is visible inlist_b.copy.deepcopy()performs a deep copy, recursively copying all objects.list_cgets a completely independent copy of the nested list, so it remains unaffected by the change tolist_a. - 2
A developer needs to parse an application configuration file named
app.ini. The file contains sections, keys, and values. Which standard library module is specifically designed for parsing this type of file format?Show answer details
Correct answer: C
The
configparsermodule is the standard Python library for working with configuration files in the format of INI files. It allows you to read, write, and manipulate structured configuration data organized into sections with key-value pairs. - 3
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.
- 4
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. - 5
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. - 6
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. - 7
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. - 8
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. - 9
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. - 10
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.
