Python Essentials 2: INTERMEDIATE


Module-3


                   

1 - 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)
a) it will print ex
b) it will print exex
c) it will print an empty line
d) it will raise an unhandled exception
Solution: a

2 - A data structure described as LIFO is actually a:
a) list
b) stack
c) heap
d) tree
Solution: b

3 - What will be the output of the following code? class A: A = 1 print(hasattr(A, 'A'))
a) 0
b) False
c) 1
d) True
Solution: d

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)
a) 2
b) The code will raise an AttributeError exception
c) 0
d) 1
Solution: b

5 - 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))
a) it will print 1
b) it will print False
c) it will print True
d) it will raise an exception
Solution: c

6 - What will be the result of executing the following code? class A: def_str_(self): return 'a' class B: def_str_(self): return 'b' class C(A, B): pass o = C() print(o)

a) it will print b
b) it will print c
c) it will print a
d) it will raise an exception
Solution: c

7 - What will be the result of executing the following code? class A: def a(self): print('a') class B: def a(self): self.a() o = C() o.c()

a) it will raise an exception
b) it will print a
c) it will print c
d) it will print b
Solution: d

8 - What will be the result of executing the following code? try: raise Exception(1,2,3) except Exception as e: print (len(e.args))

a) it will print 1
b) it will print 2
c) it will raise as unhandled exception
d) it will print 3
Solution: d

9 - 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)
a) 2
b) 3
c) 0
d) 1
Solution: b

10 - If the class’s constructor is declared as below, which one of the assignments is valid? class Class: def __init__(self): pass
a) object = Class
b) object = Class()
c) object = Class(self)
d) object = Class(object)
Solution: b

11 - 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)
a) it will raise an unhandled exception
b) it will print bcac
c) it will print acac
d) it will print bcbc
Solution: b

12 - If there is a superclass named A and a subclass named B, which one of the presented invocations should you put instead of the comment? class A: def __init__(self): self.a = 1 class B(A): def __init__(self): # put selected line here. self.b = 2 a) A.__init__(self)
b) __init__()
c) A.__init__()
d) A.__init__(1)
Solution: a

13 - What will be the result of executing the following code? class A: def__init__(self): pass a = A(1) print(hasattr(a,'A'))
a) 1
b) False
c) it will raise an exception
d) True
Solution: c

14 - What will be the result of executing the following code? class A: ​​​​ def__init__(self): return 'a' class B(A): def__init__(self): return 'b' class c(B): pass o = C() print(o)
a) it will print c
b) it will print b
c) it will print a
d) it will raise an exception
Solution: b

15 - 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))
a) 2
b) 1
c) 3
d) 0
Solution: b

16 - 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)
a) it will raise an exception
b) it will print 2
c) it will print an empty line
d) it will print 1
Solution: d