Last Updated on October 18, 2019 by Admin
PCAP – Programming Essentials in Python Quizzes Module 5 Test Answers
-
A data structure described as LIFO is actually a:
- list
- heap
- tree
- stack
-
If the class’s constructor is declared as below, which one of the assignments is valid?
class Class:
def __init__(self):
pass
- object = Class(object)
- object = Class(self)
- object = Class
- object = Class()
-
If there is a superclass named A and a subclass named B, which one of the presented invocations should you put instead of a comment?
class A:
def __init__(self):
self.a = 1
class B:
def __init__(self):
# put selected line here
self.a = 2
- __init__()
- A.__init__()
- A.__init__(self)
- A.__init__(1)
-
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 0
- it will print 2
- it will print 1
- it will raise an AttributeError exception
-
What will be the output of the following code?
class A:
def __init__(self,v = 1):
self.v = v
def set(self,v):
self.v = v
return v
a = A()
print(a.set(a.v + 1))
- 3
- 0
- 1
- 2
-
What will be the output of the following code?
class A:
X = 0
def __init__(self,v = 0):
self.Y = v
A.X += v
a = A()
b = A(1)
c = A(2)
print(c.X)
- 0
- 2
- 3
- 1
-
What will be the output of the following code?
class A:
A = 1
print(hasattr(A,’A’))
- 0
- False
- 1
- True
-
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 False
- it will print 1
- it will print True
- it will raise an exception
-
What will be the result of executing the following code?
class A:
def __str__(self):
return ‘a’
class B(A):
def __str__(self):
return ‘b’
class C(B):
pass
o = C()
print(o)
- it will raise an exception
- it will print a
- it will print c
- it will print b
-
What will be the result of executing the following code?
class A:
pass
class B(A):
pass
class C(B):
pass
print(issubclass(C,A))
- it will raise an exception
- it will print True
- it will print 1
- it will print False
-
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 print a
- it will raise an exception
- it will print b
-
What will be the result of executing the following code?
class A:
def __str__(self):
return ‘a’
class B(A):
def __str__(self):
return ‘b’
class C(B):
pass
o = C()
print(o)
- it will print b
- it will raise an exception
- it will print a
- it will print c
-
What will be the result of executing the following code?
class A:
v = 2
class B(A):
v = 1
class C(B):
pass
o = C()
print(o.v)
- it will print an empty line
- it will print 2
- it will raise an exception
- it will print 1
-
What will be the result of executing the following code?
def f(x):
try:
x = x / x
except:
print(“a”,end=”)
else:
print(“b”,end=”)
finally:
print(“c”,end=”)
f(1)
f(0)
- it will print bcbc
- it will print bcac
- it will print acac
- it will raise an unhandled exception ]
-
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
-
What will be the result of executing the following code?
class Ex(Exception):
def __init__(self,msg):
Exception.__init__(self,msg + msg)
self.args = (msg,)
try:
raise Ex(‘ex’)
except Ex as e:
print(e)
except Exception as e:
print(e)
- it will raise an unhandled exception
- it will print an empty line
- it will print exex
- it will print ex
-
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=”)
- it will print cba
- it will print 210
- it will print 012
- it will print abc
-
What will be the result of executing the following code?
def I():
s = ‘abcdef’
for c in s[::2]:
yield c
for x in I():
print(x,end=”)
- it will print an empty line
- it will print bdf
- it will print abcdef
- it will print ace
-
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 ++
-
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())
- it will print ***
- it will print **
- it will print ****
- it will print *
-
When a file is opened in read mode, it:
- it must exist (an exception will be raised otherwise)
- it cannot exist (it has to be created every time)
- it will be deleted if it exists
- it doesn’t have to exist (it will be created if absent)
-
If you want to open a text file in append mode, you would use the following mode string:
- t+a
- at
- a+t
- at+
-
The sys.stdin stream is normally associated with a:
- null device
- keyboard
- printer
- screen
-
The strerror function comes from the OS module, and it’s designed to:
- raise a string exception
- translate an error description from one language to another
- translate an error description into an error number
- translate an error number into an error description
-
If s is a stream opened in read mode, the following line
q = s.read(1)
will:
- read 1 buffer from the stream
- read 1 kilobyte from the stream
- read 1 character from the stream
- read 1 line from the stream
-
How does the readline() method react when the end‑of‑file occurs?
- it returns eof
- it returns ‑1
- it returns an empty string
- it raises an exception
-
The readlines() method returns a:
- list
- dictionary
- tuple
- string
-
Assuming that the open() invocation has gone successfully, the following snippet will:
for x in open(‘file’,’rt’):
print(x)
- read the file line by line
- read the file character by character
- cause an exception
- read the whole file at once
-
The byte array class can create objects which are designed to:
- build arrays 1 byte in size
- convert tuples into lists
- convert lists into tuples
- store amorphic data organized in bytes
-
If you want to fill a byte array with data read in from a stream, you use the:
- read() method
- readinto() method
- readfrom() method
- readbytes() method