Python Institute PCEP - Certified Entry-Level Python Programmer PCEP-30-02 Exam Questions

Page: 1 / 14
Total 30 questions
Question 1

What is the expected result of the following code?



Answer : D

The code snippet that you have sent is trying to use a list comprehension to create a new list from an existing list. The code is as follows:

my_list = [1, 2, 3, 4, 5] new_list = [x for x in my_list if x > 5]

The code starts with creating a list called ''my_list'' that contains the numbers 1, 2, 3, 4, and 5. Then, it tries to create a new list called ''new_list'' by using a list comprehension. A list comprehension is a concise way of creating a new list from an existing list by applying some expression or condition to each element. The syntax of a list comprehension is:

new_list = [expression for element in old_list if condition]

The expression is the value that will be added to the new list, which can be the same as the element or a modified version of it. The element is the variable that takes each value from the old list. The condition is an optional filter that determines which elements will be included in the new list. For example, the following list comprehension creates a new list that contains the squares of the even numbers from the old list:

old_list = [1, 2, 3, 4, 5, 6] new_list = [x ** 2 for x in old_list if x % 2 == 0]

new_list = [4, 16, 36]

The code that you have sent is trying to create a new list that contains the elements from the old list that are greater than 5. However, there is a problem with this code. The problem is that none of the elements in the old list are greater than 5, so the condition is always false. This means that the new list will be empty, and the expression will never be evaluated. However, the expression is not valid, because it uses the variable x without defining it. This will cause a NameError exception, which is an error that occurs when a variable name is not found in the current scope. The code does not handle the exception, and therefore it will terminate with an error message.

The expected result of the code is an unhandled exception, because the code tries to use an undefined variable in an expression that is never executed. Therefore, the correct answer is D. The code will cause an unhandled exception.


Question 2

Which of the following functions can be invoked with two arguments?

A)

B)

C)

D)



Answer : B

The code snippets that you have sent are defining four different functions in Python. A function is a block of code that performs a specific task and can be reused in the program. A function can take zero or more arguments, which are values that are passed to the function when it is called. A function can also return a value or None, which is the default return value in Python.

To define a function in Python, you use the def keyword, followed by the name of the function and parentheses. Inside the parentheses, you can specify the names of the parameters that the function will accept. After the parentheses, you use a colon and then indent the code block that contains the statements of the function. For example:

def function_name(parameter1, parameter2): # statements of the function return value

To call a function in Python, you use the name of the function followed by parentheses. Inside the parentheses, you can pass the values for the arguments that the function expects. The number and order of the arguments must match the number and order of the parameters in the function definition, unless you use keyword arguments or default values. For example:

function_name(argument1, argument2)

The code snippets that you have sent are as follows:

A) def my_function(): print(''Hello'')

B) def my_function(a, b): return a + b

C) def my_function(a, b, c): return a * b * c

D) def my_function(a, b=0): return a - b

The question is asking which of these functions can be invoked with two arguments. This means that the function must have two parameters in its definition, or one parameter with a default value and one without. The default value is a value that is assigned to a parameter if no argument is given for it when the function is called. For example, in option D, the parameter b has a default value of 0, so the function can be called with one or two arguments.

The only option that meets this criterion is option B. The function in option B has two parameters, a and b, and returns the sum of them. This function can be invoked with two arguments, such as my_function(2, 3), which will return 5.

The other options cannot be invoked with two arguments. Option A has no parameters, so it can only be called with no arguments, such as my_function(), which will print ''Hello''. Option C has three parameters, a, b, and c, and returns the product of them. This function can only be called with three arguments, such as my_function(2, 3, 4), which will return 24. Option D has one parameter with a default value, b, and one without, a, and returns the difference of them. This function can be called with one or two arguments, such as my_function(2) or my_function(2, 3), which will return 2 or -1, respectively.

Therefore, the correct answer is B. Option B.


Question 3

What is the expected output of the following code?



Answer : C

