Oop
Oop
Oop
Polymorphism is the ability of objects of different classes to be treated as if they are of the same
class. This allows code to be written in a more general way, which can make it more flexible and
adaptable to changing requirements.
Inheritance enables polymorphism by allowing subclasses to inherit attributes and methods from
a superclass, and by allowing subclasses to override methods with their own implementation.
3. Which of the following best describes encapsulation in object-oriented programming?
A. The process of hiding implementation details of a class from the outside world.
B. The ability of a class to have multiple methods with the same name but different
parameters.
C. The process of creating a new class based on an existing class, inheriting its
attributes andmethods.
D. The ability of objects of different classes to be treated as if they are of the same
class.
Answer: A. The process of hiding implementation details of a class from the outside
world. Explanation:
Encapsulation is the process of hiding implementation details of a class from the
outside world. This is achieved through access modifiers like private, protected, and
public, which control the visibility and accessibility of class attributes and methods.
Encapsulation is an important principle of object-oriented programming that helps to
prevent unwanted access and modification of class attributes and methods.
Option B describes overloading,
option C describes inheritance, and
option D describes polymorphism.
4 .Which of the following best describes the purpose of interfaces in object-oriented
programming?
A. To define a set of methods that must be implemented by a class.
B. To allow a class to inherit attributes and methods from another class.
C. To enable objects of different classes to be treated as if they are of the same class.
D. To enable polymorphism by allowing subclasses to override methods with their
own implementation.
Answer: A. To define a set of methods that must be implemented by a class.
Interfaces are often used in conjunction with inheritance to enable polymorphism and code reuse.
Option B describes inheritance,
5. Which of the following best describes the purpose of abstract classes in object- oriented
programming?
C. To enable objects of different classes to be treated as if they are of the same class.
D. To provide a base class that cannot be instantiated, but can be inherited by subclasses.
Answer: D. To provide a base class that cannot be instantiated, but can be inherited by
subclasses.
Abstract classes provide a base implementation of attributes and methods that can be shared by
multiple subclasses, but cannot be used directly. Abstract classes are often used in conjunction
with interfaces to enable polymorphism and code reuse.
A.Tocreateanewinstanceofaclass.
B.Toprovideawaytoaccessclassattributes.
C.Toprovideawaytomodify classattributes.
Answer: A. To create a new instance of a class. Explanation: Constructors are special methods in
object-oriented programming that are used to create a new instance of a class. Constructors are
called when a new object is created, and are responsible for initializing the object's attributes and
setting its initial state. Constructors can take parameters to allow for customization of the object
at creation time.
Options B, C, and D are incorrect because they describe the purpose of getter and setter methods
and regular methods, respectively.
7. Which of the following best describes the difference between a superclass and a subclass in
object-oriented programming?
Option A is incorrect because a superclass cannot inherit attributes and methods from a subclass.
Options C and D are incorrect because they describe the relationship between more specific and
more generic classes, which is not related to the superclass-subclass relationship.
8.Which of the following best describes the difference between private and protected access
modifiers in object-oriented programming?
A. Private attributes and methods can be accessed by any class in the same package, while
protected attributes and methods can only be accessed by the class that defines them.
B. Private attributes and methods can be accessed by any class, while protected attributes and
methods can only be accessed by subclasses and classes in the same package.
C. Private attributes and methods can only be accessed by the class that defines them, while
protected attributes and methods can be accessed by any class.
D. Private attributes and methods can only be accessed by subclasses, while protected attributes
and methods can be accessed by any class.
Answer: B. Private attributes and methods can be accessed by any class, while protected
attributes and methods can only be accessed by subclasses and classes in the same package.
Explanation: In object-oriented programming, access modifiers are used to control the visibility
and accessibility of attributes and methods. Private attributes and methods can only be accessed
by the class that defines them, while protected attributes and methods can be accessed by
subclasses and classes in the same package.
Option A is incorrect because private attributes and methods cannot be accessed by any class
outside the defining class, regardless of whether they are in the same package or not.
Option C is incorrect because protected attributes and methods can be accessed by subclasses
and classes in the same package, not by any class.
Option D is incorrect because private attributes and methods cannot be accessed by subclasses or
any other class outside the defining class.
9.Which of the following best describes the difference between method overloading and method
overriding in object-oriented programming?
B. Method overloading is when a class has multiple methods with the same name but different
parameters, while method overriding is when a subclass provides its own
implementationofasuperclassmethod.
C. Method overloading is when a subclass inherits a method from a superclass, while method
overriding is when a subclass defines a new method with the same name as a superclassmethod.
D. Method overloading and method overriding are the same thing.
Answer: B. Method overloading is when a class has multiple methods with the same name but
different parameters, while method overriding is when a subclass provides its own
implementation of a superclass method.
Explanation: Method overloading and method overriding are two different concepts in object-
oriented programming.
Method overloading is when a class has multiple methods with the same name but different
parameters, which enables the same method name to be used for different purposes. Method
overriding, on the other hand, is when a subclass provides its own
implementation of a superclass method with the same name and parameters, which enables the
subclass to customize or extend the behavior of the superclass method.
Therefore, option B is the correct answer.
Option D is incorrect because method overloading and method overriding are two different
concepts.
10. Which of the following best describes the purpose of the "this" keyword in object- oriented
programming?
A.Torefertothecurrentinstanceofaclass.
B.Torefertothesuperclassofaclass.
Answer: A. To refer to the current instance of a class. Explanation: The "this" keyword in object-
oriented programming is used to refer to the current instance of a class. It is often used to
disambiguate between class attributes and local variables with the same name, or to pass the
current object as an argument to a method.
Option B is incorrect because the "super" keyword is used to refer to the superclass of a class.
Option C is incorrect because the "this" keyword is not used to refer to a static method of a class.
Option D is incorrect because the "this" keyword is used to refer to the current instance of a
class, not a method of a different class.