HyperLearning AI - Introduction to Python

An introductory course to the Python 3 programming language, with a curriculum aligned to the Certified Associate in Python Programming (PCAP) examination syllabus (PCAP-31-02).
https://knowledgebase.hyperlearning.ai/courses/introduction-to-python

11. PCAP Practice Exam

https://knowledgebase.hyperlearning.ai/courses/introduction-to-python/modules/11/pcap-practice-exam

In this final module of our Introduction to Python course, we will consolidate everything that we have learnt by taking a practice Certified Associate in Python Programming (PCAP) examination paper.

Question 1

In [2]:
2 ** 3 ** 2 ** 1
Out[2]:
512

Question 2

In [5]:
print("Peter's sister's name's \"Anna\"")
print('Peter\'s sister\'s name\'s \"Anna\"')
Peter's sister's name's "Anna"
Peter's sister's name's "Anna"

Question 3

In [6]:
i = 250
while len(str(i)) > 72:
    i *= 2
else:
    i //= 2
print(i)
125

Question 4

In [7]:
n = 0
while n < 4:
    n += 1
    print(n, end=" ")
1 2 3 4 

Question 5

In [8]:
x = 0
y = 2
z = len("Python")
x = y > z
print(x)
print(type(x))
False
<class 'bool'>

Question 6

In [19]:
Val = 1
Val2 = 0
Val = Val ^ Val2
Val2 = Val ^ Val2
Val = Val ^ Val2
print(Val)
0

Question 7

In [11]:
z, y, x = 2, 1, 0
x, z = z, y
y = y - z
x, y, z = y, z, x
print(x, y, z)
0 1 2

Question 8

In [12]:
a = 0
b = a ** 0
if b < a + 1:
    c = 1
elif b == 1:
    c = 2
else:
    c = 3
print(a + b + c)
3

Question 9

In [13]:
i = 10
while i > 0 :
    i -= 3
    print("*")
    if i <= 3:
        break
else:
    print("*")
*
*
*

Question 10

In [14]:
# Example 1
for i in range(1, 4, 2):
    print("*")
*
*
In [15]:
# Example 2
for i in range(1, 4, 2):
    print("*", end="")
**
In [16]:
# Example 3
for i in range(1, 4, 2):
    print("*", end="**")
******
In [17]:
# Example 4
for i in range(1, 4, 2):
    print("*", end="**")
print("***")
*********
In [25]:
list(range(1, 4, 2))
Out[25]:
[1, 3]

Question 11

In [18]:
print('N/A')
N/A

Question 12

In [19]:
x = "20"
y = "30"
print(x > y)
False

Question 13

In [28]:
s = "Hello, Python!"
print(s[-14:15])
Hello, Python!
In [30]:
print(s[0:30])
Hello, Python!

Question 14

In [21]:
lst = ["A", "B", "C", 2, 4]
del lst[0:-2]
print(lst)
[2, 4]

Question 15

In [22]:
dict = { 'a': 1, 'b': 2, 'c': 3 }
for item in dict:
    print(item)
a
b
c

Question 16

In [23]:
s = 'python'
for i in range(len(s)):
    i = s[i].upper()
print(s, end="")
python

Question 17

