Java Interview Questions And Answers

a) Object Oriented : Java is an object oriented language where everything is done keeping objects (data) in mind.
b) Simple : Java is very easy to learn and follow. It’s syntax are very easy. Any programmer who has some basic knowledge about any object oriented languages like C++ can easily follow Java.
c) Platform Independent : Java is a write once, run everywhere language. That means Java program written on one platform can be run on any other platforms without much difficulties.
d) Secured : Java is a highly secured language through which you can develop virus-free and highly secured applications.
e) Robust : Java is a robust because of automatic garbage collection, better exception and error handling mechanism, no explicit use of pointers and better memory management system.
f) Portable : Java is portable because you can run Java bytecode on any hardware which has compliant JVM which converts bytecode according to that particular hardware.
g) Multithreaded : Java supports multithreaded programming where multiple threads execute their task simultaneously.
h) Distributed : Java is distributed because you can develop distributed large applications using Java concepts like RMI and EJB.
i) Dynamic : Java is a dynamic language because it supports loading of classes on demand.
j) Extensible : You can develop new classes using existing interfaces, you can declare new methods to existing classes or you can develop new sub classes to existing classes. That is all because of extensible nature of Java.
k) Functional Style Programming : With the introduction of lambda expressions, functional interfaces and Stream API in Java 8, you can also write functional style of programming in Java.
Java 17 or JDK 17 is the latest version of Java which is released on September 14, 2021. (Keep checking Oracle website for latest Java releases).
a) Inheritance
b) Abstraction
c) Polymorphism
d) Encapsulation
Inheritance is one of the key principle of object oriented programming. Through inheritance, one class can inherit the properties of another class. The class from which properties are inherited is called super class and the class to which properties are inherited is called sub class.
(Click here to see more info on Inheritance in Java)
There are 5 types of inheritance.
a) Single Inheritance : One class is extended by only one class.
b) Multilevel Inheritance : One class is extended by a class and that class in turn is extended by another class thus forming a chain of inheritance.
c) Hierarchical Inheritance : One class is extended by many classes.
d) Hybrid Inheritance : It is a combination of above types of inheritance.
e) Multiple Inheritance : One class extends more than one classes. (Java does not support multiple inheritance)
To avoid ambiguity, complexity and confusion, Java does not supports multiple inheritance. i.e. a class in Java can not extend more than one classes. For example, if Class C extends Class A and Class B which have a method with same name, then Class C will have two methods with same name. This causes ambiguity and confusion for which method to use. To avoid this, Java does not supports multiple inheritance.
Through interfaces, we can implement multiple inheritance in Java. A class in Java can not extend more than one classes, but a class can implement more than one interfaces.
No, only classes in Java are inherited from java.lang.Object class. Interfaces in Java are not inherited from java.lang.Object class. But, classes which implement interfaces are inherited from java.lang.Object class.
By declaring that member as a private. Because, private members are not inherited to sub classes.
No, a class can not extend itself.
No, constructors and initializers (Static initializers and instance initializers) are not inherited to sub classes. But, they are executed while instantiating a sub class.
Super class field will be hidden in the sub class. You can access hidden super class field in sub class using super keyword.
Yes, static members of a class are also inherited to sub classes.
super() : It is a calling statement to super class constructor.
this() : It is a calling statement to same class constructor.
Yes, We can create an object without using new operator. There are some other ways to create objects other than using new operator. But, 95% of object creation in Java is done through new operator only.
a) Using newInstance() Method
1Class c = Class.forName("packageName.MyClass");
2 
3 MyClass object = (MyClass) c.newInstance();
b) Using clone() method. 1 MyClass object1 = new MyClass(); 2 3 MyClass object2 = object1.clone(); c) Using object deserialization 1 ObjectInputStream inStream = new ObjectInputStream(anInputStream ); 2 3 MyClass object = (MyClass) inStream.readObject(); MyClass object = (MyClass) inStream.readObject(); d) Creating string and array objects 1 String s = "string object"; 2 3 int[] a = {1, 2, 3, 4};
Constructor Chaining is a technique of calling another constructor from one constructor. this() is used to call same class constructor where as super() is used to call super class constructor.
No. There is no way in Java to call sub class constructor from a super class constructor.
No, constructors in Java don’t have return type. If you keep return type for a constructor, it will be treated as a normal method and also compiler gives a warning saying that method has a constructor name.
Constructor without arguments is called no-arg constructor. Default constructor in Java is always a no-arg constructor.
Private constructors are used to restrict the instantiation of a class. When a class needs to prevent other classes from creating it’s objects then private constructors are suitable for that. Objects to the class which has only private constructors can be created within the class. A very good use of private constructor is in singleton pattern. This ensures only one instance of a class exist at any point of time.
(Click here to see more about Java Singleton Design Pattern)
No, we can’t use this() and super() in a method.

