PCAP – Programming Essentials in Python Quizzes Module 5 Test Answers

  1. A data structure described as LIFO is actually a:

    • list
    • heap
    • tree
    • stack
  2. 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()
  3. 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)
  4. 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
  5. 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
  6. 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
  7. What will be the output of the following code?

    class A:

    A = 1

    print(hasattr(A,’A’))

    • 0
    • False
    • 1
    • True
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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 ]
  15. 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
  16. 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
  17. 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
  18. 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
  19. 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 ++
  20. 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 *
  21. 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)
  22. If you want to open a text file in append mode, you would use the following mode string:

    • t+a
    • at
    • a+t
    • at+
  23. The sys.stdin stream is normally associated with a:

    • null device
    • keyboard
    • printer
    • screen
  24. 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
  25. 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
  26. 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
  27. The readlines() method returns a:

    • list
    • dictionary
    • tuple
    • string
  28. 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
  29. 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
  30. 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

Subscribe to YouTube Channel