The code snippet that you have sent is checking if two numbers are equal and printing the result. The code is as follows:

num1 = 1 num2 = 2 if num1 == num2: print(4) else: print(1)

The code starts with assigning the values 1 and 2 to the variables ''num1'' and ''num2'' respectively. Then, it enters an if statement that compares the values of ''num1'' and ''num2'' using the equality operator (==). If the values are equal, the code prints 4 to the screen. If the values are not equal, the code prints 1 to the screen.

The expected output of the code is 1, because the values of ''num1'' and ''num2'' are not equal. Therefore, the correct answer is C. 1.


Question 4

What is the expected output of the following code?



Answer : B

The code snippet that you have sent is using the slicing operation to get parts of a string and concatenate them together. The code is as follows:

pizza = ''pizza'' pasta = ''pasta'' folpetti = ''folpetti'' print(pizza[0] + pasta[0] + folpetti[0])

The code starts with assigning the strings ''pizza'', ''pasta'', and ''folpetti'' to the variables pizza, pasta, and folpetti respectively. Then, it uses the print function to display the result of concatenating the first characters of each string. The first character of a string can be accessed by using the index 0 inside square brackets. For example, pizza[0] returns ''p''. The concatenation operation is used to join two or more strings together by using the + operator. For example, ''a'' + ''b'' returns ''ab''. The code prints the result of pizza[0] + pasta[0] + folpetti[0], which is ''p'' + ''p'' + ''f'', which is ''ppt''.

The expected output of the code is ppt, because the code prints the first characters of each string. Therefore, the correct answer is B. ppt.


Question 5

What is the expected result of running the following code?



Answer : C

The code snippet that you have sent is trying to use the index method to find the position of a value in a list. The code is as follows:

the_list = [1, 2, 3, 4, 5] print(the_list.index(6))

The code starts with creating a list called ''the_list'' that contains the numbers 1, 2, 3, 4, and 5. Then, it tries to print the result of calling the index method on the list with the argument 6. The index method is used to return the first occurrence of a value in a list. For example, the_list.index(1) returns 0, because 1 is the first value in the list.

However, the code has a problem. The problem is that the value 6 is not present in the list, so the index method cannot find it. This will cause a ValueError exception, which is an error that occurs when a function or operation receives an argument that has the right type but an inappropriate value. The code does not handle the exception, and therefore it will terminate with an error message.

The expected result of the code is an unhandled exception, because the code tries to find a value that does not exist in the list. Therefore, the correct answer is C. The code raises an unhandled exception.


Question 6

Which of the following are the names of Python passing argument styles?

(Select two answers.)



Answer : A, D

Keyword arguments are arguments that are specified by using the name of the parameter, followed by an equal sign and the value of the argument. For example,print (sep='-', end='!')is a function call with keyword arguments.Keyword arguments can be used to pass arguments in any order, and to provide default values for some arguments1.

Positional arguments are arguments that are passed in the same order as the parameters of the function definition. For example,print ('Hello', 'World')is a function call with positional arguments.Positional arguments must be passed before any keyword arguments, and they must match the number and type of the parameters of the function2.


Question 7

What happens when the user runs the following code?



Answer : D

The code snippet that you have sent is a while loop with an if statement and a print statement inside it. The code is as follows:

while True: if counter < 0: print('''') else: print(''**'')

The code starts with entering a while loop that repeats indefinitely, because the condition ''True'' is always true. Inside the loop, the code checks if the value of ''counter'' is less than 0. If yes, it prints a single asterisk () to the screen. If no, it prints three asterisks (**) to the screen. However, the code does not change the value of ''counter'' inside the loop, so the same condition is checked over and over again. The loop never ends, and the code enters an infinite loop.

The program outputs either one asterisk () or three asterisks (**) to the screen repeatedly, depending on the initial value of ''counter''. Therefore, the correct answer is D. The program enters an infinite loop.


Page:    1 / 14   
Total 30 questions