0% found this document useful (0 votes)
64 views9 pages

OOPs Interview Que Ans

Structured programming refers to programming with a definite control flow using constructs like if/then/else, while loops, and subroutines. Nearly all programming paradigms include structured programming. Object-oriented programming (OOP) is based on four main concepts: inheritance, encapsulation, polymorphism, and abstraction. OOP promotes code reuse and flexibility through these concepts. Some key OOP concepts include classes, which provide templates for objects, and objects which are instances of classes.

Uploaded by

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

OOPs Interview Que Ans

Structured programming refers to programming with a definite control flow using constructs like if/then/else, while loops, and subroutines. Nearly all programming paradigms include structured programming. Object-oriented programming (OOP) is based on four main concepts: inheritance, encapsulation, polymorphism, and abstraction. OOP promotes code reuse and flexibility through these concepts. Some key OOP concepts include classes, which provide templates for objects, and objects which are instances of classes.

Uploaded by

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

What is meant by Structured Programming?

Structured Programming refers to the method of programming which consists of a


completely structured control flow. Here structure refers to a block, which contains a
set of rules, and has a definitive control flow, such as (if/then/else), (while and for),
block structures, and subroutines.

Nearly all programming paradigms include Structured programming, including the


OOPs model.

 What are the main features of OOPs?

OOPs or Object Oriented Programming mainly comprises of the below four features,
and make sure you don't miss any of these:

 Inheritance
 Encapsulation
 Polymorphism
 Data Abstraction

What are some advantages of using OOPs?


 OOPs is very helpful in solving very complex level of problems.
 Highly complex programs can be created, handled, and maintained easily
using object-oriented programming.
 OOPs, promote code reuse, thereby reducing redundancy.
 OOPs also helps to hide the unnecessary details with the help of Data
Abstraction.
 OOPs, are based on a bottom-up approach, unlike the Structural
programming paradigm, which uses a top-down approach.
 Polymorphism offers a lot of flexibility in OOPs.

What is a class?

A class can be understood as a template or a blueprint, which contains some values,


known as member data or member, and some set of rules, known as behaviors or
functions. So when an object is created, it automatically takes the data and functions
that are defined in the class.
Therefore the class is basically a template or blueprint for objects. Also one can
create as many objects as they want based on a class.
For example, first, a car’s template is created. Then multiple units of car are created
based on that template.

What is an object?

An object refers to the instance of the class, which contains the instance of the
members and behaviors defined in the class template. In the real world, an object is
an actual entity to which a user interacts, whereas class is just the blueprint for that
object. So the objects consume space and have some characteristic behavior.
For example, a specific car.

What is encapsulation?
One can visualize Encapsulation as the method of putting everything that is required
to do the job, inside a capsule and presenting that capsule to the user. What it
means is that by Encapsulation, all the necessary data and methods are bind
together and all the unnecessary details are hidden to the normal user. So
Encapsulation is the process of binding data members and methods of a program
together to do a specific job, without revealing unnecessary details.

Encapsulation can also be defined in two different ways:

1) Data hiding: Encapsulation is the process of hiding unwanted information, such as


restricting access to any member of an object.

2) Data binding: Encapsulation is the process of binding the data members and the
methods together as a whole, as a class.

What is Polymorphism?

Polymorphism is composed of two words - “poly” which means “many”, and “morph”
which means “shapes”. Therefore Polymorphism refers to something that has many
shapes.
In OOPs, Polymorphism refers to the process by which some code, data, method, or
object behaves differently under different circumstances or contexts. Compile-time
polymorphism and Run time polymorphism are the two types of polymorphisms in
OOPs languages.

13. What is Compile time Polymorphism and how is it different from


Runtime Polymorphism?
Compile Time Polymorphism: Compile time polymorphism, also known as Static
Polymorphism, refers to the type of Polymorphism that happens at compile time.
What it means is that the compiler decides what shape or value has to be taken by
the entity in the picture.

Example:

