Java Interview Questions And Answers

In this Article you'll get each and everything for Java interview Questions

1. Can we overload main() method?

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.

 

2. Can we declare main() method as private or protected or with no access modifier?

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. If you define main() method other than public, compilation will be successful but you will get run time error as no main method found.

 

3. Can we declare main() method as non-static?

No, main() method must be declared as static so that JVM can call main() method without instantiating it’s class. If you remove ‘static’ from main() method signature, compilation will be successful but program fails at run time.

 

4. Why main() method must be static?

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.

 

5. Can we change return type of main() method?

No, the return type of main() method must be void only. Any other type is not acceptable.

 

6. Can main() method take an argument other than String array?

No, argument of main() method must be string array. But, from the introduction of varargs you can pass varargs of String type as an argument to main() method. varargs are nothing but the array.

 

7. Can we run java class without main() method?

No, you can’t run java class without main method. But, there are some scenarios like if super class has main() method, then sub class can be run without defining main() method in it.

Note: Before Java 7, you can run java class by using static initializers. But, from Java 7 it is not possible.

 

8. Can you create an object without using new operator in Java?

Yes, we can create an object without using new operator. There are some other ways to create objects other than using new operator.

  1. Using newInstance() Method
  2. Using clone() method.
  3. Using object deserialization
  4. Creating String and array objects

 

9. What is constructor chaining?

Constructor Chaining is a technique of calling another constructor from one constructor. this() is used to call same class constructor whereas super() is used to call super class constructor.

 

10. Can we call sub class constructor from super class constructor?

No. There is no way in java to call sub class constructor from a super class constructor.

 

11. What happens if you keep return type for a constructor?

It will be treated as a normal method. But compiler gives a warning saying that method has a constructor name.

 

12. What is no-arg constructor?

Constructor without arguments is called no-arg constructor. Default constructor in java is always a no-arg constructor.

 

13. What is the use of private constructor?

Private constructors are used to restrict the instantiation of a class. When a class needs to prevent other classes from creating its objects then private constructors are suitable for that. Objects to the class which has only private constructors can be created within the class, e.g. Singleton pattern. This ensures only one instance of a class exist at any point of time.

 

14. Can we use both this() and super() in a method?

No, We can’t use both this() and super() in a method, because both needs to be the first statement inside constructor .

 

15. Difference between Class Variables and Instance Variables in Java?

Class Variables

Instance Variables

Class variables are declared with keyword static.

Instance variables are declared without static keyword.

Class variables are common to all instances of a class. These variables are shared between the objects of a class.

Instance variables are not shared between the objects of a class. Each instance will have their own copy of instance variables.

As class variables are common to all objects of a class, changes made to these variables through one object will reflect in another.

As each object will have its own copy of instance variables, changes made to these variables through one object will not reflect in another object.

Class variables can be accessed using either class name or object reference.

Instance variables can be accessed only through an object reference.

 

16. What do you mean by inheritance?

Inheritance is one of the key features of object oriented programming. Through inheritance, a class (Sub, Child, Derived Class) can inherit properties of another class (Super, Parent, Base Class). Sub class can have its own properties along with the inherited properties from its super class.

 

17. What are the types of inheritance?

There are 5 types of inheritance.

1). Single Inheritance: One class is extended by only one class.

2). Multilevel Inheritance: One class is extended by a class and that class is again extended by another class thus forming a chain of inheritance.

3). Hierarchical Inheritance: One class is extended by many classes.

4). Multiple Inheritance: One class extends more than one classes. (Java does not support multiple inheritance in case of class.)

5).Hybrid Inheritance: It is a combination of (Hierarchical and Multiple inheritance).

NOTE:-

  1. In case of class, Java supports 3 types of inheritance (Single, Multilevel, and Hierarchical)
  2. In case of interface, Java supports all types of inheritance.

 

18. Can a class extend more than one classes or does java support multiple inheritance? If not, why?

No, a class in java cannot extend more than one classes or java does not support multiple inheritance in case of class. To avoid ambiguity, complexity and confusion, java does not supports multiple inheritance for class. 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 in case of class.

 

19. How do you implement multiple inheritance in java?

Through interfaces, we can implement multiple inheritance in java. As classes in java cannot extend more than one classes, but a class can implement more than one interfaces.

 

20. You know that all classes in java are inherited from java.lang.Object class. Are interfaces also inherited from Object class.?

No, only classes in java are inherited from Object class. Interfaces in java are not inherited from Object class. But, classes which implement interfaces are inherited from Object class.

 

21. How do you restrict a member of a class from inheriting to its sub classes?

By declaring that member as a private. Because, private members are not inherited to sub classes.

 

22.Can a class extend itself?

