OOP's Interview Questions

1- What is the difference between C++ and Java?
  • C++ supports pointers whereas Java does not supports pointers.
  • Java is platform independent language but c++ is platform dependent language.
  • Java uses compiler and interpreter both and in c++ their is only compiler.
  • C++ supports operator overloading multiple inheritance but java does not.
  • Thread support is built-in Java but not in C++.
  • There is no goto statement in Java.
  • Exception and Auto Garbage Collector handling in Java is different because there are no destructors into Java.
  • Java has method overloading, but no operator overloading just like c++.
  • Java does not support unsigned integer but c++ supports.

2 - Is java a Pure Object Oriented Programming Language?
Java is not a pure object oriented programming language because it supports primitive data types and static keyword and you can run a java program without making a single object.
3 - What is difference between Encapsulation and Abstraction?
Both encapsulation and abstraction are oops important concepts . Encapsulation means hiding the internal implemetation details of data and method within a class from outside world. Abstraction on the other hand displays the essential features but hide the unnecessary features of an object.
4 - What is difference between a class and an object?
A class is a blueprint that describes the details of an object. A class has a state(instance variables) and behaviour (methods).
An object is a instance of a class. You can have n numbers of objects of one class. An object represent to things state and behavior.
5 - What is difference between Static Binding and Dynamic Binding?
Method overloading is called static binding or early binding and method overriding is called dynamic binding or late binding. Static binding is done at compile time and dynamic binding is done at runtime.
6 - Is multiple Inheritence support by Java and why?
No, java doesn't supports multiple inheritance because it avoid diamond problem in java.
Diamond Problem
7 - What is Difference between Abstract and Interface?
  • Java Interface is 100% abstract as it has body less methods and abstract class may have both methods simple as well as body less methods.
  • Variables declared in a Java interface is by default final whereas abstract class may contain non-final variables.
  • Members of a Java interface are public by default and abstract class can have class members like private, protected, etc
  • Java interface should be implemented using keyword "implements" and abstract class should be extended using keyword "extends".
  • Class that implements interface need to define its methods and subclass that extends abstract class also need to define its abstract methods.

8 - What is Difference between Default constructor and Parameterized Constructor?
Default constructor are those which has no parameter and parameterized constructor are those which has parameters. If no constructor is define in a class than jvm by default create default constructor and if a constructor is define by developer no matter default or parameterized jvm not creates default constructor.
9 - What is difference between this and super keyword?
super keyword always refers to the parent elements like parent method, parent constructor, parent instance variables whereas this keyword always refers to the current class elements like methods, constructors, instance variables.
Calling parent class method by using super. Example -> super.parentClassMethodName();
Calling current class method. Example -> this.currentClassMethodName();
10 - What is Static keyword and variable?
  • If there is a need for a variable to be common to all the objects of a single java class, then the static modifier should be used in the variable declaration.
  • Any object that belongs to same class can modify its static variables value.
  • As static variables, static methods are also common to classes.
  • It is a Good practice, static methods should be invoked with using the class name.
  • Static methods generally use to access static fields.
  • You can access static members with class name, without class name and with class instance.

11 - How many types of Access Modifiers in Java?
There are 4 types of access modifiers in java namely
  • Public
  • Protected
  • Default
  • Private

12 - What are difference between pass by reference and pass by value?
Pass by reference means passing an object reference and pass by value means passing variable into method argument means pass by value make copy of the variable whereas pass by reference only passes the reference to object not whole object .
13 - What is difference between Method Overloading and Method Overriding?
  • Method overloading let you reuse the same method name in a class, but with different arguments, types of arguments and position of arguments whereas method overriding is to overrides the super class method functionality according to class need.
  • Method overloading must change the argument list whereas in method overriding method signature should be same as super class method.
  • Overloaded method can be final and static but you cannot override final and static methods.
  • Overloaded methods can change the access modifier but the access level can't be less restrictive in overridden method.

14 - What is the difference between Constructor and Methods?
Constructor is a special method that is used to initialize a newly created object and is called just after the memory is allocated for the object. It can be used to initialize the objects, to required,or default values at the time of object creation. It is not mandatory for the coder to write a constructor for the class.
  • Constructor has no return type but method have return type.
  • Constructor name is same as class name but method may have any name.
  • Constructor is called when object is created whereas method is call with class object.

15 - What is difference between aggregation and composition?
Aggregation is a special from of association, aggregation is when one class is used as a part of another class, but still exists outside of that other class.
Composition is a special case of aggregation. A restricted aggregation is called composition. When an object contains the other object, if the contained object cannot exist without the existence of container object, then it is called composition.
16 - Can a local variable be declared as static?
No, you cannot declare static variable inside any method because java provide static variable and methods for a class.
17 - Can we have static methods in an Interface?
No you cannot have static methods in interface because all methods in interface is override by implementing class and you know that you cannot override static methods.
18 - How destructors are defined in Java ?
Java take cares of all garbage collection automatically and hence there is no destructor in Java. but still if you want to perform some additional tasks, then you can use the finalize() method of java.
19 - How many different ways to create a objects?
There are 4 ways of creating object in java
  • By using new keyword, this common and mostly used to create object
    MyClass object=new MyClass();
  • If you know class name and it has public default constructor, you can create object by Class.forName()
    MyClass object= (MyClass) Class.forName("MyClass").newInstance();
  • Using clone(),it can be used to create a copy of an existing object.
    MyClass object=new MyClass();
    MyClass anotherObject=object.clone();
  • Using object deserialization
    ObjectInputStream oi = new ObjectInputSteram(InputStream);
    MyClass object = (MyClass) oi.readObject();

20 - Can abstract class be decleared without abstract method?
Yes, you can have abstract class without any abstract method.