// In this program, we will see how multiple functions are created with the same
name,
// but the compiler decides which function to call easily at the compile time
itself.
class CompileTimePolymorphism{
// 1st method with name add
public int add(int x, int y){
return x+y;
}
// 2nd method with name add
public int add(int x, int y, int z){
return x+y+z;
}
// 3rd method with name add
public int add(double x, int y){
return (int)x+y;
}
// 4th method with name add
public int add(int x, double y){
return x+(int)y;
}
}
class Test{
public static void main(String[] args){
CompileTimePolymorphism demo=new CompileTimePolymorphism();
// In the below statement, the Compiler looks at the argument types and decides
to call method 1
System.out.println(demo.add(2,3));
// Similarly, in the below statement, the compiler calls method 2
System.out.println(demo.add(2,3,4));
// Similarly, in the below statement, the compiler calls method 3
System.out.println(demo.add(2,3.4));
// Similarly, in the below statement, the compiler calls method 4
System.out.println(demo.add(2.5,3));
}
}
In the above example, there are four versions of add methods. The first method takes
two parameters while the second one takes three. For the third and fourth methods,
there is a change of order of parameters. The compiler looks at the method signature
and decides which method to invoke for a particular method call at compile time.

Runtime Polymorphism: Runtime polymorphism, also known as Dynamic


Polymorphism, refers to the type of Polymorphism that happens at the run time.
What it means is it can't be decided by the compiler. Therefore what shape or value
has to be taken depends upon the execution. Hence the name Runtime
Polymorphism.
Example:

class AnyVehicle{
public void move(){
System.out.println(“Any vehicle should move!!”);
}
}
class Bike extends AnyVehicle{
public void move(){
System.out.println(“Bike can move too!!”);
}
}
class Test{
public static void main(String[] args){
AnyVehicle vehicle = new Bike();
// In the above statement, as you can see, the object vehicle is of type
AnyVehicle
// But the output of the below statement will be “Bike can move too!!”, 
// because the actual implementation of object ‘vehicle’ is decided during
runtime vehicle.move();
vehicle = new AnyVehicle();
// Now, the output of the below statement will be “Any vehicle should move!!”, 
vehicle.move();
}
}

As the method to call is determined at runtime, as shown in the above code, this is
called runtime polymorphism.

What is meant by Inheritance?

In object-oriented programming, inheritance is the mechanism by which an object or


class (referred to as a child) is created using the definition of another object or class
(referred to as a parent). Inheritance not only helps to keep the implementation
simpler but also helps to facilitate code reuse.

What is Abstraction?

Abstraction is the method of hiding unnecessary details from the necessary ones. It is
one of the main features of OOPs. 

Is it always necessary to create objects from class?


No. An object is necessary to be created if the base class has non-static methods. But
if the class has static methods, then objects don’t need to be created. You can call
the class method directly in this case, using the class name.

What is a constructor?
Constructors are special methods whose name is the same as the class name. The
constructors serve the special purpose of initializing the objects.
For example, suppose there is a class with the name “MyClass”, then when you
instantiate this class, you pass the syntax:
MyClass myClassObject = new MyClass();

Now here, the method called after “new” keyword - MyClass(), is the constructor of
this class. This will help to instantiate the member data and methods and assign
them to the object myClassObject.

What are the various types of constructors in C++?

The most common classification of constructors includes:

Default constructor: The default constructor is the constructor which doesn’t take any
argument. It has no parameters.
class ABC
{
int x;
ABC()
{
x = 0;
}
}
Parameterized constructor: The constructors that take some arguments are known as
parameterized constructors.
class ABC
{
int x;

ABC(int y)
{
x = y;
}
}
Copy constructor: A copy constructor is a member function that initializes an object
using another object of the same class.
class ABC
{
int x;

ABC(int y)
{
x = y;
}
// Copy constructor
ABC(ABC abc)
{
x = abc.x;
}
}

What is a copy constructor?

Copy Constructor is a type of constructor, whose purpose is to copy an object to


another. What it means is that a copy constructor will clone an object and its values,
into another object, is provided that both the objects are of the same class.

What is a destructor?

Contrary to constructors, which initialize objects and specify space for them,
Destructors are also special methods. But destructors free up the resources and
memory occupied by an object. Destructors are automatically called when an object
is being destroyed. 
What are the various types of inheritance?

The various types of inheritance include:

 Single inheritance
 Multiple inheritances
 Multi-level inheritance
 Hierarchical inheritance
 Hybrid inheritance

 What is a subclass?

The subclass is a part of Inheritance. The subclass is an entity, which inherits from
another class. It is also known as the child class.

Define a superclass?

Superclass is also a part of Inheritance. The superclass is an entity, which allows


subclasses or child classes to inherit from itself.

You might also like