No, a class cannot extend itself.

 

23. Are constructors and initializers also inherited to sub classes?

No, Constructors and initializers (Static initializers and instance initializers) are not inherited to sub classes. But, they are executed while instantiating a sub class.

 

24. What happens if both, super class and sub class, have a field with same name?

Super class field will be hidden in the sub class. You can access hidden super class field in sub class using super keyword.

 

25. Are static members inherited to sub classes?

Yes, Static members are also inherited to sub classes.

 

26. How many types of modifiers are there in Java?

Two types of modifiers are there in java. They are,

  1. Access Modifiers:- private, default or no modifier, protected, public (by default default)
  2. Non-access Modifiers:- (static, final, abstract, synchronized, transient, volatile, native, strictfp)

 

27What are access modifiers in java?

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.

  1. private: private fields or methods or constructors are visible within the class in which they are defined.
  2. default or No-access modifier :Members of a class which are defined with no access modifiers are visible within the package in which they are defined.
  3. protected: Protected members of a class are visible within the package but they can be inherited to sub classes outside the package.
  4. public: public members are visible everywhere.

 

28. What are non-access modifiers in java?

These are the modifiers which are used to achieve other functionalities like,

  1. static: This modifier is used to specify whether a member is a class member or an instance member.
  2. final: It is used to restrict the further modification of a class or a method or a field.
  3. abstract: abstract class or abstract method must be enhanced or modified further.
  4. synchronized:It is used to achieve thread safety. Only one thread can execute a method or a block which is declared as synchronized at any given time.

 

29. Can we use a field or a method declared without access modifiers outside the package?

No, we can’t use a field or a method with no-access (default) specifier outside the package.

 

30. Can a method or a class be final and abstract at the same time?

No, it is not possible. A class or a method cannot 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 whereas abstract class or abstract method must be modified further.

 

31. Can we declare a class as private?

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.

 

32. Can we declare an abstract method as private also?

No, abstract methods cannot be private. They must be public or protected or default so that they can be modified further.

 

33. Can we declare a class as protected?

We can’t declare an outer class as protected. But, we can declare an inner class (class as a member of another class) as protected.

 

34. A class cannot be declared with synchronized keyword. Then, why we call classes like Vector, StringBuffer are synchronized classes?

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.

 

35. What is type casting?

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 can be changed but not the data itself.

 

36. What are the types of casting?

There are two types of casting.

1) 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.

2) Derived Casting:- When the data is casted from one derived type to another derived type, then it is called derived casting.

 

37. What is auto widening and explicit narrowing?

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.

 

38. What is auto-up casting and explicit down casting?

An object of sub class type can be automatically casted to its 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.

 

39. Can an int primitive type of data implicitly casted to Double derived type?

Yes, first int is auto-widened to double and then double is auto-boxed to Double.

double  d = 10;  //auto-widening from int to double

Double D = d;  //auto-boxing from double to Double

 

40. What is ClassCastException?

ClassCastException is an exception which occurs at run time when an object of one type cannot be casted to another type.

 

41. How do you avoid ClassCastException in your code?

By using generics, you can avoid ClassCastException.

 

42. What is boxing and unboxing?

Wrapping of primitive content into corresponding wrapper class object is called boxing. Unwrapping the wrapper class object into corresponding primitive content is called unboxing.

 

43. What is the difference between auto-widening, auto-upcasting and auto-boxing?

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.



44. What are the priorities of auto-widening, auto-upcasting and auto-boxing?


 

The sequence is:-

Auto-widening à Auto-Boxing à Auto-upcasting à  varargs



45. What is method overloading?

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.

 

46. What is method signature? What are the things it consist of?

Method signature is used by the compiler to differentiate the methods. Method signature consists of three things.

  1. Method name
  2. Number of arguments
  3. Types of arguments

 

47. Can we declare one overloaded method as static and another one as non-static?

Yes. Overloaded methods can be either static or non-static.

48. How do compiler differentiate overloaded methods from duplicate methods?

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 types of arguments.

49. Is it possible to have two methods in a class with same method signature but different return types?

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, it gives compile time error.

50. In “MyClass”, there is a method called “myMethod” with four different overloaded forms. All four different forms have different visibility (private, protected, public and default). Is “myMethod” properly overloaded?

Yes. Compiler checks only method signature for overloading of methods not the visibility of methods.

When a class has more than one method with same name, then we call that method is overloaded. The overloaded methods will have different number of arguments or different types of arguments, but name of the methods remains same.

Compiler checks method signature for duplicate methods or for method overloading. Method signature consist of three things, 1) Method Name   2) Number Of Arguments   3) Types of arguments.

If these three things are same for any two methods in a class, then compiler gives duplicate method error.