What will be the output of the following Python code? >>> class A: pass >>> class B(A): pass >>> obj=B() >>> isinstance(obj,A)
1.True
2.False
3.Wrong syntax for isinstance() method
4.Invalid method for classes
What will be the output of the following Python code? class A: def __init__(self, x= 1): self.x = x class der(A): def __init__(self,y = 2): super().__init__() self.y = y def main(): obj = der() print(obj.x, obj.y) main()
1. Error, the syntax of the invoking method is wrong
2. The program runs fine but nothing is printed
3. 1 0
4.1 2
What will be the output of the following Python code? class Demo: def __init__(self): self.a = 1 self.__b = 1 def get(self): return self.__b obj = Demo() obj.a=45 print(obj.a)
1.The program runs properly and prints 45
2.The program has an error because the value of members of a class can’t be changed from outside the class
3. The program runs properly and prints 1
4.The program has an error because the value of members outside a class can only be changed as self.a=45
Which of the following statements is true?
1. The __new__() method automatically invokes the __init__ method
2.The __init__ method is defined in the object class
3. The __eq(other) method is defined in the object class
4.The __repr__() method is defined in the object class
Which of these is a private data field? def Demo: def __init__(self): __a = 1 self.__b = 1 self.__c__ = 1 __d__= 1
1. __a
2.__b
3.__c__
4.__d__
Can one block of except statements handle multiple exception?
1.yes, like except TypeError, SyntaxError [,…]
2. yes, like except [TypeError, SyntaxError]
3.no
4.none of the mentioned
hat will be the output of the following Python code? class A: def __init__(self): self.multiply(15) print(self.i) def multiply(self, i): self.i = 4 * i; class B(A): def __init__(self): super().__init__() def multiply(self, i): self.i = 2 * i; obj = B()
1.15
2. 60
3. An exception is thrown
4.30
hat will be the output of the following Python code? class Demo: def __check(self): return " Demo's check " def display(self): print(self.check()) class Demo_Derived(Demo): def __check(self): return " Derived's check " Demo().display() Demo_Derived().display()
1.Demo’s check Derived’s check
2.Demo’s check Demo’s check
3.Derived’s check Demo’s check
4.Syntax error
How many except statements can a try-except block have?
1. zero
2.one
3.more than one
4.more than zero
Is the following Python code valid? try: # Do something except: # Do something else: # Do something
1. no, there is no such thing as else
2.no, else cannot be used with except
3.no, else must come before except
4.yes
Is the following Python code valid? try: # Do something except: # Do something finally: # Do something
1.no, there is no such thing as finally
2.no, finally cannot be used with except
3.no, finally must come before except
4.yes
Methods of a class that provide access to private members of the class are called as ______ and ______
1.getters/setters
2.__repr__/__str__
3.user-defined functions/in-built functions
4.__init__/__del__
What does built-in function help do in context of classes?
1. Determines the object name of any value
2.Determines the class identifiers of any value
3.Determines class description of any built-in type
4.Determines class description of any user-defined built-in type
What does built-in function type do in context of classes?
1.Determines the object name of any value
2.Determines the class name of any value
3.Determines class description of any value
4.Determines the file name of any value
What does single-level inheritance mean?
1.A subclass derives from a class which in turn derives from another class
2.A single superclass inherits from multiple subclasses
3. A single subclass derives from a single superclass
4.Multiple base classes inherit a single derived class
What happens when ‘1’ == 1 is executed?
1.we get a True
2.we get a False
3. an TypeError occurs
4.a ValueError occurs
What is the biggest reason for the use of polymorphism?
1. It allows the programmer to think at a more abstract level
2. There is less program code to write
3. The program will have a more elegant design and will be easier to maintain and update
4.Program code takes up less space
What is the use of duck typing?
1.More restriction on the type values that can be passed to a given method
2.No restriction on the type values that can be passed to a given method
3.Less restriction on the type values that can be passed to a given method
4.Makes the program code smaller
What type of inheritance is illustrated in the following Python code? class A(): pass class B(A): pass class C(B): pass
1.Multi-level inheritance
2.Multiple inheritance
3.Hierarchical inheritance
4.Single-level inheritance
What type of inheritance is illustrated in the following Python code? class A(): pass class B(): pass class C(A,B): pass
1. Multi-level inheritance
2.Multiple inheritance
3.Hierarchical inheritance
4.Single-level inheritance
What will be the output of the following Python code? class student: def __init__(self): self.marks = 97 self.__cgpa = 8.7 def display(self): print(self.marks) obj=student() print(obj._student__cgpa)
1.The program runs fine and 8.7 is printed
2. Error because private class members can’t be accessed
3.Error because the proper syntax for name mangling hasn’t been implemented
4.he program runs fine but nothing is printed
What will be the output of the following Python code? class A: def one(self): return self.two() def two(self): return 'A' class B(A): def two(self): return 'B' obj1=A() obj2=B() print(obj1.two(),obj2.two())
1. A A
2. A B
3.B B
4.An exception is thrown
What will be the output of the following Python code? class A: def one(self): return self.two() def two(self): return 'A' class B(A): def two(self): return 'B' obj2=B() print(obj2.two())
1.A
2.An exception is thrown
3.A B
4.B
What will be the output of the following Python code? class A: def __init__(self): self.multiply(15) def multiply(self, i): self.i = 4 * i; class B(A): def __init__(self): super().__init__() print(self.i) def multiply(self, i): self.i = 2 * i; obj = B()
1. 15
2.30
3.An exception is thrown
4.60
What will be the output of the following Python code? class A: def __init__(self): self._x = 5 class B(A): def display(self): print(self._x) def main(): obj = B() obj.display() main()
1.Error, invalid syntax for object declaration
2.Nothing is printed
3.5
4.Error, private class member can’t be accessed in a subclass
What will be the output of the following Python code? class A: def __init__(self): self.__x = 1 class B(A): def display(self): print(self.__x) def main(): obj = B() obj.display() main()
1.1
2. 0
3.Error, invalid syntax for object declaration
4.Error, private class member can’t be accessed in a subclass
What will be the output of the following Python code? class A: def __init__(self, x, y): self.x = x self.y = y def __str__(self): return 1 def __eq__(self, other): return self.x * self.y == other.x * other.y obj1 = A(5, 2) obj2 = A(2, 5) print(obj1 == obj2)
1.False
2.1
3. True
4.An exception is thrown
What will be the output of the following Python code? class A: def __init__(self,x): self.x = x def count(self,x): self.x = self.x+1 class B(A): def __init__(self, y=0): A.__init__(self, 3) self.y = y def count(self): self.y += 1 def main(): obj = B() obj.count() print(obj.x, obj.y) main()
1.3 0
2.3 1
3.0 1
4.An exception in thrown
What will be the output of the following Python code? class A: def __init__(self,x=3): self._x = x class B(A): def __init__(self): super().__init__(5) def display(self): print(self._x) def main(): obj = B() obj.display() main()
1.5
2.Error, class member x has two values
3.3
4.Error, protected class member can’t be accessed in a subclass
What will be the output of the following Python code? class A: def __repr__(self): return "1" class B(A): def __repr__(self): return "2" class C(B): def __repr__(self): return "3" o1 = A() o2 = B() o3 = C() print(obj1, obj2, obj3)
1. 1 1 1
2. 1 2 3
3.1’ ‘1’ ‘1’
4.An exception is thrown
What will be the output of the following Python code? class A: def __str__(self): return '1' class B(A): def __init__(self): super().__init__() class C(B): def __init__(self): super().__init__() def main(): obj1 = B() obj2 = A() obj3 = C() print(obj1, obj2,obj3) main()
1.1 1 1
2.1 2 3
3.‘1’ ‘1’ ‘1’
4. An exception is thrown
What will be the output of the following Python code? class Demo: def __init__(self): self.a = 1 self.__b = 1 def get(self): return self.__b obj = Demo() print(obj.get())
1.The program has an error because there isn’t any function to return self.a
2.The program has an error because b is private and display(self) is returning a private member
3.The program has an error because b is private and hence can’t be printed
4. The program runs fine and 1 is printed
What will be the output of the following Python code? class Demo: def check(self): return " Demo's check " def display(self): print(self.check()) class Demo_Derived(Demo): def check(self): return " Derived's check " Demo().display() Demo_Derived().display()
1.Demo’s check Derived’s check
2.Demo’s check Demo’s check
3.Derived’s check Demo’s check
4.Syntax error
What will be the output of the following Python code? class Demo: def __init__(self): self.a = 1 self.__b = 1 def display(self): return self.__b obj = Demo() print(obj.__b)
1. The program has an error because there isn’t any function to return self.a
2. The program has an error because b is private and display(self) is returning a private member
3. The program has an error because b is private and hence can’t be printed
4.The program runs fine and 1 is printed
What will be the output of the following Python code? class Demo: def __init__(self): self.x = 1 def change(self): self.x = 10 class Demo_derived(Demo): def change(self): self.x=self.x+1 return self.x def main(): obj = Demo_derived() print(obj.change()) main()
1.11
2. 2
3.1
4.An exception is thrown
What will be the output of the following Python code? class fruits: def __init__(self): self.price = 100 self.__bags = 5 def display(self): print(self.__bags) obj=fruits() obj.display()
1.The program has an error because display() is trying to print a private class member
2.The program runs fine but nothing is printed
3.The program runs fine and 5 is printed
4. The program has an error because display() can’t be accessed
What will be the output of the following Python code? class objects: def __init__(self): self.colour = None self._shape = "Circle" def display(self, s): self._shape = s obj=objects() print(obj._objects_shape)
1.The program runs fine because name mangling has been properly implemented
2.Error because the member shape is a protected member
3.Error because the proper syntax for name mangling hasn’t been implemented
4. Error because the member shape is a private member
What will be the output of the following Python code? def foo(): try: print(1) finally: print(2) foo()
1. 1 2
2.1
3.2
4.none of the mentioned
What will be the output of the following Python code? def foo(): try: return 1 finally: return 2 k = foo() print(k)
1.1
2.2
3.3
4. error, there is more than one return statement in a single try-finally block
What will be the output of the following Python code? try: if '1' != 1: raise "someError" else: print("someError has not occurred") except "someError": print ("someError has occurred")
1.someError has occurred
2. someError has not occurred
3.invalid code
4.none of the mentioned
What will be the output of the following Python code? x=10 y=8 assert x>y, 'X too small'
1.Assertion Error
2. 10 8
3. No output
4.108
When is the finally block executed?
1. when there is no exception
2. when there is an exception
3. only if some condition that has been specified is satisfied
4.always
When will the else part of try-except-else be executed?
1.always
2.when an exception occurs
3. when no exception occurs
4.when an exception occurs in to except block
Which of the following best describes polymorphism?
1. Ability of a class to derive members of another class as a part of its own definition
2. Means of bundling instance variables and methods in order to restrict access to certain class members
3.Focuses on variables and passing of variables to functions
4.Allows for objects of different types and behaviour to be treated as the same general type
Which of the following is false about protected class members?
1.They begin with one underscore
2.They can be accessed by subclasses
3.They can be accessed by name mangling method
4. They can be accessed within a class
Which of the following is not a type of inheritance?
1.Double-level
2.Multi-level
3.Single-level
4.Multiple
Which of the following is the most suitable definition for encapsulation?
1. Ability of a class to derive members of another class as a part of its own definition
2.Means of bundling instance variables and methods in order to restrict access to certain class members
3. Focuses on variables and passing of variables to functions
4.Allows for implementation of elegant software that is well designed and easily modified
Which of the following statements is true?
1.A non-private method in a superclass can be overridden
2.A subclass method can be overridden by the superclass
3. A private method in a superclass can be overridden
4.Overriding isn’t possible in Python
Which of the following statements isn’t true?
1.A non-private method in a superclass can be overridde
2.A derived class is a subset of superclass
3.The value of a private variable in the superclass can be changed in the subclass
4.When invoking the constructor from a subclass, the constructor of superclass is automatically invoked
Which of these is not a fundamental features of OOP?
1.Encapsulation
2.Inheritance
3. Instantiation
4.Polymorphism