Python Institute PCPP1 - Certified Professional in Python Programming 1 PCPP-32-101 Exam Questions

Page: 1 / 14
Total 45 questions
Question 1

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



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

Question 2

Select the true statements about the sqlite3 module. (Select two answers.)



Answer : C, D

1. The execute method is provided by the Cursor class

This statement is true because the execute method is one of the methods of the Cursor class in the sqlite3 module. The Cursor class represents an object that can execute SQL statements and fetch results from a database connection. The execute method takes an SQL query as an argument and executes it against the database. For example, cur = conn.cursor (); cur.execute (''SELECT * FROM table'') creates and executes a cursor object that selects all rows from a table.

2. The fetchone method returns None when no rows are available

This statement is true because the fetchone method is another method of the Cursor class in the sqlite3 module. The fetchone method fetches the next row of a query result set and returns it as a single tuple or None if no more rows are available. For example, row = cur.fetchone () fetches and returns one row from the cursor object or None if there are no more rows.


Question 3

Select the true statements about sockets. (Select two answers)



Answer : A, D

1. A socket is a connection point that enables a two-way communication between programs running in a network.

This statement is true because a socket is a software structure that serves as an endpoint for sending and receiving data across a network. A socket is defined by an application programming interface (API) for the networking architecture, such as TCP/IP.A socket can be used to establish a communication channel between two programs running on the same or different network nodes12.

2. A socket is always the secure means by which computers on a network can safely communicate, without the risk of exposure to an attack.

This statement is false because a socket by itself does not provide any security or encryption for the data transmitted over the network. A socket can be vulnerable to various types of attacks, such as eavesdropping, spoofing, hijacking, or denial-of-service.To ensure secure communication, a socket can use additional protocols or mechanisms, such as SSL/TLS, SSH, VPN, or firewall3.

3. A socket is a connection point that enables a one-way communication only between remote processes.

This statement is false because a socket can enable both one-way and two-way communication between processes running on the same or different network nodes. A socket can be used for connection-oriented or connectionless communication, depending on the type of protocol used.For example, TCP is a connection-oriented protocol that provides reliable and bidirectional data transfer, while UDP is a connectionless protocol that provides unreliable and unidirectional data transfer12.

4. A socket can be used to establish a communication endpoint for processes running on the same or different machines.

This statement is true because a socket can be used for inter-process communication (IPC) within a single machine or across different machines on a network.A socket can use different types of addresses to identify the processes involved in the communication, such as IP address and port number for network sockets, or file name or path for Unix domain sockets12.


1: https://en.wikipedia.org/wiki/Network_socket2: https://www.geeksforgeeks.org/socket-in-computer-network/3: https://www.tutorialspoint.com/what-is-a-network-socket-computer-networks

Question 4

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



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 5

What is a___traceback___?

(Select two answers )



Answer : A, D

The correct answers areA. An attribute owned by every exception objectandD. An attribute that holds interesting information that is particularly useful when the programmer wants to store exception details in other objects. A traceback is an attribute of an exception object that contains a stack trace representing the call stack at the point where the exception was raised. The traceback attribute holds information about the sequence of function calls that led to the exception, which can be useful for debugging and error reporting.


Question 6

Look at the following code snippets and decide which ones follow PEP 8 recommendations for whitespaces in expressions and statements (Select two answers.)

A)

No whitespace immediately before the opening parenthesis that starts the list of arguments of a function call:

B)

A whitespace immediately before a comma, semicolon, and colon:

C)

No whitespace between a trailing comma and a following closing parenthesis:

D)

A whitespace immediately after the opening parenthesis that starts indexing or slicing:



Answer : A, C

Option A is true because PEP 8 recommends avoiding extraneous whitespace immediately inside parentheses, brackets or braces1.

Option C is true because PEP 8 recommends avoiding extraneous whitespace between a trailing comma and a following close parenthesis1.


Question 7

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



Answer : B

The provided code snippet defines a functionf1that accepts variable-length arguments using the*argsand**kwargssyntax. The*argsparameter allows for an arbitrary number of unnamed arguments to be passed to the function as a tuple, while the**kwargsparameter allows for an arbitrary number of named arguments to be passed to the function as a dictionary.

Therefore, the correct statement that best describes the code is:

1. The*argsparameter holds a list of unnamed parameters, while the**kwargsparameter holds a dictionary of named parameters.


Official Python documentation on Function definitions:https://docs.python.org/3/tutorial/controlflow.html#defining-functions

Thearg parameter holds a list of unnamed parameters. In the given code snippet, thef1function takes two arguments:*argand**kwarg. The*argsyntax in the function signature is used to pass a variable number of non-keyword (positional) arguments to the function. Inside the function,argis a tuple containing the positional arguments passed to the function. The**kwargsyntax in the function signature is used to pass a variable number of keyword arguments to the function. Inside the function,kwargis a dictionary containing the keyword arguments passed to the function.

Page:    1 / 14   
Total 45 questions