Java Interview Questions and Answers for 3 years experience

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.

 

1.What is object in Java?

Object is an instance of class which has a state and behavior. We can create any number of objects of a class.

2.What is a Class in Java?

A class can be defined as a template/blueprint that describes the behavior/state of the object of its type.

3.What are the different types OOPS concepts you know?

Different OOPS concepts are :

  • Polymorphism
  • Inheritance
  • Encapsulation
  • Abstraction

4.What is inheritance in Java?

Inheritance can be defined as the process where one class acquires the properties of another class.The class which inherits the properties of other is known as subclass/child class and the class whose properties had been inherited is known as super/parent class.

5.Can we access the members of child class when object is created using super class reference?

No, We cannot access member of child class when object is created using super class reference.
Example:

Fruit //parent class
Apple // Child class
Fruits demo = new Apple(); //object creation

Here using “demo” object you can access variable and methods of parent class only (Fruit class) but not child class (apple) variable and methods, however if you have used same method name and variable name in child class which are already present in parent class then method of parent class will be overridden (Dynamic polymorphism) and variable will not be overloaded as it is not allowed in Java.

6.Explain super keyword in Java?

Super keyword is used in Java for below two reasons :

To differentiate the members between super class and sub class ,if the members of the super class have the same names as that of sub class, to differentiate these variables and methods we use super keyword.
Example:
super.a; //using variable of super class
super.add(); //calling super class method
Invoking the super class constructor from subclass.
Example:
Fruit //Super class
Apple //Child classIn the child class constructor we will write below code , when constructor of child class is called super class constructor will also be get called implicitly if we have not called it explicitly and code written in site it will be executed first.
public class Apple extends Fruit {
Apple(int age) { //child class constructor.
super(age); //calling super class constructor.}               

}

7.Can we override private methods of super class?

No, we cannot override private or final methods of super class.

8.Can we override super class method when subclass is in different package?

Yes, we can override method when subclass is in different package with condition that , super class methods are public/Protected and not final or private.

9.Can we override constructor?

No, we cannot override constructor.

10.Is method overloading possible by changing the return type of method only?

No, method overloading is not possible by changing only the return type of method.

11.Can we overload java main() method?

Yes, we can overload main method but JVM calls main() method which receives string array as arguments only.

12.Can you override a private method in Java?

No,its not possible to override private method in Java because it is not accessible in child class.

13.How can you stop any one to create an object of any class outside it?

By declaring the constructor of the class as private.

14.How will you achieve HAS-A relationship?

We can achieve HAS-A relationship by help of aggregation. When any object contain one or more object inside it.
Example:
class Apple(
int a;
}

class Box {
int id;
String name;
Apple obj2; //Apple is a class.
}

15.Why we use aggregation?

To reuse the code and when there is no IS-A relation then we use aggregation.

16.Inheritance denotes which type of relationship?

Inheritance denoted IS-A relationship.

17.Can we overload the final method in Java?

Yes, we overload the final method in Java.

18.Can we override an abstract method?

Yes ,is mandatory to override abstract method in implementing class and we can override if required in extending class.

19.Can you override a method in the same class?

No, we cannot override the method in the same class.

20.Can a main() method be declared final?

Yes, we can declare main method final. If we make this final it can not be overridden and compiler will still find it.

21.Can a main() method be declared final?

Yes, we can declare main method final. If we make this final it can not be overridden and compiler will still find it.

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<>();
companies.add(“Barclays”);
companies.add(“Accenture”);
companies.add(“Cognizant”);

Collections.sort(companies);
System.out.println(companies);

// 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.

 

Author: Saad Mansuri

Leave a Reply

Your e-mail address will not be published. Required fields are marked *