python institute PCEP-30-02 Exam Questions

Questions for the PCEP-30-02 were updated on : Nov 21 ,2025

Page 1 out of 2. Viewing questions 1-15 out of 30

Question 1

What is the expected output of the following code?

  • A. 2
  • B. 0
  • C. 3
  • D. 1
Answer:

D

User Votes:
A
50%
B
50%
C
50%
D
50%

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.
Reference:
Python List count() Method - W3Schools

Discussions
vote your answer:
A
B
C
D
0 / 1000

Question 2

What is the expected result of the following code?

  • A. The code is erroneous and cannot be run.
  • B. 20
  • C. 10
  • D. 30
Answer:

A

User Votes:
A
50%
B
50%
C
50%
D
50%

Explanation:
The code snippet that you have sent is trying to use the global keyword to access and modify a
global variable inside a function. The code is as follows:
speed = 10 def velocity(): global speed speed = speed + 10 return speed
print(velocity())
The code starts with creating a global variable called “speed” and assigning it the value 10. A global
variable is a variable that is defined outside any function and can be accessed by any part of the
code. Then, the code defines a function called “velocity” that takes no parameters and returns the
value of “speed” after adding 10 to it. Inside the function, the code uses the global keyword to
declare that it wants to use the global variable “speed”, not a local one. A local variable is a variable
that is defined inside a function and can only be accessed by that function. The global keyword
allows the function to modify the global variable, not just read it. Then, the code adds 10 to the value
of “speed” and returns it. Finally, the code calls the function “velocity” and prints the result.
However, the code has a problem. The problem is that the code uses the global keyword inside the
function, but not outside. The global keyword is only needed when you want to modify a global
variable inside a function, not when you want to create or access it outside a function. If you use the
global keyword outside a function, you will get a SyntaxError exception, which is an error that occurs
when the code does not follow the rules of the Python language. 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 uses the global
keyword incorrectly. Therefore, the correct answer is A. The code is erroneous and cannot be run.
Reference:
Python Global Keyword - W3SchoolsPython Exceptions: An Introduction – Real Python
The code is erroneous because it is trying to call the “velocity” function without passing any
parameter, which will raise a TypeError exception. The “velocity” function requires one parameter
“x”, which is used to calculate the return value of “speed” multiplied by “x”. If no parameter is
passed, the function will not know what value to use for “x”.
The code is also erroneous because it is trying to use the “new_speed” variable before it is defined.
The “new_speed” variable is assigned the value of 20 after the first function call, but it is used as a
parameter for the second function call, which will raise a NameError exception. The variable should
be defined before it is used in any expression or function call.
Therefore, the code will not run and will not produce any output.
The correct way to write the code would be:
# Define the speed variable
speed = 10
# Define the velocity function
def velocity(x):
return speed * x
# Define the new_speed variable
new_speed = 20
# Call the velocity function with new_speed as a parameter
print(velocity(new_speed))
Copy
This code will print 200, which is the result of 10 multiplied by 20.
Reference:
[Python Programmer Certification (PCPP) – Level 1]
[Python Programmer Certification (PCPP) – Level 2]
[Python Programmer Certification (PCPP) – Level 3]
[Python: Built-in Exceptions]
[Python: Defining Functions]
[Python: More on Variables and Printing]

Discussions
vote your answer:
A
B
C
D
0 / 1000

Question 3

Which of the following are the names of Python passing argument styles?
(Select two answers.)

  • A. keyword
  • B. reference
  • C. indicatory
  • D. positional
Answer:

A, D

User Votes:
A
50%
B
50%
C
50%
D
50%

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 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
.
Reference: 1: 5 Types of Arguments in Python Function Definitions | Built In 2
: python - What’s the
pythonic way to pass arguments between functions …

Discussions
vote your answer:
A
B
C
D
0 / 1000

Question 4

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

B)

C)

D)

  • A. Option A
  • B. Option B
  • C. Option C
  • D. Option D
