OOPS Interview Questions - PART 1
OOPS Interview Questions - PART 1
1. What is OOPs?
GAURAV SHARMA
4. What is an object?
An object is a real-world entity which is the basic unit of OOPs for example chair, cat,
dog, etc. Different objects have different states or attributes, and behaviors.
5. What is a class?
A class is a prototype that consists of objects in different states and with different
behaviors. It has a number of methods that are common the objects present within that
class.
Class: User-defined blueprint from which objects are created. It consists of methods or
set of instructions that are to be performed on the objects.
7. What is inheritance?
GAURAV SHARMA
Inheritance is a feature of OOPs which allows classes inherit common properties from
other classes. For example, if there is a class such as ‘vehicle’, other classes like ‘car’,
‘bike’, etc can inherit common properties from the vehicle class. This property helps you
get rid of redundant code thereby reducing the overall size of the code.
Single inheritance
Multiple inheritance
Multilevel inheritance
Hierarchical inheritance
Hybrid inheritance
10. Can you call the base class method without creating an instance?
Yes, you can call the base class without instantiating it if:
It is a static method
The base class is inherited by some other subclass
GAURAV SHARMA
11. What is encapsulation?
Encapsulation is 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.
2) Data binding: Encapsulation is the process of binding the data members and the
methods together as a whole, as a class.
Polymorphism is composed of two words - “poly” which means “many”, and “morph”
which means “shapes”. Therefore Polymorphism refers to something that has many
shapes.
GAURAV SHARMA
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.
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
GAURAV SHARMA
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 4
System.out.println(demo.add(2,3.4));
// Similarly, in the below statement, the compiler calls method 3
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.
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{
GAURAV SHARMA
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.
GAURAV SHARMA
Default Do not have permission to create
You can create a default constructor
constructor any default constructor
o Extensibility
o Implementation Hiding
o Accessing object using interfaces
o Loose coupling
GAURAV SHARMA
20. Can abstract class have Constructors ?
24. Abstract class must have only abstract methods. Is it true or false?
False. Abstract class can have both abstract and non abstract methods.
When you have a requirement where your base class should provide the default implementation
of certain methods whereas other methods should be open to being overridden by child classes
that time you have to use abstract classes.
Because, it has not fully implemented the class as its abstract methods can not be
executed.
If the compiler allows us to create the object for the abstract class, then you can
invoke the abstract method using that object which cannot be executed by CLR at
runtime.
Hence, to restrict the calling of abstract methods, the compiler does not allow you
to instantiate an abstract class.
GAURAV SHARMA
27. Which type of members can you define in an Abstract class?
You can define all static and non-static members including properties, fields, indexers and also
abstract methods.
Overloaded operators are functions with special names the keyword operator
followed by the symbol for the operator being defined.
Similar to any other function, an overloaded operator has a return type and a
parameter list.\
Yes, it is it possible that a Method can return multiple values at a time by using the
following:
GAURAV SHARMA
o KeyValue pair
o Ref or Out parameters
o Struct or Class
o Tuple
o If the static keyword is applied to a class, all the members of the class
must be static.
o Static methods can only access static members of same class.
o Static properties are used to get or set the value of static fields of a
class.
o Static constructor cannot be parameterized.
o Access modifiers cannot be applied on static constructor. Because, it
is always a public default constructor which is used to initialize static
fields of the class.
GAURAV SHARMA
36. What is Static ReadOnly?
Static Readonly type variable value can be assigned at runtime or at compile time
and can be changed at runtime.
Such variable's value can only be changed in the static constructor and can not be
changed further.
It can change only once at runtime.
THIS
o THIS refers to the current instance (not the “current class”).
o It can only be used in non-static methods/members. Because, in a
static method there is no current instance.
o Calling a method on this will call the method in the same way as it
would if you called it on a variable containing the same instance.
BASE
o BASE is a keyword that allows inherited method call, i.e. it calls the
specified method from the base type.
o It can only be used in a non-static method.
o It is usually used in a virtual method override, but actually can be used
to call any method in the base type.
o It is very different from normal method invocation because it
circumvents the normal virtual-method dispatch.
o It calls the base method directly even if it is virtual.
A partial class is only used to split the definition of a class in two or more classes in
a same source code file or more than one source files.
GAURAV SHARMA
You can create a class definition in multiple files but it will be compiled as one class
at run time and also when you’ll create an instance of this class. So, you can
access all the methods from all source file with a same object.
Partial Class can be created in the same namespace and it does not allowed to
create a partial class in different namespace.
You can use “partial” keyword with all the class name which you want to bind
together with the same name of class in same namespace.
GAURAV SHARMA
GAURAV SHARMA