Oops-questions

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Questions on OOPs

1. What is OOPs?
It is a programming paradigm that is used to develop software applications by creating objects that
interact with each other.

In simple terms, OOP is a way of writing software that models real-world objects, such as people, cars, or
buildings, as software objects with specific properties and behaviour.

The main idea behind OOP is to create reusable code that can be modified and extended with ease. It
allows programmers to write code that is easier to read, understand, and maintain.

2. What are the key principles of OOPs?


It is one of the most asked interview questions on OOPs concepts. You must know that there are four
principles of OOP using which developers can write code more efficiently and maintain it with ease.

a) Encapsulation
It is used to hide the internal details of an object and its behaviour from the outside world.

This allows objects to be used and manipulated without revealing their internal workings.
Encapsulation is achieved by defining the object's attributes as private or protected, and providing
public methods to access and modify those attributes.

b) Inheritance
It is the process of creating new classes from existing classes. It allows a new class to inherit properties
and behaviour from an existing class.

The existing class is known as the superclass or parent class. The new class is known as the subclass or
child class.

c) Polymorphism
It is the ability of an object to take on multiple forms. It allows different objects to be used
interchangeably, even though they may have different implementations. Polymorphism can be
achieved through method overloading and method overriding.

d) Abstraction
It is the process of simplifying complex systems by modelling them at a high level. In OOP, abstraction
is achieved by creating abstract classes and interfaces that define the methods that must be
implemented by their subclasses.

This allows the programmer to focus on the essential features of the system, while ignoring the
irrelevant details.

3. What is a class in OOPs?


It is a blueprint or a template for constructing objects. It is used to define a set of properties and
methods that will be present in an object belonging to that class.

It is essentially a user-defined data type that encapsulates data (in the form of fields or properties) and
behaviour (in the form of methods). The fields or properties define the characteristics of the object, while
the methods define what the object can do.

Example of class
Let's consider a class called "Car". This class may have fields such as "make", "model", "year", and
"color", which define the characteristics of a car object.

It may also have methods such as "start", "stop", "accelerate", and "brake", which define the behaviour of
a car object.

Once a class has been defined, it can be used to create multiple instances or objects of that class. Each
object will have its own set of values for the fields or properties, and will be able to perform the
methods defined by the class.

4. What is an object in OOPs?


An object is an instance of a class. It is a concrete entity that can be created based on a class blueprint.

When an object is created, it has its own unique set of properties and methods based on the definition
of its class. These properties and methods can be accessed and manipulated through the object's public
interface, which consists of its public methods and properties.

Example of object

Consider the class "Car". An object of the Car class can be created by instantiating the class, like this:

Car myCar = new Car();

In this case, myCar is an object of the Car class. It has its own set of properties (such as make, model,
year, and color) and methods (such as start, stop, accelerate, and brake) that are defined by the Car
class. These properties and methods can be accessed and manipulated using dot notation, like this:

myCar.color = "red"; myCar.start();

5. What is inheritance in OOPs?


This is among the top OOPs questions for interview.

Inheritance is the process of creating a new class (called the subclass or child class) from an existing class
(called the superclass or parent class).

The subclass inherits properties and behaviours from its superclass, and can also add new properties and
behaviours or modify existing ones.
The basic idea behind inheritance in OOP is to create a hierarchy of classes with increasing levels of
specialisation. The most general class in the hierarchy is the superclass, which defines the basic
properties and behaviours that are shared by all its subclasses. Each subclass can then add more specific
properties and behaviours that are unique to it.

Example of inheritance in OOP

Consider a class hierarchy that includes a superclass called "Vehicle" and two subclasses called "Car"
and "Motorcycle". The Vehicle class may have properties such as "make", "model", "year", and "color",
as well as methods such as "start" and "stop" that are common to all types of vehicles.

The Car subclass may inherit these properties and methods from the Vehicle class, but also add its own
properties, such as "numDoors" and methods such as "getGasMileage". The Motorcycle subclass may
also inherit from Vehicle, but may have its own unique properties such as "numWheels" and methods
such as "lean".

6. What is encapsulation in OOPs?


Encapsulation is the concept of hiding the implementation details of a class from its users, and
providing a public interface or API (Application Programming Interface) for interacting with the class.

The idea behind encapsulation is to protect the internal state of an object from being modified directly
by external code, and to enforce the use of the public interface for any interactions with the object.
This helps to prevent errors and ensure that the object is used correctly.

Example of encapsulation

