0% found this document useful (0 votes)
2 views4 pages

Sheet Oop

The document consists of a series of questions and answers related to object-oriented programming concepts in Python, focusing on objects, classes, inheritance, and method invocation. Key topics include the definition of objects, the role of constructors, the use of getattr() and setattr(), and different types of inheritance. Additionally, it addresses relationships between entities like university and professor, course and faculty, and house and door.

Uploaded by

salmaabo341
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Sheet Oop

The document consists of a series of questions and answers related to object-oriented programming concepts in Python, focusing on objects, classes, inheritance, and method invocation. Key topics include the definition of objects, the role of constructors, the use of getattr() and setattr(), and different types of inheritance. Additionally, it addresses relationships between entities like university and professor, course and faculty, and house and door.

Uploaded by

salmaabo341
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1. _____ represents an entity in the real world with its identity and behaviour.

a) A method
b) An object
c) A class
d) An operator
Answer: b
Explanation: An object represents an entity in the real world that can be distinctly identified. A class may
define an object.

2. _____ is used to create an object.


a) class
b) constructor
c) User-defined functions
d) In-built functions
Answer: b
Explanation: The values assigned by the constructor to the class members is used to create the object.

3. What is getattr() used for?


a) To access the attribute of the object
b) To delete an attribute
c) To check if an attribute exists or not
d) To set an attribute
Answer: a
Explanation: getattr(obj,name) is used to get the attribute of an object.

4- What will be the output of the following Python code?


class change:
def __init__(self, x, y, z):
self.a = x + y + z

x = change(1,2,3)
y = getattr(x, 'a')
setattr(x, 'a', y+1)
print(x.a)
a) 6
b) 7
c) Error
d) 0
Answer: b
Explanation: First, a=1+2+3=6. Then, after setattr() is invoked, x.a=6+1=7.

5. Which of the following statements is wrong about inheritance?


a) Protected members of a class can be inherited
b) The inheriting class is called a subclass
c) Private members of a class can be inherited and accessed
d) Inheritance is one of the features of OOP
Answer: c
Explanation: Any changes made to the private members of the class in the subclass aren’t reflected in
the original members.

6. Suppose B is a subclass of A, to invoke the __init__ method in A from B, what is the line of code you
should write?
a) A.__init__(self)
b) B.__init__(self)
c) A.__init__(B)
d) B.__init__(A)
Answer: a
Explanation: To invoke the __init__ method in A from B, either of the following should be written:
A.__init__(self) or super().__init__(self).

7- What type of inheritance is illustrated in the following Python code?

class A():
pass
class B():
pass
class C(A,B):
pass
a) Multi-level inheritance
b) Multiple inheritance
c) Hierarchical inheritance
d) Single-level inheritance
Answer: b
Explanation: In multiple inheritance, two or more subclasses are derived from the superclass as shown
in the above piece of code.

8- What type of inheritance is illustrated in the following Python code?


class A():
pass
class B(A):
pass
class C(B):
pass
a) Multi-level inheritance
b) Multiple inheritance
c) Hierarchical inheritance
d) Single-level inheritance
Answer: a
Explanation: In multi-level inheritance, a subclass derives from another class which itself is derived from
another class.

9. Which Of The Following Can Be Used To Invoke The __init__


Method In B From A, Where A Is A Subclass Of B?
A. super().__init__()
B. A and D
C. B.__init__()
D. B.__init__(self)
Answer. B

10. What Relationship Correctly Fits For University And Professor?


A. association
B. composition
C. inheritance
D. All of the above
Answer. B

Q-11. What Relationship Is Suited For Course And Faculty?


A. association
B. composition
C. inheritance
D. None of the above
Answer. A

Q-12. What Relationship Is Best Suited For House And Door?


A. association
B. composition
C. inheritance
D. All of the above
Answer. B

13- Which of the following statements is true?


a) A non-private method in a superclass can be overridden
b) A subclass method can be overridden by the superclass
c) A private method in a superclass can be overridden
d) Overriding isn’t possible in Python
Answer: a
Explanation: A public method in the base class can be overridden by the same named method in the
subclass.

You might also like