AP - Programming Essentials in Python Quizzes: Summary Test 2 Answers
AP - Programming Essentials in Python Quizzes: Summary Test 2 Answers
import math
print(dir(math))
an error message
a string containing the fully qualified name of the module
a list of all the entities residing in the math module
the number of all the entities residing in the math module
3. The compiled Python bytecode is stored in files having names
ending with:
py
pyb
pc
pyc
4. Assuming that all three files, a.py, b.py, and c.py reside in the
same folder, what will be the output produced by running the
c.py file?
# file a.py
print(“a”,end=”)
#file b.py
import a
print(“b”,end=”)
#file c.py
print(“c”,end=”)
import a
import b
cba
abc
bac
cab
5. What will be the output of the following code, located in file
p.py?
print(__name__)
p.py
main
__p.py__
__main__
6. The following statement
assert var != 0
will stop the program when var == 0
is erroneous
has no effect
will stop the program when var != 0
12. The following code prints:
x = “\\\\”
print(len(x))
2
1
3
the code will cause an error
13. The following code prints:
x = “\\\”
print(len(x))
3
the code will cause an error
1
2
14. The following code prints:
print(chr(ord(‘p’) + 2))
s
t
q
r
15. The following code:
print(float(“1.3”))
raises a ValueError exception
prints 13
prints 1,3
1.3
16. If the class’s constructor is declared as below, which one
of the assignments is invalid?
class Class:
def __init__(self,val=0):
pass
object = Class(None)
object = Class(1)
object = Class()
object = Class(1,2)
17. What will be output of the following code?
class A:
def __init__(self,v = 2):
self.v = v
def set(self,v = 1):
self.v += v
return self.v
a = A()
b = a
b.set()
print(a.v)
0
3
1
2
18. What will be output of the following code?
class A:
A = 1
def __init__(self):
self.a = 0
print(hasattr(A,’a’))
1
False
0
True
19. What will be the result of executing the following code?
class A:
pass
class B(A):
pass
class C(B):
pass
print(issubclass(A,C))
it will print False
it will print True
it will print 1
it will raise an exception
20. The sys.stderr stream is normally associated with:
the keyboard
a null device
the screen
the printer
21. What will be the effect of running the following code?
class A:
def __init__(self,v):
self.__a = v + 1
a = A(0)
print(a.__a)
it will print 1
it will print 2
it will raise an AttributeError exception
it will print 0
22. What will be the result of executing the following code?
class A:
def __init__(self):
pass
a = A(1)
print(hasattr(a,’A’))
it will print True
it will raise an exception
it will print False
it will print 1
23. What will be the result of executing the following code?
class A:
def a(self):
print(‘a’)
class B:
def a(self):
print(‘b’)
class C(B,A):
def c(self):
self.a()
o = C()
o.c()
it will print c
it will raise an exception
it will print b
it will print a
24. What will be the result of executing the following code?
try:
raise Exception(1,2,3)
except Exception as e:
print(len(e.args))
it will print 2
it will print 1
it will raise an unhandled exception
it will print 3
25. What will be the result of executing the following code?
def I(n):
s = ‘+’
for i in range(n):
s += s
yield s
for x in I(2):
print(x,end=”)
it will print ++
it will print ++++++
it will print +
it will print +++
26. What will be the result of executing the following code?
class I:
def __init__(self):
self.s = ‘abc’
self.i = 0
def __iter__(self):
return self
def __next__(self):
if self.i == len(self.s):
raise StopIteration
v = self.s[self.i]
self.i += 1
return v
for x in I():
print(x,end=”)
012
abc
cba
210
27. What will be the result of executing the following code?
def o(p):
def q():
return ‘*’ * p
return q
r = o(1)
s = o(2)
print(r() + s())