Consider a class called "BankAccount". This class may have private fields such as "balance" and
"accountNumber", which should not be modified directly by external code. Instead, the class may
provide public methods such as "deposit", "withdraw", and "getBalance" for interacting with the
account.

7. What is abstraction in OOPs?


It is the process used to define a simplified interface or model that represents the essential features
of an object, without including unnecessary details or implementation specifics.

Abstraction helps to manage complexity by hiding the underlying details of a system and presenting
only the essential features that are relevant to the user. This allows the user to interact with the
system at a higher level of abstraction, without having to worry about the low-level details.An
abstract class may contain one or more abstract methods, which are defined but not implemented in
the abstract class. The subclasses of the abstract class must implement these abstract methods to
provide concrete functionality.

Example
For example, consider a class hierarchy that includes a superclass called "Animal" and two subclasses
called "Dog" and "Cat".

The Animal class may have abstract methods such as "makeSound" and "move", which are implemented
differently in each subclass. The Dog subclass may implement the "makeSound" method as "bark" and
the "move" method as "run", while the Cat subclass may implement the "makeSound" method as
"meow" and the "move" method as "walk".
8. What is polymorphism in OOPs?
Polymorphism is a fundamental concept in Object-Oriented Programming (OOP) that allows objects of
different classes to be treated as if they are objects of a common superclass or interface.

It enables the same code to work with different objects in different ways, depending on their actual type
or class.

9. What is method overloading? Explain with an example.


It is a form of static polymorphism that allows multiple methods to have the same name, but with
different parameters or argument types.

When an overloaded method is called, the compiler determines which method to call based on the
number, type, and order of the arguments passed to the method. The method name and the number,
type, and order of the parameters must be different in each overloaded method.

Example of method overloading in OOPs


For example, consider a class called "Math" that contains two overloaded methods called "add".

The first method takes two integers as arguments and returns their sum, while the second method takes
two doubles as arguments and returns their sum.

When we call the "add" method with two integers, the first method is called, and when we call the
"add" method with two doubles, the second method is called.

10. What is method overriding? Explain with an example.


It is a form of dynamic polymorphism that allows a subclass to provide its own implementation of a
method that is already defined in its superclass.
When a method is called on an object, the runtime environment determines which implementation of
the method to call based on the actual type of the object at runtime. The method name, return type,
and parameter types must be the same in both the superclass and the subclass.

Example of method overriding in OOPs


For example, consider a class hierarchy that includes a superclass called "Animal" and a subclass called
"Dog". The Animal class may have a method called "speak" that returns a string, while the Dog subclass
may override the "speak" method to return "woof".

When we call the "speak" method on a Dog object, the overridden method in the Dog class is called,
and when we call the "speak" method on an Animal object, the original method in the Animal class is
called.

11. What is the difference between an instance variable and a class variable?
In object-oriented programming, instance variables and class variables are two types of variables that can
be declared within a class.

The main difference between them is that instance variables are associated with an instance of a class,
while class variables are associated with the class itself.
Key differences between instance variables and class variables:
a) Scope
Instance variables are only accessible within the instance of the class in which they are defined, whereas
class variables are accessible throughout the entire class.
b) Lifetime
Instance variables are created when an instance of the class is created and are destroyed when the
instance is destroyed, whereas class variables exist for the entire lifetime of the program.
c) Memory allocation
Instance variables are allocated memory each time an instance of the class is created, whereas class
variables are allocated memory only once when the class is loaded.
d) Sharing
Each instance of a class has its own copy of instance variables, while class variables are shared by all
instances of the class.

12. What are the four fundamental principles of OOPs?


The four fundamental principles of Object-Oriented Programming (OOPs) are:

● Encapsulation
● Inheritance
● Polymorphism
● Abstraction

13. What is the difference between abstraction and encapsulation?


Abstraction and Encapsulation are two important concepts in Object-Oriented Programming (OOPs).
They are often confused with each other as they both deal with hiding complexity, but they are
different in nature.

Here are the differences between the two:

Feature Abstraction Encapsulation

Abstraction is the process of Encapsulation is the process of hiding


identifying the essential the internal implementation details of
Definition features of an object and an object from the outside world and
ignoring the non-essential exposing only the necessary
ones. information through a public interface.

Abstraction helps to manage


Encapsulation helps to achieve data
complexity by breaking down
hiding and data abstraction by hiding
Purpose a complex system into
the implementation details of an
smaller, more manageable
object from the outside world.
parts.

Abstraction focuses on the


behaviour and characteristics Encapsulation focuses on the
Level of Focus of an object without worrying implementation details of an object and
about the implementation restricts access to its internal state.
details.

