1. Find the value of a, b, c in the given code :
a, b = c = 2 + 2, "Hello"
a. a=4, ‘Hello’, b=4, ‘Hello’, c=4, ‘Hello’
b. a=2, b=4, ‘Hello’, c=4, ‘Hello’
c. a=4, b= ‘Hello’, c=4, ‘Hello’
d. a=4, b= ‘Hello’, c=NULL
View Answer
Ans. c
Explanation:
a = 2+2
b = “Hello”
c = 4, “Hello”
2. Find the output :
a = 8.3
b = 2
print(a//b)
a. 4.15
b. 4
c. 4.1
d. 4.0
View Answer
Ans. d
Explanation : ‘/’ would have given 4.15 as the answer. However, ‘//’ operator performs floor division.
3. Find the output :
s1={3, 4}
s2={1, 2}
s3=set()
i=0
j=0
for i in s1:
for j in s2:
s3.add((i,j))
i+=1
j+=1
print(s3)
a. {(3, 4), (1, 2)}
b. Error
c. {(4, 2), (3, 1), (4, 1), (5, 2)}
d. {(3, 1), (4, 2)}
View Answer
Ans. c
Explanation : Above code runs for
i = 3 j = 1
i = 4 j = 2
i = 4 j = 1
i = 5 j = 2
{(3, 1), (4, 1), (4, 2), (5, 2)}
4. Find the output :
class A():
def disp(self):
print("A disp()")
class B(A):
pass
obj = B()
obj.disp()
a. Invalid syntax for inheritance
b. Error because when object is created, argument must be passed
c. Nothing is printed
d. A disp()
View Answer
Ans. d
Explanation : We created an object of class B that inherits class A and so disp() method (of class A) becomes visible with reference to object obj. Option d is correct
5. Consider the following code :
class Customer:
def __init__(self,cust_id, cust_name):
self.__cust_id = cust_id
self.__cust_name = cust_name
self.__bill_amount = 0.0
class RegularCustomer(Customer):
def __init__(self,cust_id, cust_name):
self.__cust_id = cust_id
self.__cust_name = cust_name
self.__bill_amount = 0.0
self.__discount = 5
Identify the OOP principle(s) that has/have been implemented in the above code :
a. Only Encapsulation
b. Only Inheritance
c. Both Encapsulation and Inheritance
d. Only Polymorphism
View Answer
Ans. c
Explanation : Clearly, RegularCustomer class inherits Customer class, perfect example of Inheritance. Also, both classes are having their attributes which are encapsulated with their respective class, hence, Encapsulation.