Python Institute PCEP-30-02 - Questions & Answers
Free preview · every answer includes a full explanation
Product page: https://prepkeys.com/pcep-30-02.html
What is the expected result of the following code?

5
2
1
The code will cause an unhandled
Explanation: 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.
References:
Python - List Comprehension - W3SchoolsPython - List Comprehension -
GeeksforGeeksPython Exceptions: An Introduction ?Real Python
What happens when the user runs the following code?

The code outputs 3.
The code outputs 2.
The code enters an infinite loop.
The code outputs 1.
Explanation: The code snippet that you have sent is calculating the value of a variable "total" based on the values in the range of 0 to 3. The code is as follows: total = 0 for i in range(0, 3): if i % 2 == 0: total = total + 1 else: total = total + 2 print(total) The code starts with assigning the value 0 to the variable "total". Then, it enters a for loop that iterates over the values 0, 1, and 2 (the range function excludes the upper bound). Inside the loop, the code checks if the current value of "i" is even or odd using the modulo operator (%). If "i" is even, the code adds 1 to the value of "total". If "i" is odd, the code adds 2 to the value of "total". The loop ends when "i" reaches 3, and the code prints the final value of "total" to the screen.
The code outputs 2 to the screen, because the value of "total" changes as follows: When i = 0, total = 0 + 1 = 1
When i = 1, total = 1 + 2 = 3
When i = 2, total = 3 + 1 = 4
When i = 3, the loop ends and total = 4 is printed Therefore, the correct answer is B. The code outputs 2.
References:
[Python Institute - Entry-Level Python Programmer Certification]
What is the expected output of the following code?

The code outputs nothing.
3
1
4
Explanation: 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.
References:
[Python Institute - Entry-Level Python Programmer Certification]
What is the expected output of the following code?

5
4
6
The code raises an exception and outputs nothing.
Explanation: The code snippet that you have sent is trying to print the combined length of two lists, "collection" and "duplicate". The code is as follows:
collection = [] collection.append(1) collection.insert(0, 2) duplicate = collection duplicate.append(3) print(len (collection) + len(duplicate)) The code starts with creating an empty list called "collection" and appending the number 1 to it. The list now contains [1]. Then, the code inserts the number 2 at the beginning of the list. The list now contains [2, 1]. Then, the code creates a new list called "duplicate" and assigns it the value of "collection". However, this does not create a copy of the list, but rather a reference to the same list object. Therefore, any changes made to "duplicate" will also affect "collection", and vice versa. Then, the code appends the number 3 to "duplicate". The list now contains [2, 1, 3], and so does "collection". Finally, the code tries to print the sum of the lengths of "collection" and "duplicate". However, this causes an exception, because the len function expects a single argument, not two. The code does not handle the exception, and therefore outputs nothing. The expected output of the code is nothing, because the code raises an exception and terminates. Therefore, the correct answer is D. The code raises an exception and outputs nothing.
References:
[Python Institute - Entry-Level Python Programmer Certification]
A set of rules which defines the ways in which words can be coupled in sentences is called:
lexis
syntax
semantics
dictionary
Explanation: Syntax is the branch of linguistics that studies the structure and rules of sentences in natural languages. Lexis is the vocabulary of a language. Semantics is the study of meaning in language. A dictionary is a collection of words and their definitions, synonyms, pronunciations, etc.
References:
[Python Institute - Entry-Level Python Programmer Certification]
What happens when the user runs the following code?

The program outputs three asterisks ( *** )to the screen.
The program outputs one asterisk ( * ) to the screen.
The program outputs five asterisks ( ***** ) to the screen.
The program enters an infinite loop.
Explanation: 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.
References:
[Python Institute - Entry-Level Python Programmer Certification]
Which of the following are the names of Python passing argument styles? (Select two answers.)
keyword
reference
indicatory
positional
Explanation: 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 arguments 1. 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 function 2.
References:
1: 5 Types of Arguments in Python Function Definitions | Built In
2: python - What's the
pythonic way to pass arguments between functions ...
What is the expected output of the following code?