In [24]:
lst = [i // i for i in range(0,4)]
sum = 0
for n in lst:
    sum += n
print(sum)
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-24-129bbd05e72e> in <module>
----> 1 lst = [i // i for i in range(0,4)]
      2 sum = 0
      3 for n in lst:
      4     sum += n
      5 print(sum)

<ipython-input-24-129bbd05e72e> in <listcomp>(.0)
----> 1 lst = [i // i for i in range(0,4)]
      2 sum = 0
      3 for n in lst:
      4     sum += n
      5 print(sum)

ZeroDivisionError: integer division or modulo by zero

Question 18

In [35]:
lst = [[c for c in range(r)] for r in range(3)]
for x in lst:
    for y in x:
        if y < 2:
            print('*', end='')
***

Question 19

In [39]:
lst = [2 ** x for x in range(0, 11)]
print(lst[-1])
1024

Question 20

In [41]:
lst1 = "12,34"
lst2 = lst1.split(',')
print(len(lst1) < len(lst2))
False

Question 21

In [32]:
def fun(a, b=0, c=5, d=1):
    return a ** b ** c

print(fun(b=2, a=2, c=3))
256

Question 22

In [48]:
x = 5
f = lambda x: 1 + 2
print(f(x))
3

Question 23

In [34]:
from math import pi as xyz
print(pi)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-34-508ddb7707ef> in <module>
      1 from math import pi as xyz
----> 2 print(pi)

NameError: name 'pi' is not defined

Question 24

In [35]:
print('N/A')
N/A

Question 25

In [36]:
from random import randint
for i in range(10):
    print(random(1, 5))
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-36-6f5442a055ae> in <module>
      1 from random import randint
      2 for i in range(10):
----> 3     print(random(1, 5))

NameError: name 'random' is not defined

Question 26

In [37]:
x = 1 # line 1
def a(x): # line 2
    return 2 * x
# line 3
x = 2 + a(x) # line 4
print(a(x)) # line 5
8

Question 27

In [38]:
a = 'hello' # line 1
def x(a,b): # line 2
    z = a[0] # line 3
    return z # line 4
print(x(a)) # line 5
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-38-52e820b3bd89> in <module>
      3     z = a[0] # line 3
      4     return z # line 4
----> 5 print(x(a)) # line 5

TypeError: x() missing 1 required positional argument: 'b'

Question 28

In [39]:
s = 'SPAM'
def f(x):
    return s + 'MAPS'
print(f(s))
SPAMMAPS

Question 29

In [40]:
print('N/A')
N/A

Question 30

In [41]:
def gen():
    lst = range(5)
    for i in lst:
        yield i*i

for i in gen():
    print(i, end="")
014916

Question 31

In [42]:
print('N/A')
N/A

Question 32

In [43]:
print('N/A')
N/A

Question 33

In [44]:
print('N/A')
N/A

Question 34

In [45]:
# Example 1
x = 1
y = 0
z = x%y
print(z)
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-45-0411fd02421d> in <module>
      2 x = 1
      3 y = 0
----> 4 z = x%y
      5 print(z)

ZeroDivisionError: integer division or modulo by zero
In [46]:
# Example 2
x = 1
y = 0
z = x/y
print(z)
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-46-0581dd6c300e> in <module>
      2 x = 1
      3 y = 0
----> 4 z = x/y
      5 print(z)

ZeroDivisionError: division by zero

Question 35

In [47]:
x = 0
try:
    print(x)
    print(1 / x)
except ZeroDivisionError:
    print("ERROR MESSAGE")
finally:
    print(x + 1)
print(x + 2)
0
ERROR MESSAGE
1
2

Question 36

In [49]:
class A:
    def a(self):
        print("A", end='')

class B(A):
    def a(self):
        print("B", end='')

class C(B):
    def b(self):
        print("B", end='')

a = A()
b = B()
c = C()
a.a()
b.a()
c.b()
ABB

Question 37

In [50]:
try:
    print("Hello")
    raise Exception
    print(1/0)
except Exception as e:
    print(e)
Hello

Question 38

In [51]:
# Example 1
class CriticalError(Exception):
    def __init__(self, message='ERROR MESSAGE A'):
        Exception.__init__(self, message)

raise CriticalError
raise CriticalError("ERROR MESSAGE B")
---------------------------------------------------------------------------
CriticalError                             Traceback (most recent call last)
<ipython-input-51-4a2de5d705fc> in <module>
      4         Exception.__init__(self, message)
      5 
----> 6 raise CriticalError
      7 raise CriticalError("ERROR MESSAGE B")

CriticalError: ERROR MESSAGE A
In [52]:
# Example 2
class CriticalError(Exception):
    def __init__(self, message='ERROR MESSAGE A'):
        Exception.__init__(self, message)

raise CriticalError("ERROR MESSAGE B")
---------------------------------------------------------------------------
CriticalError                             Traceback (most recent call last)
<ipython-input-52-d33b9c4da8e2> in <module>
      4         Exception.__init__(self, message)
      5 
----> 6 raise CriticalError("ERROR MESSAGE B")

CriticalError: ERROR MESSAGE B

Question 39

In [ ]:
file = open(test.txt)
print(file.readlines())
file.close()

Question 40

In [ ]:
f = open("file.txt", "w")
f.close()