Java Interview Questions

Java Interview Questions

Below are most frequently asked question in Interview for Java topic. Now a days expectation had been increased for an testers and many projects are moving to Devops with Agile Methodology where each team member had equal visibility to the client , So it is expected from testers that being not just a manual tester they should know about the Automation tools, at least any one programming language and Devops tools so that they can also be a equal contributor in team.

21.Can we override the static method?

No, Static methods can’t be overridden as its scope is class level rather than object level.

22.Can we overload the static method?

Yes, we can overload the static method.

23.What is the use of instanceof operator in Java?

In Java instanceof operator is used to test whether the object is an instance of the specified type

24.What is the base class of all classes in Java?

The base class of all the classes in Java is Object.

25.How will you achieve abstraction?

There are two ways to achieve abstraction in java

  • Abstract class (0 to 100%)
  • Interface (100%)
26.Why we use interface?

We use interface to achieve abstraction ,we can support the functionality of multiple inheritance and it used to achieve loose coupling.

27.Can method have body in interface?

No, Method cannot have body inside an interface.

28.Is it okay if we don’t implement all the methods declared in the interface?

No , it is mandatory to class that implement interface must implement all the methods declared in the interface.

29.What will happen if we define a concrete method in an interface?

If we declare any concrete method in an interface compile time error will come.

30.Can we create non static variables in an interface?

No, we cannot create non-static variables in an interface because by default variable will be treated as public static final variables.

31.Do we need to initialize variable defined in interface?

Yes, we have to initialized the variable defined in interface.

32.When we use extends and implements keywords?

We use extends and implements keywords in below conditions,

  • An interface extends another interface.
  • A class implements an interface.
  • A class extends another class.
33.Abstract class have constructors in Java?

Yes, abstract class can have constructor in Java. Since we cannot create instance of abstract class, constructor will be called when we create instance of concrete implementation class.

34.Do we need to initialize variable defined in abstract class?

No, we don’t need to initialize variable defined in abstract class.

35.What is the purpose of constructor in abstract class, if you can not instantiate abstract class?

It is used to initialize common variables, which are declared inside abstract class.

36.Can abstract class implements interface in Java?

Yes, abstract class can implement interface by using implements keyword and as it’s a abstract class it not mandatory to implement all the methods of an interface.

37.Is it mandatory for abstract class to have an abstract method?

No, It is not mandatory for an abstract class to have any abstract method.

38.Abstract class can contain non abstract methods?

Yes, Abstract class can contain not abstract methods.

39.How will you make any class immutable in Java?

Yes, we can make any class immutable in java with below steps :
1.Making class as final so that sub class will not be able to modify the dat.
2.Declaring all the variable private and final so it will not be access outside the class and final means only one time we can modify the data. 3.
3.We will write the constructor to initialize the variables.
4.We will write getter method , we want to access data but we will not write any setter method inside the class.

40.Can we define abstract class as final in Java?

Java don’t allow to declare abstract class as final.

41.What is the difference between collection and collections?

collection is an interface and collections is an class in Java.

42.List the methods present in collections class

Below are some important methods present in collections class,

  • public static void Copy(List destination , List source)
  • public static List emptyList()
  • public static void fill (List listTofill , Object objToFillwith)
  • public static int frequency (Collecton c, Object o)
  • public static void reverse(List list)
  • public static rotate(List list , int distance)
  • public static shuffle(List list)
  • public static void sort(List list)
43.What are the some of important methods declared in the Map interface?

Some of the important methods declared in the Map interface are :
put(Key, value),get(Object key) ,remove(Object key), int size(),isEmpty() ,keySet(), values(); etc..

44.Which classes implements Map interface?

There are four classes which implements Map interface ,

  • HashMap
  • TreeMap
  • LinkedHashMap
  • ConcurrentHashmap
45.Write the program to sort the element of a list in their natural order

ArrayList companies = new ArrayList<>();


// Output: [Accenture,Barclays,Cognizant]

46.What is the difference between JAR , WAR and EAR files?
  • The difference between jar and war files are :
    War : .war is an Web Application archive and contains JSPs, “normal” HTTP served files (HTML, images, etc.), servlets, tag libraries .War files are intended to contain complete Web applications.
  • JAR : .jar is Java Application Archive that mainly runs a desktop application on a user’s machine. Jar files are intended to hold generic libraries of Java classes, resources, auxiliary files, etc.
  • EAR : .ear is an Enterprise Application archive and may contain ejb JAR files, WAR files, and RAR (connector) files.Each type of file (.jar, .war, .ear) is processed uniquely by application servers, servlet containers etc.

Leave a Reply

Your email address will not be published. Required fields are marked *