MCQs Relationship with classes
1. Do we’ve to use list() to perform one-many operation?
- FALSE
- TRUE
Ans: True
2. How can you perform many to many relationship?
- By using dictionary in each class
- By using list in each class
- By using tuple in each class
- By using any of the above
Ans: By using any of the above
3.What is the output of the following code snippet.
class A:
def __init__(self):
self.calcI(30)
print("i from A is", self.i)
def calcI(self, i):
self.i = 2 * i
class B(A):
def __init__(self):
super().__init__()
def calcI(self, i):
self.i = 3 * i
b = B()
- i from A is 90
- i from A is 60
- i from A is 0
- i from A is 30
Ans : i from A is 90
4. Which of the following defines unidirectional relationship
- By Using any object we can access other class object.
- None of the above
- By creating a list of objects of other class inside each class
- By using specific object only then you can access the other class object
Ans : By using specific object only then you can access the other class object
5.What will be the output?
class Person:
def __init__(self,a,b):
self.name=a
self.o=b
def display(self):
print”Name:”,self.name
self.o.display()
class Company:
def __init__(self,c):
self.company=c
def display(self):
print”Company:”,self.company
c=Company(“Amphisoft”)
p=Person(“Jaya”,c)
p.display()
Name: Jaya
Company: Amphisoft
Name: Jaya
Company: Amphisoft
Name: Amphisoft
Company: Jaya
Ans : Name: Jaya
Company: Amphisoft
6. What will be the output?
class Person:
def __init__(self,a,b):
self.name=a
self.o=b
def display(self):
print”Name:”,self.name
self.o.display()
class Company:
def __init__(self,c):
self.company=c
def display(self):
print”Company:”,self.company
c=Company(“Amphisoft”)
p=Person(“Jaya”,c)
- Name: Jaya Company: Amphisoft
- Name: Jaya
- Company: Amphisoft
- Complie time error
Ans : Company: Amphisoft
7.What is the output of the following code
class Person:
def __init__(self,a,b=None):
self.name=a
self.o=b
def display(self):
print("Name:",self.name)
print("Company:" , self.o.company)
class Company:
def __init__(self,c,d=None):
self.company=c
self.o=d
def display(self):
print("Name:" , self.o.name)
print("Company:",self.company)
print("Menu\n1.Person\n2.Company")
n=int(input("Enter you choice\n"))
if n==1:
c=Company("Amphisoft")
p=Person("Jaya",c)
p.display()
elif n==2:
p=Person("Jaya")
c=Company("Amphisoft",p)
c.display()
- It depends upon class
- If n=1 then Name: Jaya is printed
- Name: Jaya Company: Amphisoft
- If n=2 then Company: Amphisoft is printed
Ans: Name: Jaya Company: Amphisoft
8. Which of the following statements are correct?
A. A reference variable is an object.
B. A reference variable refers to an object.
C. An object may contain other objects.
D. An object can contain the references to other objects.
A and B
B and D
B and C
C and D
Ans : B and D
9.What is the output of the following code snippet.
class Person:
def __init__(self , id):
self.id = id
sam = Person(100)
sam.__dict__['age'] = 49
print(sam.age + len(sam.__dict__))
50
49
51
52
Ans : 51
10. Which of the following statements are correct about the given code snippet?
class A:
def __init__(self, i = 0):
self.i = i
class B(A):
def __init__(self, j = 0):
self.j = j
def main():
b = B()
print(b.i)
print(b.j)
main()
- Class B inherits A, but the data field “i” in A is not inherited.
- The data field “j” cannot be accessed by object b.
- Class B inherits A, thus automatically inherits all data fields in A.
- When you create an object of B, you have to pass an argument such as B(5).
Ans : Class B inherits A, but the data field “i” in A is not inherited.
11. What does it signify?
class Person:
def __init__(self,a,b = None):
self.name=a
self.listOfcompany=[]
self.listOfcompany.append(b)
def display(self):
print("Name:",self.name)
self.listOfcompany[0].display()
class Company:
def __init__(self,c):
self.company=c
def display(self):
print("Company:",self.company)
c=Company("Amphisoft")
p=Person("Jaya",c)
p.display()
- one-many
- one-one
- uni directional
- Many-Many
Ans : uni directional
12. What does it signify?
class Person:
def __init__(self,a,b):
self.name=a
self.listOfcompany=b
def display(self):
print("Name:",self.name)
for i in self.listOfcompany:
i.display()
class Company:
def __init__(self,c,d = None):
self.company=c
self.o=d
def display(self):
print("Company:",self.company)
l=[]
for i in range(n):
c=Company(input("Enter the company"))
l.append(c)
p=Person("Jaya",l)
p.display()
- one-many
- one-one
- uni directional
- Many-Many
Ans : one-many
13. What does it signify?
class City:
def __init__(self,name,state) :
self.__name = name
self.__state = state
def __str__(self):
s=str(self.__state).split()
if s[1]=="has":
return self.__name+" is in state "+str(s[0])
else:
return self.__name+" is in state "+str(s[0])+" "+str(s[1])
class State:
def __init__(self,name,city_list) :
self.__name = name
self.__city_list = city_list
@property
def city_list(self) :
return self.__city_list
@city_list.setter
def city_list(self,city_list) :
self.__city_list=val
@property
def name(self) :
return self.__name
def __str__(self) :
return self.__name+" has "+str(len(self.__city_list))+" cities"
state_list = []
state_list.append(State("Tamilnadu",[]))
state_list.append(State("Andhra",[]))
state_list.append(State("Karnataka",[]))
state_list.append(State("Kerala",[]))
print("Enter City Details")
city_created_list = []
choice = "Yes"
while choice == "Yes" :
city_name = input("Enter city name\n")
state_name = input("Enter state name\n")
state_found_flag = 0
for state in state_list :
if state_name == state.name :
city = City(city_name, state)
state.city_list.append(city)
city_created_list.append(city)
state_found_flag = 1
if state_found_flag == 0 :
state = State(state_name,[])
state_list.append(state)
city = City(city_name, state)
city_created_list.append(city)
state.city_list.append(city)
choice = input("Do you want to add another city? Type Yes / No\n")
print("\nCity Details\n")
for city in city_created_list :
print(city)
print("\nState Details\n")
for state in state_list :
print(state)
- uni directional
- one-many
- one-one
- Many-Many
Ans: one many
14. If n and m value is 10,What will be the output?
class Person:
def __init__(self,a,b = None):
self.name=a
self.listOfcompany=[]
def companyList(self,l):
for i in l:
self.listOfcompany.append(i)
def display(self):
print("Name:",self.name)
for i in self.listOfcompany :
print(i.company)
class Company:
def __init__(self,c,d = None):
self.company=c
self.listofPerson=[]
def personList(self,l):
for i in l:
self.listofPerson.append(i)
def display(self):
print("Company:",self.company)
for i in self.listofPerson:
i.display()
l=[]
l1=[]
print("Enter company Name")
for i in range(10):
c=Company(input())
l.append(c)
print("Enter Person name")
for i in range(10):
p=Person(input())
l1.append(p)
c.personList(l1)
p.companyList(l)
p.display()
- It will print 10 person details with the corresponding company details
- It will print the person name and 10 company details
- it will chech weither the person is working in any one of the company specified in the list
- it will print 10 person name along with 10 company names
Ans : It will print the person name and 10 company details
Statement:
Mr. Vishwas is a big lover of cars.
He has a dream to own a car of his reqiurements.
His requirements include few car and company related and few engine related.
Write a Python code to take the reqiurements of Vishwas and display the same.
Create following classes with the following attributes and methods.
Develop the relationship between the classes.
Initialize the constructor using __init__ in each class.
Class Name: Engine
Data type – Attribute : String – transmission , String – type
Method Name: start() and display()
Class Name: Cars
Data type – Attribute : String – companyname , String – colour , Engine – eng
Method Name: run() and display()
Class Name: Owner
Data type – Attribute : String – name , Cars – car
Method Name: display()
Answer the below questions by considering the above scenario
15. What is composition?
- Implies that the two objects are quite strongly linked
- One object can be thought of as belonging exclusively to the other object.
- Both a and b
- None of the mentioned.
Ans: Both a and b
16. Composition represents
- “Has a” relationship
- “Is a” relationship
- “Uses a” relationship
- None of the mentioned
Ans: “Has a” relationship
17. Which of the following combination of classes is an example for composition?
- Engine-Owner
- Owner- Cars
- Cars-Engine
- None of the mentioned
Ans: Cars-Engine
18. Aggregation represents
- “Has a” relationship
- “Is a” relationship
- “Uses a” relationship
- None of the mentioned
Ans: “Uses a” relationship
19. Which of the following combination of classes is an example for aggregation?
- Engine-Owner
- Owner- Cars
- Cars-Engine
- None of the mentioned
Ans: Owner- Cars
20. Where does the object of Engine class created?
class Engine:
def __init__(self,k,l):
self.transmission=k
self.type1=l
def start(self):
print"Engine working!!!"
class Cars:
def __init__(self,c,d,a,b):
self.companyName=c
self.type=d
self.eng=Engine(a,b)
def run(self):
self.eng.start()
print"Car is running!!!"
c= Cars('BMW','automatic gear','6 Transmission','Petrol')
c.run()
- in Engine class
- in Cars class
- in Main method
- None of the mentioned
Ans: in Cars class
21. What is the output of above code?
class Engine:
def __init__(self,k,l):
self.transmission=k
self.type1=l
def start(self):
print"Engine working!!!"
class Cars:
def __init__(self,c,d,a,b):
self.companyName=c
self.type=d
self.eng=Engine(a,b)
def run(self):
self.eng.start()
print"Car is running!!!"
c= Cars('BMW','automatic gear','6 Transmission','Petrol')
c.run()
- Engine working!!! Car is running!!!
- Car is running!!! Engine working!!!
- Car is running!!!
- Engine working!!!
Ans: Engine working!!! Car is running!!!
22. What is the type of relationship that exist between Owner class and Cars class?
- Composition
- Aggregation
- Both
- None of the mentioned
Happy Learning – If you require any further information, feel free to contact me.