Answer:

B

User Votes:
A
50%
B
50%
C
50%
D
50%

Explanation:
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.

Discussions
vote your answer:
A
B
C
D
0 / 1000

Question 5

What is true about exceptions and debugging? (Select two answers.)

  • A. A tool that allows you to precisely trace program execution is called a debugger.
  • B. If some Python code is executed without errors, this proves that there are no errors in it.
  • C. One try-except block may contain more than one except branch.
  • D. The default (anonymous) except branch cannot be the last branch in the try-except block.
Answer:

A, C

User Votes:
A
50%
B
50%
C
50%
D
50%

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.
Reference:
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]

Discussions
vote your answer:
A
B
C
D
0 / 1000

Question 6

What is the expected output of the following code?

  • A. 1
  • B. The code raises an unhandled exception.
  • C. False
  • D. ('Fermi ', '2021', 'False')
Answer:

D

User Votes:
A
50%
B
50%
C
50%
D
50%

Explanation:
The code snippet that you have sent is defining and calling a function in Python. The code is as
follows:
def runner(brand, model, year): return (brand, model, year)
print(runner(“Fermi”))
The code starts with defining a function called “runner” with three parameters: “brand”, “model”,
and “year”. The function returns a tuple with the values of the parameters. A tuple is a data type in
Python that can store multiple values in an ordered and immutable way. A tuple is created by using
parentheses and separating the values with commas. For example, (1, 2, 3) is a tuple with three
values.
Then, the code calls the function “runner” with the value “Fermi” for the “brand” parameter and
prints the result. However, the function expects three arguments, but only one is given. This will
cause a TypeError exception, which is an error that occurs when a function or operation receives an
argument that has the wrong type or number. The code does not handle the exception, and therefore
it will terminate with an error message.
However, if the code had handled the exception, or if the function had used default values for the
missing parameters, the expected output of the code would be ('Fermi ', ‘2021’, ‘False’). This is
because the function returns a tuple with the values of the parameters, and the print function
displays the tuple to the screen. Therefore, the correct answer is D. ('Fermi ', ‘2021’, ‘False’).
Reference:
Python Functions - W3SchoolsPython Tuples - W3SchoolsPython Exceptions: An
Introduction – Real Python

Discussions
vote your answer:
A
B
C
D
0 / 1000

Question 7

What is the expected result of running the following code?

  • A. The code prints 1 .
  • B. The code prints 2
  • C. The code raises an unhandled exception.
  • D. The code prints 0
Answer:

C

User Votes:
A
50%
B
50%
C
50%
D
50%

Explanation:
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.
Reference:
Python List index() Method - W3SchoolsPython Exceptions: An Introduction – Real Python

Discussions
vote your answer:
A
B
C
D
0 / 1000

Question 8

DRAG DROP
Drag and drop the code boxes in order to build a program which prints Unavailable to the screen.
(Note: one code box will not be used.)

Answer:

None

User Votes:

Explanation:

Discussions
vote your answer:
0 / 1000

Question 9

What is the expected result of the following code?

  • A. 5
  • B. 2
  • C. 1
  • D. The code will cause an unhandled
Answer:

D

User Votes:
A
50%
B
50%
C
50%
D
50%

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.
Reference:
Python - List Comprehension - W3SchoolsPython - List Comprehension -
GeeksforGeeksPython Exceptions: An Introduction – Real Python

Discussions
vote your answer:
A
B
C
D
0 / 1000

Question 10

What is the expected output of the following code?

  • A. The code is erroneous and cannot be run.
  • B. ppt
  • C. 213
  • D. pizzapastafolpetti
Answer:

B

User Votes:
A
50%
B
50%
C
50%
D
50%

Explanation:
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.
Reference:
Python String Slicing - W3SchoolsPython String Concatenation - W3Schools

Discussions
vote your answer:
A
B
C
D
0 / 1000

Question 11