Abstraction can be Encapsulation can be implemented


Implementation implemented using abstract using access modifiers like public,
classes and interfaces. private, and protected.

Abstraction is closely related


Encapsulation is closely related to data
Relationship to inheritance and
hiding and information hiding.
polymorphism.

14. What is the difference between inheritance and polymorphism?


Inheritance and Polymorphism are two important concepts in Object-Oriented Programming (OOPs).
They are often used together to create reusable and extensible code. The differences between the two
are:

Feature Inheritance Polymorphism

Inheritance is the mechanism by


Polymorphism is the ability of an
which one class acquires the
Definition object to take on multiple forms or
properties and behaviour of another
behaviours.
class.

Inheritance helps in creating new Polymorphism helps in creating flexible


classes that are modified versions and extensible code that can work with
Purpose
of existing classes without having objects of different classes.
to rewrite the entire code.

There are two types of There are two types of polymorphism:


Types inheritance: single-level and compile-time and runtime
multi-level inheritance. polymorphism.

Inheritance is implemented Polymorphism is implemented using


Implementation
using the extends keyword in method overloading and method
Python and C++. overriding.

Polymorphism is closely related to


Inheritance is closely related to the
Relationship the concept of interfaces and abstract
concept of parent and child
classes.
classes.

15. What is the role of the final keyword in Python?


The final keyword is used to define entities (variables, methods, and classes) that cannot be changed or
overridden.

Here are the different uses of the final keyword in Python:


a) Final variables:
A final variable is a variable whose value cannot be changed once it is initialised. It is also called a
constant. A final variable can be initialised during declaration or in a constructor. Once initialised, its
value cannot be modified.

Example:

final int MAX_VALUE = 100;

b) Final methods:
A final method is a method that cannot be overridden by a subclass. When a method is marked as final,
its implementation is fixed and cannot be changed by a subclass.
c) Final classes:
A final class is a class that cannot be extended by any subclass. When a class is marked as final, it cannot
be subclasses.

16. What is the difference between a static method and an instance method?
Static methods and instance methods are two different types of methods in Python. Here is a tabular
comparison of static and instance methods:

Feature Static Method Instance Method

A static method is a method An instance method is a method that


Definition that belongs to a class rather belongs to an instance of a class.
than an instance of a class.

A static method can be called using An instance method can only be called
Access the class name without creating an using an instance of the class.
instance of the class.

A static method is stored in a


An instance method is stored in memory
Memory permanent memory location and is
when an object is created.
Allocation loaded into memory when the class is
loaded.

A static method cannot use the this An instance method can use the this
Use of this
keyword as it does not belong to keyword to refer to the current instance
keyword
an instance of a class. of the class.
Accessing A static method cannot access An instance method can access both static
Instance instance variables directly. It can only and instance variables directly.
Variables access static variables.

A static method cannot be overridden An instance method can be overridden in


Overriding
in Python. Python.

17. What is the difference between private, protected, and public access modifiers in OOPs?
Access modifiers in OOP are used to control the visibility and accessibility of class members (variables,
methods, and nested classes) from other classes.

The three most commonly used access modifiers are private, protected, and public.
a) Private:
Members declared as private can only be accessed within the same class. They are not visible to any
other class, including subclasses.

This is the most restrictive access level and is often used to protect sensitive data or to prevent
unintended modifications to class members.

b) Protected:
Members declared as protected can be accessed within the same class, subclasses, and other classes in
the same package.

They are not visible to classes in different packages, except through inheritance. This access level is less
restrictive than private but more restrictive than public.

c) Public:
Members declared as public can be accessed from any class or package. This is the least restrictive
access level and is often used for methods and variables that are part of the class's public interface.
Q. List some features of OOP?
Ans: i. Emphasis is on data rather than procedures.
ii. Programs that are divided into what are known as objects.
iii. Follows bottom – up approach in program design.
iv. Functions that operate on the data of an object are tried together in the data structure.

Q.Define Class? 
A Class is a collection of objects of similar type. The classes are user-defined data types and behave like
built-in types of a programming language. A class is a way to bind the data and its associated functions
together. A class is a user-defined data type with a template that serves to define its properties. A class
is a blueprint that defines the variables & the methods common to all objects of a certain kind.
Q What do you mean by reusability?
The process of adding additional features to an existing class without modifying it is known as
„Reusability‟. The reusability is achieved through inheritance. This is possible by deriving a new class
from an existing class. The new class will have the combined features of both the classes.

You might also like