PCPP1 – Certified Professional Python Programmer Level 1 Free Sample Questions

Description

Covers advanced object-oriented programming with inheritance and composition, PEP 8 and PEP 257 conventions, tkinter GUI programming, socket-based network programming, and file processing.

20 free sample questions218 in the full practice test

Try simulator

PCPP-32-101 Sample Questions

  1. Question 1

    Intermediate

    Advanced Object-Oriented Programming · Inheritance, polymorphism, and composition

    A financial services company is developing a framework where different asset classes must be represented. A base Asset class is defined. Stock and Bond classes inherit from Asset. A Portfolio class is then created, which holds a collection of Asset objects. Which two primary OOP principles are demonstrated by the Portfolio class's relationship with the Asset, Stock, and Bond classes?

    Answer and explanation

    Correct answer: B

    The Portfolio class holding a collection of Asset objects is a classic example of Composition (a 'has-a' relationship). Since the collection can contain objects of subclasses like Stock and Bond, and the Portfolio can operate on them through the common Asset interface, this demonstrates Polymorphism. Inheritance is shown by Stock and Bond extending Asset, but the question specifically asks about the Portfolio's relationship.

  2. Question 2

    Advanced

    Advanced Object-Oriented Programming · Extended function argument syntax and decorators

    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?

    Answer and explanation

    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.

  3. Question 3

    Intermediate

    Advanced Object-Oriented Programming · Advanced exception handling

    You are reviewing Python code and find the following line inside an except block:

    raise ConnectionError("Failed to connect to database") from exc

    What is the primary purpose of the from exc syntax in this context?

    Answer and explanation

    Correct answer: D

    The raise NewException from OriginalException syntax 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.

  4. Question 4

    Beginner

    Network Programming · REST Client Development

    A developer is writing a REST client using the requests library to fetch user data from an API endpoint. The API requires a custom header X-API-Key for authentication. Which code snippet correctly sends a GET request to https://api.example.com/users/123 with the required custom header?

    Answer and explanation

    Correct answer: B

    The requests library uses the headers parameter to pass a dictionary of custom HTTP headers. The data parameter is used for the request body (typically in POST or PUT), params is for URL query parameters, and auth is for specific authentication schemes like Basic or Digest auth.

  5. Question 5

    Beginner

    Coding Conventions, Best Practices, and Standardization · PEP 8 Guidelines

    According to PEP 8, what is the recommended way to check if a list named my_list is empty?

    Answer and explanation

    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. While len(my_list) == 0 works, it is considered less idiomatic.

  6. Question 6

    Intermediate

    File Processing and Communicating with a Program's Environment · File Format Processing

    A developer is using the csv module to write data to a file. They want to ensure that each row is written from a dictionary, where the dictionary keys correspond to the header row. Which combination of csv module components should be used to open the file and write the rows?

    Answer and explanation

    Correct answer: B

    The csv.DictWriter class is specifically designed to write rows from dictionaries. It requires a fieldnames argument during instantiation to define the header. Its writerow method then takes a dictionary and writes the values corresponding to the fieldnames. It's crucial to open the file with newline='' to prevent extra blank rows from being written on some platforms.

  7. Question 7

    Intermediate

    GUI Programming · GUI toolkits and layout management

    When designing a GUI with tkinter, what is the fundamental difference between the grid and place geometry managers?

    Answer and explanation

    Correct answer: C

    The grid manager organizes widgets in a table-like structure of rows and columns. This allows the layout to adapt as the window is resized. In contrast, the place manager positions widgets at specific x/y coordinates (either absolute pixels or relative to the container size), which does not automatically adjust well to window resizing, making it less suitable for most applications.

  8. Question 8

    Intermediate

    Advanced Object-Oriented Programming · Python core syntax operations

    A developer needs to create a custom class that behaves like a dictionary but logs a message every time a key is accessed. Which magic method must be implemented to intercept key retrieval?

    Answer and explanation

    Correct answer: C

    The __getitem__ method is used to implement evaluation of self[key], which is how dictionary values are accessed. By implementing this method, the developer can add custom logic (like logging) before retrieving and returning the value. __getattr__ is for attribute access (e.g., self.key), and __get__ is part of the descriptor protocol.

  9. Question 9

    Intermediate

    Advanced Object-Oriented Programming · (De)serialization of Python objects

    What is the primary motivation for using the shelve module over the pickle module for object persistence?

    Answer and explanation

    Correct answer: C

    The main advantage of shelve is its dictionary-like interface. It stores pickled objects in a database file, allowing you to access, add, or modify individual objects by their string key without having to load and rewrite the entire data structure. In contrast, pickle typically serializes an entire object graph to a stream, which must be read sequentially.

  10. Question 10

    Beginner

    Network Programming · Working with sockets in Python

    True or False: In Python's socket module, TCP sockets are created using socket.SOCK_DGRAM and UDP sockets are created using socket.SOCK_STREAM.

    Answer and explanation

    Correct answer: B

    The statement is false. It has the constants reversed. socket.SOCK_STREAM is used for connection-oriented TCP sockets, while socket.SOCK_DGRAM is used for connectionless UDP sockets.

Register free for 10 more questions

Or unlock all 218 PCPP-32-101 questions with explanations, timed mode and flashcards.