Assuming that the following assignment has been successfully executed:

Which of the following expressions evaluate to True? (Select two expressions.)

  • A. the_List.index {"1"} in the_list
  • B. 1.1 in the_list |1:3 |
  • C. len (the list [0:2]} <3
  • D. the_list. index {'1'} -- 0
Answer:

CD

User Votes:
A
50%
B
50%
C
50%
D
50%

Explanation:
The code snippet that you have sent is assigning a list of four values to a variable called “the_list”.
The code is as follows:
the_list = [‘1’, 1, 1, 1]
The code creates a list object that contains the values ‘1’, 1, 1, and 1, and assigns it to the variable
“the_list”. The list can be accessed by using the variable name or by using the index of the values.
The index starts from 0 for the first value and goes up to the length of the list minus one for the last
value. The index can also be negative, in which case it counts from the end of the list. For example,
the_list[0] returns ‘1’, and the_list[-1] returns 1.
The expressions that you have given are trying to evaluate some conditions on the list and return a
boolean value, either True or False. Some of them are valid, and some of them are invalid and will
raise an exception. An exception is an error that occurs when the code cannot be executed properly.
The expressions are as follows:
A) the_List.index {“1”} in the_list: This expression is trying to check if the index of the value ‘1’ in the
list is also a value in the list. However, this expression is invalid, because it uses curly brackets instead
of parentheses to call the index method. 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_list.index {“1”} will raise a SyntaxError exception and output nothing.
B) 1.1 in the_list |1:3 |: This expression is trying to check if the value 1.1 is present in a sublist of the
list. However, this expression is invalid, because it uses a vertical bar instead of a colon to specify the
start and end index of the sublist. The sublist is obtained by using the slicing operation, which uses
square brackets and a colon to get a part of the list. For example, the_list[1:3] returns [1, 1], which is
the sublist of the list from the index 1 to the index 3, excluding the end index. However, the_list |1:3
| will raise a SyntaxError exception and output nothing.
C) len (the list [0:2]} <3: This expression is trying to check if the length of a sublist of the list is less
than 3. This expression is valid, because it uses the len function and the slicing operation correctly.
The len function is used to return the number of values in a list or a sublist. For example, len(the_list)
returns 4, because the list has four values. The slicing operation is used to get a part of the list by
using square brackets and a colon. For example, the_list[0:2] returns [‘1’, 1], which is the sublist of
the list from the index 0 to the index 2, excluding the end index. The expression len (the list [0:2]} <3
returns True, because the length of the sublist [‘1’, 1] is 2, which is less than 3.
D) the_list. index {‘1’} – 0: This expression is trying to check if the index of the value ‘1’ in the list is
equal to 0. This expression is valid, because it uses the index method and the equality operator
correctly. 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. The equality operator is used to
compare two values and return True if they are equal, or False if they are not. For example, 0 == 0
returns True, and 0 == 1 returns False. The expression the_list. index {‘1’} – 0 returns True, because
the index of ‘1’ in the list is 0, and 0 is equal to 0.
Therefore, the correct answers are C. len (the list [0:2]} <3 and D. the_list. index {‘1’} – 0.
Reference:
Python List Methods - W3Schools5. Data Structures — Python 3.11.5 documentationList
methods in Python - GeeksforGeeks

Discussions
vote your answer:
A
B
C
D
0 / 1000

Question 12

DRAG DROP
Assuming that the phonc_dir dictionary contains namemumber pairs, arrange the code boxes to
create a valid line of code which retrieves Martin Eden's phone number, and assigns it to the number
variable.

Answer:

None

User Votes:

Explanation:

number = phone_dir["Martin Eden"]
This code uses the square brackets notation to access the value associated with the key “Martin
Eden” in the phone_dir dictionary. The value is then assigned to the variable number. A dictionary is
a data structure that stores key-value pairs, where each key is unique and can be used to retrieve its
corresponding value. You can find more information about dictionaries in Python in the following
references:
[Python Dictionaries - W3Schools]
[Python Dictionary (With Examples) - Programiz]
[5.5. Dictionaries — How to Think Like a Computer Scientist …]

