Python Institute PCEP-30-01 Certified Entry-Level Python Programmer Online Training
Python Institute PCEP-30-01 Online Training
The questions for PCEP-30-01 were last updated at Nov 22,2024.
- Exam Code: PCEP-30-01
- Exam Name: Certified Entry-Level Python Programmer
- Certification Provider: Python Institute
- Latest update: Nov 22,2024
What is the output of the following code?
a = 1
b = 0
x = a or b
y = not(a and b)
print(x + y)
- A . The program will cause an error
- B . 1
- C . The output cannot be predicted
- D . 2
D
Explanation:
Topics: logical operators booleans addition operator
implicit type casting
Try it yourself:
a = 1
b = 0
x = a or b
print(x) # 1
print(1 or 0) # 1
y = not(a and b)
print(y) # True
print(1 and 0) # 0
print(not 0) # True
print(x + y) # 2
print(1 + True) # 2
If you calculate with a boolean True becomes the integer 1 and therefore 1 + True is 2
What is the output of the following snippet?
dct = {}
dct[‘1’] = (1, 2)
dct[‘2’] = (2, 1)
for x in dct.keys():
print(dct[x][1], end=”)
- A . 21
- B . (2,1)
- C . (1,2)
- D . 12
A
Explanation:
Topics: dictionary tuple indexing for dict.keys() print()
Try it yourself:
dct = {}
dct[‘1’] = (1, 2)
dct[‘2’] = (2, 1)
print(dct) # {‘1’: (1, 2), ‘2’: (2, 1)}
for x in dct.keys():
print(dct[x][1], end=”) # 21
print()
print(dct[‘1’][1]) # 2
print(dct[‘2’][1]) # 1
dct.keys() are the keys ‘1’ and ‘2’
dct[‘1’][1] is 2 and
dct[‘2’][1] is 1
What would you insert instead of so that the program checks for even numbers?
if ???:
print(‘x is an even number’)
- A . x % x == 0
- B . x % 1 == 2
- C . x % 2 == 0
- D . x % ‘even’ == True
- E . x % 2 == 1
C
Explanation:
Topics: if modulus operator equal to operator
Try it yourself:
x = 4
if x % 2 == 0:
print(‘x is an even number’) # x is an even number
Every number that divided by two does not leave a rest is even.
What is the expected output of the following code?
x = [0, 1, 2]
x.insert(0, 1)
del x[1]
print(sum(x))
- A . 3
- B . 2
- C . 4
- D . 5
C
Explanation:
Topics: insert() del sum() list
Try it yourself:
x = [0, 1, 2]
x.insert(0, 1)
print(x) # [1, 0, 1, 2]
del x[1]
print(x) # [1, 1, 2]
print(sum(x)) # 4
insert() inserts an item at a given position.
The first argument is the index of the element before which to insert.
insert(0, 1) inserts 1 before index 0 (at the front of the list).
The del keyword deletes the given object.
In this case x[1]
The sum() function adds the items of a list (or a different iterable) and returns the sum.
The digraph written as #! is used to:
- A . tell a Unix or Unix-like OS how to execute the contents of a Python file.
- B . create a docstring.
- C . make a particular module entity a private one.
- D . tell an MS Windows OS how to execute the contents of a Python file.
A
Explanation:
Topics: #! shebang
This is a general UNIX topic.
Best read about it here:
https://en.wikipedia.org/wiki/Shebang_(Unix)
What is the expected output of the following code?
data = [‘Peter’, 404, 3.03, ‘Wellert’, 33.3]
print(data[1:3])
- A . None of the above.
- B . [‘Peter’, 404, 3.03, ‘Wellert’, 33.3]
- C . [‘Peter’, ‘Wellert’]
- D . [404, 3.03]
D
Explanation:
Topic: list indexing
Try it yourself:
data = [‘Peter’, 404, 3.03, ‘Wellert’, 33.3]
print(data[1:3]) # [404, 3.03]
You have a list of five elements of various data types.
[1:3] slices inclusive the first index and exclusive the third index.
Meaning it slices the first and second index.
What will be the output of the following code snippet?
d = {}
d[1] = 1
d[‘1’] = 2
d[1] += 1
sum = 0
for k in d:
sum += d[k]
print(sum)
- A . 3
- B . 4
- C . 1
- D . 2
B
Explanation:
Topics: for dictionary indexes add and assign operator
Try it yourself:
d = {}
print(d) # {}
d[1] = 1
print(d) # {1: 1}
d[‘1’] = 2
print(d) # {1: 1, ‘1’: 2}
d[1] += 1
print(d) # {1: 2, ‘1’: 2}
sum = 0
for k in d:
sum += d[k]
print("key: ", k, " – value: ", d[k])
# key: 1 – value: 2
print(sum) # 4
sum = 0
for k in d.keys():
sum += d[k]
print("key: ", k, " – value: ", d[k])
# key: 1 – value: 2
print(sum) # 4
The knowledge you need here is that a dictionary
can have indexes of different data types.
Therefore d[1] is a different index than d[‘1’]
and they can both exist in the same dictionary.
To iterate through a dictionary is the same as
iterating through dict.keys()
In k will be the keys of the dictionary.
In this case 1 and ‘1’
The value of the first key will be 2
and the value of the other key will also be 2
and therefore (the) sum is 4
How many stars will the following snippet print to the monitor?
i = 4
while i > 0:
i -= 2
print(‘*’)
if i == 2:
break
else:
print(‘*’)
The snippet will enter an infinite loop.
- A . 0
- B . 2
- C . 1
C
Explanation:
Topics: if while break else (nobreak)
Try it yourself:
i = 4
while i > 0: # i is 4
i -= 2 # i is 2
print(‘*’) # *
if i == 2: # Yip, i is 2
break # Leave the loop directly
else: # Does not apply, because the break got triggered
print(‘*’)
In the first iteration the break gets directly triggered.
Therefore there will be only one star.
The else would only apply, if the break does NOT get triggered.
What is CPython?
- A . It’ a programming language that is a superset of the C language,
- B . designed to produce Python-like performance with code written in C
- C . It’ a programming language that is a superset of the Python,
- D . designed to produce C-like performance with code written in Python
- E . It’s a default, reference implementation of the C language, written in Python
- F . It’s a default, reference implementation of the Python language, written in C
F
Explanation:
Topic: CPython
Guido van Rossum used the "C" programming language to implement the very first version of his language and this decision is still in force.
All Pythons coming from the PSF (Python Software Foundation)
are written in the "C" language.
There are many reasons for this approach.
One of them (probably the most important) is that thanks to it, Python may be easily ported and migrated to all platforms with the ability to compile and run "C" language programs (virtually all platforms have this feature,
which opens up many expansion opportunities for Python).
This is why the PSF implementation is often referred to as CPython. This is the most influential Python among all the Pythons in the world. https://en.wikipedia.org/wiki/CPython
What is the expected output of the following code?
def func(p1, p2):
p1 = 1
p2[0] = 42
x = 3
y = [1, 2, 3]
func(x, y)
print(x, y[0])
- A . 3 1
- B . The code is erroneous.
- C . 1 42
- D . 3 42
- E . 1 1
D
Explanation:
Topics: def list argument passing mutable vs. immutable
Try it yourself:
def func(p1, p2):
p1 = 1
p2[0] = 42
x = 3
y = [1, 2, 3]
func(x, y)
print(x, y[0]) # 3 42
This question is about argument passing.
It is a big difference, whether you pass a mutable or an immutable data type.
The immutable integer in x gets copied to p1
and the change of p1 does not effect x
The mutable list in y gets referenced to p2
and the change of p2 effect y