Free Python Institute PCPP-32-101 Exam Actual Questions

The questions for PCPP-32-101 were last updated On May 4, 2024

Question No. 1

Select the true statement about composition

Show Answer Hide Answer
Correct Answer: B

Composition is an object-oriented design concept that models ahas-a relationship. In composition, a class known ascompositecontains an object of another class known ascomponent.In other words, a composite class has a component of another class1.

1. Composition allows a class to be projected as a container of different classes.

Composition is a concept in Python that allows for building complex objects out of simpler objects, by aggregating one or more objects of another class as attributes. The objects that are aggregated are generally considered to be parts of the whole object, and the containing object is often viewed as a container for the smaller objects.

In composition, objects are combined in a way that allows for greater flexibility and modifiability than what inheritance can offer. With composition, it is possible to create new objects by combining existing objects, by using a container object to host other objects. By contrast, with inheritance, new objects extend the behavior of their parent classes, and are limited by that inheritance hierarchy.


Official Python documentation on Composition:https://docs.python.org/3/tutorial/classes.html#composition

GeeksforGeeks article on Composition vs Inheritance:https://www.geeksforgeeks.org/composition-vs-inheritance-python/

Real Python article on Composition and Inheritance:https://realpython.com/inheritance-composition-python/

Question No. 2

Analyze the following snippet and select the statement that best describes it.

Show Answer Hide Answer
Correct Answer: D

In the given code snippet, an instance ofOwnMathexception is raised with an explicitly specified__cause__attribute that refers to the original exception (ZeroDivisionError). This is an example of explicitly chaining exceptions in Python.


Question No. 3

Analyze the following snippet and choose the best statement that describes it.

Show Answer Hide Answer
Correct Answer: C

The correct answer isC. Excalibur is the value passed to an instance variable. In the given code snippet,self.nameis an instance variable of theSwordclass. When an instance of theSwordclass is created withvarl = Sword('Excalibur'), the value'Excalibur'is passed as an argument to the__init__method and assigned to thenameinstance variable of thevarlobject.

The code defines a class calledSwordwith an__init__method that takes one parametername. When a new instance of theSwordclass is created withvarl = Sword('Excalibur'), the value of the'Excalibur'string is passed as an argument to the__init__method, and assigned to theself.nameinstance variable of thevarlobject.


Official Python documentation on Classes:https://docs.python.org/3/tutorial/classes.html

Question No. 4

The following snippet represents one of the OOP pillars Which one is that?

Show Answer Hide Answer
Correct Answer: C

The given code snippet demonstrates the concept of encapsulation in object-oriented programming. Encapsulation refers to the practice of keeping the internal state and behavior of an object hidden from the outside world and providing a public interface for interacting with the object. In the given code snippet, the__init__andget_balancemethods provide a public interface for interacting with instances of theBankAccountclass, while the__balanceattribute is kept hidden from the outside world by using a double underscore prefix.


Question No. 5

Analyze the following function and choose the statement that best describes it.

Show Answer Hide Answer
Correct Answer: A

In the given code snippet, therepeatfunction is a decorator that takes an argumentnum_timesspecifying the number of times the decorated function should be called. Therepeatfunction returns an inner functionwrapper_repeatthat takes a functionfuncas an argument and returns another inner functionwrapperthat callsfuncnum_timestimes.

The provided code snippet represents an example of a decorator that accepts its own arguments. The@decorator_functionsyntax is used to apply thedecorator_functionto thesome_functionfunction. Thedecorator_functiontakes an argumentarg1and defines an inner functionwrapper_functionthat takes the original functionfuncas its argument. Thewrapper_functionthen returns the result of callingfunc, along with thearg1argument passed to thedecorator_function.

Here is an example of how to use this decorator withsome_function:

@decorator_function('argument 1')

def some_function():

return 'Hello world'

Whensome_functionis called, it will first be passed as an argument to thedecorator_function. Thedecorator_functionthen adds the string'argument 1'to the result of callingsome_function()and returns the resulting string. In this case, the final output would be'Hello world argument 1'.


Official Python documentation on Decorators:https://docs.python.org/3/glossary.html#term-decorator