Discussions
vote your answer:
0 / 1000

Question 13

What is true about tuples? (Select two answers.)

  • A. Tuples are immutable, which means that their contents cannot be changed during their lifetime.
  • B. The len { } function cannot be applied to tuples.
  • C. An empty tuple is written as { } .
  • D. Tuples can be indexed and sliced like lists.
Answer:

A, D

User Votes:
A
50%
B
50%
C
50%
D
50%

Explanation:
Tuples are one of the built-in data types in Python that are used to store collections of data. Tuples
have some characteristics that distinguish them from other data types, such as lists, sets, and
dictionaries. Some of these characteristics are:
Tuples are immutable, which means that their contents cannot be changed during their lifetime.
Once a tuple is created, it cannot be modified, added, or removed. This makes tuples more stable
and reliable than mutable data types. However, this also means that tuples are less flexible and
dynamic than mutable data types.
For example, if you want to change an element in a tuple, you
have to create a new tuple with the modified element and assign it to the same variable12
Tuples are ordered, which means that the items in a tuple have a defined order and can be accessed
by using their index. The index of a tuple starts from 0 for the first item and goes up to the length of
the tuple minus one for the last item. The index can also be negative, in which case it counts from
the end of the tuple. For example, if you have a tuple t = ("a", "b", "c"), then t[0] returns "a", and t[-
1] returns "c"
Tuples can be indexed and sliced like lists, which means that you can get a single item or a sublist of a
tuple by using square brackets and specifying the start and end index. For example, if you have a
tuple t = ("a", "b", "c", "d", "e"), then t[2] returns "c", and t[1:4] returns ("b", "c", "d"). Slicing does
not raise any exception, even if the start or end index is out of range.
It will just return an empty
tuple or the closest possible sublist12
Tuples can contain any data type, such as strings, numbers, booleans, lists, sets, dictionaries, or even
other tuples. Tuples can also have duplicate values, which means that the same item can appear
more than once in a tuple. For example, you can have a tuple t = (1, 2, 3, 1, 2)
, which contains two 1s
and two 2s12
Tuples are written with round brackets, which means that you have to enclose the items in a tuple
with parentheses. For example, you can create a tuple t = ("a", "b", "c") by using round brackets.
However, you can also create a tuple without using round brackets, by just separating the items with
commas. For example, you can create the same tuple t = "a", "b", "c" by using commas.
This is called
tuple packing, and it allows you to assign multiple values to a single variable12
The len() function can be applied to tuples, which means that you can get the number of items in a
tuple by using the len() function. For example, if you have a tuple t = ("a", "b", "c"),
then len(t)
returns 312
An empty tuple is written as (), which means that you have to use an empty pair of parentheses to
create a tuple with no items. For example, you can create an empty tuple t = () by using empty
parentheses. However, if you want to create a tuple with only one item, you have to add a comma
after the item, otherwise Python will not recognize it as a tuple. For example, you can create a tuple
with one item t = ("a",)
by using a comma12
Therefore, the correct answers are A. Tuples are immutable, which means that their contents cannot
be changed during their lifetime. and D. Tuples can be indexed and sliced like lists.
Reference:
Python Tuples - W3SchoolsTuples in Python - GeeksforGeeks

Discussions
vote your answer:
A
B
C
D
0 / 1000

Question 14

Assuming that the following assignment has been successfully executed:
My_list – [1, 1, 2, 3]
Select the expressions which will not raise any exception.
(Select two expressions.)

  • A. my_list[-10]
  • B. my_list|my_Li1st | 3| I
  • C. my list [6]
  • D. my_List- [0:1]
Answer:

B, D

User Votes:
A
50%
B
50%
C
50%
D
50%