(Click here to see more about Class Variables Vs Instance Variables)
A class can have any number of constructors. These constructors will have different list of arguments. It is called constructor overloading. Constructor overloading provides different ways to instantiate a class.
Constructor is a special member of a class which is used to create the objects to the class. It is special because it will have same name as class. It will have no return type. Method is ordinary member of a class which is used to implement some behavior of a class. It will have it’s own name and return type.
Static method is common to all instances of a class. Static methods are stored in the class memory. Where as non-static methods are stored in the object memory. Each instance of a class will have their own copy of non-static methods.
Also Read : Classes And Objects Quiz
Yes, we can overload main() method. A Java class can have any number of main() methods. But to run the Java class, class should have main() method with signature as public static void main(String[] args). If you do any modification to this signature, compilation will be successful. But, you can’t run the Java program. You will get run time error as main method not found.
No, main() method must be public. You can’t define main() method as private or protected or with no access modifier. This is because to make the main() method accessible to JVM.
No, main() method must be declared as static so that JVM can call main() method without instantiating it’s class.
Suppose, if main() is allowed to be non-static, then while calling the main method JVM has to instantiate it’s class. While instantiating it has to call constructor of that class. There will be an ambiguity if constructor of that class takes an argument that what argument JVM has to pass while instantiating class containing main() method.
No, the return type of main() method must be void only.
Two types of modifiers are there in Java. They are,

        These are the modifiers which are used to restrict the visibility of a class or a field or a method or a constructor. Java supports 4 access modifiers.

         a) private : private fields or methods or constructors are visible within the class in which they are defined.
        
         b) protected : Protected members of a class are visible within the package but they can be inherited to sub classes outside the package.
        
         c) public : public members are visible everywhere.
        
          d) default or No-access modifiers : Members of a class which are defined with no access modifiers are visible within the package in which they are defined.
        
        (For more info on access modifiers, click here.)
    
        These are the modifiers which are used to achieve the functionalities other than the accessibility. For example,
        
        a) static : This modifier is used to specify whether a member is a class member or an instance member.
        
        b) final : It is used to restrict the further modification of a class or a method or a field. (for more on final, click here).
        
        c) abstract : abstract class or abstract method must be enhanced or modified further. (For more on abstract,  click here).
        
        d) synchronized : It is used to achieve thread safeness. Only one thread can execute a method or a block which is declared as synchronized at any given time. (for more on synchronized, click here.)
        
        (For more info on access Vs non-access modifiers, click here)
    
No, it is not possible. A class or a method can not be final and abstract at the same time. final and abstract are totally opposite in nature. final class or final method must not be modified further where as abstract class or abstract method must be modified further.
We can’t declare an outer class as private. But, we can declare an inner class (class as a member of another class) as private.
No, abstract methods can not be private. They must be public or protected or default so that they can be modified further.
No. synchronized keyword can be used either with a method or block.
Any classes which have only synchronized methods and blocks are treated as synchronized classes. Classes like Vector, StringBuffer have only synchronized methods. That’s why they are called as synchronized classes.
Also Read : Java Modifiers Quiz
When the data is converted from one data type to another data type, then it is called type casting. Type casting is nothing but changing the type of the data. Using type casting, only type of the data is changed but not the data itself.
(Click here for more info on type casting in Java)
There are two types of casting.
a) Primitive Casting : When the data is casted from one primitive type ( like int, float, double etc… ) to another primitive type, then it is called Primitive Casting.
b) Derived Casting : When the data is casted from one derived type to another derived type, then it is called derived casting.
The data is implicitly casted from small sized primitive type to big sized primitive type. This is called auto-widening. i.e The data is automatically casted from byte to short, short to int, int to long, long to float and float to double..
You have to explicitly cast the data from big sized primitive type to small sized primitive type. i.e you have to explicitly convert the data from double to float, float to long, long to int, int to short and short to byte. This is called explicit narrowing.
An object of sub class type can be automatically casted to super class type. This is called auto-up casting. An object of super class type should be explicitly casted to sub class type, It is called explicit down casting.
                        Yes, first int is auto-widened to double and then double is auto-boxed to Double.

                        1  double d = 10;     //auto-widening from int to double
                        2
                        3  Double D =  d;     //auto-boxing from double to Double
                    
ClassCastException is an exception which occurs at run time when an object of one type can not be casted to another type.
(Click here to see more on ClassCastException)
Wrapping of primitive content into corresponding wrapper class object is called boxing.
Unwrapping the wrapper class object into corresponding primitive content is called unboxing.
Auto-widening occurs when small sized primitive type is casted to big sized primitive type. Auto-upcasting occurs when sub class type is casted to super class type. Auto-boxing occurs when primitive type is casted to corresponding wrapper class.
(Click here to see more detailed article on auto-widening Vs auto-upcasting Vs auto-boxing)
Polymorphism refers to any entity whether it is a method or a constructor or an operator which takes many forms or can be used for multiple tasks.
(Click here to see more info on polymorphism in Java)
When a class has more than one method with same name but different parameters, then we call those methods are overloaded. Overloaded methods will have same name but different number of arguments or different types of arguments.
(Click here to see more on method overloading in Java)
Method signature is used by the compiler to differentiate the methods. Method signature consist of three things.
  • Method name
  • Number of arguments
  • Types of arguments