2
0
3
1
Explanation: The code snippet that you have sent is using the count method to count the number of occurrences of a value in a list. The code is as follows:
my_list = [1, 2, 3, 4, 5] print(my_list.count(1))
The code starts with creating a list called "my_list" that contains the numbers 1, 2, 3, 4, and 5. Then, it uses the print function to display the result of calling the count method on the list with the argument 1. The count method is used to return the number of times a value appears in a list. For example, my_list.count(1) returns 1, because 1 appears once in the list.
The expected output of the code is 1, because the code prints the number of occurrences of 1 in the list.
Therefore, the correct answer is D. 1.
References:
Python List count() Method - W3Schools
What is true about exceptions and debugging? (Select two answers.)
A tool that allows you to precisely trace program execution is called a debugger.
If some Python code is executed without errors, this proves that there are no errors in it.
One try-except block may contain more than one except branch.
The default (anonymous) except branch cannot be the last branch in the try-except block.
Explanation: Exceptions and debugging are two important concepts in Python programming that are related to handling and preventing errors. Exceptions are errors that occur when the code cannot be executed properly, such as syntax errors, type errors, index errors, etc. Debugging is the process of finding and fixing errors in the code, using various tools and techniques. Some of the facts about exceptions and debugging are: A tool that allows you to precisely trace program execution is called a debugger. A debugger is a program that can run another program step by step, inspect the values of variables, set breakpoints, evaluate expressions, etc. A debugger can help you find the source and cause of an error, and test possible solutions. Python has a built-in debugger module called pdb, which can be used from the command line or within the code. There are also other third-party debuggers available for Python, such as PyCharm, Visual Studio Code, etc12
If some Python code is executed without errors, this does not prove that there are no errors in it. It only means that the code did not encounter any exceptions that would stop the execution. However, the code may still have logical errors, which are errors that cause the code to produce incorrect or unexpected results. For example, if you write a function that is supposed to calculate the area of a circle, but you use the wrong formula, the code may run without errors, but it will give you the wrong answer. Logical errors are harder to detect and debug than syntax or runtime errors, because they do not generate any error messages. You have to test the code with different inputs and outputs, and compare them with the expected results34
One try-except block may contain more than one except branch. A try-except block is a way of handling exceptions in Python, by using the keywords try and except. The try block contains the code that may raise an exception, and the except block contains the code that will execute if an exception occurs. You can have multiple except blocks for different types of exceptions, or for different actions to take. For example, you can write a try-except block like this: try: # some code that may raise an exception except ValueError: # handle the ValueError exception except ZeroDivisionError: # handle the ZeroDivisionError exception except: # handle any other exception.
This way, you can customize the error handling for different situations, and provide more informative messages or alternative solutions5
The default (anonymous) except branch can be the last branch in the try-except block. The default except branch is the one that does not specify any exception type, and it will catch any exception that is not handled by the previous except branches. The default except branch can be the last branch in the try-except block, but it cannot be the first or the only branch. For example, you can write a try-except block like this: try: # some code that may raise an exception except ValueError: # handle the ValueError exception except: # handle any other exception
This is a valid try-except block, and the default except branch will be the last branch. However, you cannot write a try-except block like this: try: # some code that may raise an exception except: # handle any exception.
This is an invalid try-except block, because the default except branch is the only branch, and it will catch all exceptions, even those that are not errors, such as KeyboardInterrupt or SystemExit. This is considered a bad practice, because it may hide or ignore important exceptions that should be handled differently or propagated further.
Therefore, you should always specify the exception types that you want to handle, and use the default except branch only as a last resort5
Therefore, the correct answers are
A. A tool that allows you to precisely trace program execution is called a debugger. and
C. One try-except block may contain more than one except branch.
References:
Python Debugger ?Python pdb - GeeksforGeeksHow can I see the details of an exception in
Python's debugger?Python Debugging (fixing problems)Python - start interactive debugger when exception would be otherwise thrownPython Try Except [Error Handling and Debugging -- Programming with Python for Engineers]
How many hashes (+) does the code output to the screen?

one
zero (the code outputs nothing)
five
three
Explanation: The code snippet that you have sent is a loop that checks if a variable "floor" is less than or equal to 0 and prints a string accordingly. The code is as follows:
floor = 5 while floor > 0: print("+") floor = floor - 1 The code starts with assigning the value 5 to the variable
"floor". Then, it enters a while loop that repeats as long as the condition "floor > 0" is true. Inside the loop, the code prints a "+" symbol to the screen, and then subtracts 1 from the value of "floor". The loop ends when "floor" becomes 0 or negative, and the code exits. The code outputs five "+" symbols to the screen, one for each iteration of the loop.
Therefore, the correct answer is C. five.
References:
[Python Institute - Entry-Level Python Programmer Certification]
Showing 10 of 420 questions · Unlock the full set