Explanation:
The code snippet that you have sent is assigning a list of four numbers to a variable called “my_list”.
The code is as follows:
my_list = [1, 1, 2, 3]
The code creates a list object that contains the elements 1, 1, 2, and 3, and assigns it to the variable
“my_list”. The list can be accessed by using the variable name or by using the index of the elements.
The index starts from 0 for the first element and goes up to the length of the list minus one for the
last element. The index can also be negative, in which case it counts from the end of the list. For
example, my_list[0] returns 1, and my_list[-1] returns 3.
The code also allows some operations on the list, such as slicing, concatenation, repetition, and
membership. Slicing is used to get a sublist of the original list by specifying the start and end index.
For example, my_list[1:3] returns [1, 2]. Concatenation is used to join two lists together by using the
+ operator. For example, my_list + [4, 5] returns [1, 1, 2, 3, 4, 5]. Repetition is used to create a new
list by repeating the original list a number of times by using the * operator. For example, my_list * 2
returns [1, 1, 2, 3, 1, 1, 2, 3]. Membership is used to check if an element is present in the list by using
the in operator. For example, 2 in my_list returns True, and 4 in my_list returns False.
The expressions that you have given are trying to access or manipulate the list in different ways.
Some of them are valid, and some of them are invalid and will raise an exception. An exception is an
error that occurs when the code cannot be executed properly. The expressions are as follows:
A) my_list[-10]: This expression is trying to access the element at the index -10 of the list. However,
the list only has four elements, so the index -10 is out of range. This will raise an IndexError exception
and output nothing.
B) my_list|my_Li1st | 3| I: This expression is trying to perform a bitwise OR operation on the list and
some other operands. The bitwise OR operation is used to compare the binary representation of two
numbers and return a new number that has a 1 in each bit position where either number has a 1. For
example, 3 | 1 returns 3, because 3 in binary is 11 and 1 in binary is 01, and 11 | 01 is 11. However,
the bitwise OR operation cannot be applied to a list, because a list is not a number. This will raise a
TypeError exception and output nothing.
C) my list [6]: This expression is trying to access the element at the index 6 of the list. However, the
list only has four elements, so the index 6 is out of range. This will raise an IndexError exception and
output nothing.
D) my_List- [0:1]: This expression is trying to perform a subtraction operation on the list and a sublist.
The subtraction operation is used to subtract one number from another and return the difference.
For example, 3 - 1 returns 2. However, the subtraction operation cannot be applied to a list, because
a list is not a number. This will raise a TypeError exception and output nothing.
Only two expressions will not raise any exception. They are:
B) my_list|my_Li1st | 3| I: This expression is not a valid Python code, but it is not an expression that
tries to access or manipulate the list. It is just a string of characters that has no meaning. Therefore, it
will not raise any exception, but it will also not output anything.
D) my_List- [0:1]: This expression is a valid Python code that uses the slicing operation to get a sublist
of the list. The slicing operation does not raise any exception, even if the start or end index is out of
range. It will just return an empty list or the closest possible sublist. For example, my_list[0:10]
returns [1, 1, 2, 3], and my_list[10:20] returns []. The expression my_List- [0:1] returns the sublist of
the list from the index 0 to the index 1, excluding the end index. Therefore, it returns [1]. This
expression will not raise any exception, and it will output [1].
Therefore, the correct answers are B. my_list|my_Li1st | 3| I and D. my_List- [0:1].
Reference: [Python Institute - Entry-Level Python Programmer Certification]

Discussions
vote your answer:
A
B
C
D
0 / 1000

Question 15

What is the expected output of the following code?

  • A. 5
  • B. 4
  • C. 6
  • D. The code raises an exception and outputs nothing.
Answer:

D

User Votes:
A
50%
B
50%
C
50%
D
50%

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.
Reference: [Python Institute - Entry-Level Python Programmer Certification]

Discussions
vote your answer:
A
B
C
D
0 / 1000
To page 2