Compiler uses method signature to check whether the method is overloaded or duplicated. Duplicate methods will have same method signatures i.e same name, same number of arguments and same types of arguments. Overloaded methods will also have same name but differ in number of arguments or else in types of arguments.
Yes. Overloaded methods can be either static or non static.
No, compiler will give duplicate method error. Compiler checks only method signature for duplication not the return types. If two methods have same method signature, straight away it gives compile time error.
Yes. Overloaded methods can be synchronized.
Yes, overloaded methods can be final.
                    1   public class A
                    2  {
                    3   public A()
                    4   {
                    5       //-----> (1)
                    6   }
                    7 
                    8   void A()
                    9  {
                    10       //-----> (2)
                    11  }
                    12 } 
                
None of them. It is neither constructor overloaded nor method overloaded. First one is a constructor and second one is a method.
False. Overloading is the best example for static binding. (Click here to see what is static binding and what is dynamic binding)
Yes, we can override a method which is overloaded in super class.
Modifying a super class method in the sub class is called method overriding. Using method overriding, we can change super class method according to the requirements of sub class.
(Click here to see more info on method overriding in Java)

                    There are 5 main rules you should kept in mind while overriding a method. They are,

                    a) Name of the method must be same as that of super class method.
                    
                    b) Return type of overridden method must be compatible with the method being overridden. i.e if a method has primitive type as it’s return type then it must be overridden with primitive type only and if a method has derived type as it’s return type then it must be overridden with same type or it’s sub class types.
                    
                    c) You must not reduce the visibility of a method while overriding.
                    
                    d) You must not change parameter list of a method while overriding.
                    
                    e) You can not increase the scope of exceptions while overriding a method with throws clause.
                   
                  
No, static methods can not be overridden. If we try to override them they will be hidden in the sub class.
If we change the arguments of overriding method, then that method will be treated as overloaded not overridden.
Yes. You can increase the visibility of overriding methods but can’t reduce it.
Yes. You can change as Integer is a sub class of Number type.
Yes. Overridden method may throw SQLException or it’s sub class exception or any unchecked type of exceptions.
No. We can’t change an exception of a method with throws clause from unchecked to checked.
(Click here to see more about method overriding with throws clause)
Using super keyword, we can refer super class version of overridden method in the sub class.
No question of overriding private methods. They are not at all inherited to sub class.
Yes. You can remove throws clause of a method while overriding it.
No. You can’t override non-static methods as static.
Yes. We can change an exception from checked to unchecked but reverse is not possible.
Yes, we can change. But, exceptions must be compatible with throws clause in the super class method.
Click here to see the differences between method overloading and overriding.
Click here to see what is static binding and dynamic binding in Java.
False. Abstract methods can also have concrete methods.
Not necessarily. Abstract class may or may not have abstract methods.
No. Constructor, Static Initialization Block, Instance Initialization Block and variables can not be abstract.
Because, final and abstract are totally opposite in nature. A final class or method can not be modified further where as abstract class or method must be modified further. final keyword is used to denote that a class or method does not need further improvements. abstract keyword is used to denote that a class or method needs further improvements.
No, we can’t instantiate a class once it is declared as abstract even though it does not have abstract methods.
No. Abstract methods can not be private. If abstract methods are allowed to be private, then they will not be inherited to sub class and will not get enhanced.
It is because, we can’t create objects to abstract classes but we can create objects to their sub classes. From sub class constructor, there will be an implicit call to super class constructor. That’s why abstract classes should have constructors. Even if you don’t write constructor for your abstract class, compiler will keep default constructor.
No, abstract methods can not be static.
Yes, a class can have abstract class as it’s member.
True. Abstract classes can be nested i.e an abstract class can have another abstract class as it’s member.
No, abstract methods can not be declared as synchronized. But methods which override abstract methods can be declared as synchronized.
Yes. Local inner class can be abstract.
Yes. Abstract methods can be declared with throws clause.
Yes, abstract classes can have interfaces as their member.
No. Interfaces can’t have constructors, static initializers and instance initializers.
No. The fields of interfaces are static and final by default. They are just like constants. You can’t change their value once they got.
Yes, we can declare an interface with abstract keyword. But, there is no need to write like that. All interfaces in Java are abstract by default.
True. .class file will be generated for every interface after compilation.
No. While overriding any interface methods, we should use public only. Because, all interface methods are public by default and you should not reduce the visibility while overriding them.
No. You can’t define interfaces as local members of methods like local inner classes. They can be a part of top level class or interface.
No, an interface can’t extend a class. But it can extend another interface.
No. Interfaces don’t extend Object class. ( Click here for more )