Java for Testers Interview Questions And Answers – Part 5

Java for Testers Interview Questions And Answers – Part 5

Java for Testers – Interview Questions and Answers Part-5

1) What is the difference between List and Set?

– List in Java allows duplicates while Set doesn’t allow duplicates.
– List is an Ordered Collection while Set is an unordered Collection. List maintains insertion order of elements, means any element which is inserted before will go on lower index than any element which is inserted after. Set in Java doesn’t maintain any order.
– Set uses equals() method to check uniqueness of elements stored in Set, while SortedSet uses compareTo() method to implement natural sorting order of elements.
– Popular implementation of List interface in Java includes ArrayList, Vector and LinkedList. While popular implementation of Set interface includes HashSet, TreeSet and LinkedHashSet.

2) What is the purpose of using Constructors in Java?

Constructors are used to assign values to the class variables at the time of object creation, either explicitly done by the programmer or by Java itself (default constructor).

3) How Constructors are different from Methods in Java?

– Name of the constructor must be same with the name of the Class but there is no such requirement for a method in Java. Methods can have any arbitrary name in Java.
– Constructor doesn’t have any return type but the method has the return type and returns something unless its void.
– Constructors are chained and they are called in a particular order, there is no such facility for methods.
– Constructors are not inherited by child classes but methods are inherited by child classes until they are made private. in which case they are only visible in class on which they are declared.

4) What is the purpose of using ‘this’ keyword in Java?

Below are the different usage of this keyword:

– this can be used to refer current class instance variable.
– this can be used to invoke current class method (implicitly)
– this() can be used to invoke current class constructor.
– this can be passed as an argument in the method call.
– this can be passed as argument in the constructor call.
– this can be used to return the current class instance from the method.

5) What is Overloading in Java?

Overloading allows different methods to have the same name, but different signatures where the signature can differ by the number of input parameters or type of input parameters or both. Overloading is related to compile-time (or static) polymorphism.

6) What is the purpose of using Packages in Java?

Package are used in Java, in-order to avoid name conflicts and to control access of class, interface and enumeration etc.

7) What is the keyword used by a Java class to inherit the properties say variables and methods of another Class?

The keyword used for inheritance is extends.

class derived-class extends base-class
//methods and fields


8) How to access the variables and methods of another Class in Java?

By creating an instance of another class.

class A
int i = 0,j = 1;

class B
//Accessing variables of class A by creating an instance of the class A
A a1 = new A():
int a = a1.i;
int b = a1.j;


9) What is Overriding in Java?

Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes. When a method in a subclass has the same name, same parameters or signature and same return type(or sub-type) as a method in its super-class, then the method in the subclass is said to override the method in the super-class.

10) Is Overriding applicable for Constructors in Java?

Constructors cannot be overriden in java.

11) What are the different modifiers in Java?

– Default: The data members, class or methods which are not declared using any access modifiers i.e. having default access modifier are accessible only within the same package.
– Private: The methods or data members declared as private are accessible only within the class in which they are declared.
– Protected: The methods or data members declared as protected are accessible within same package or sub classes in different package.
– Public: Classes, methods or data members which are declared as public are accessible from every where in the program. There is no restriction on the scope of a public data members.

12) What is the difference between default and protected access modifiers in Java?

A Default member may be accessed only if the Class accessing the member belongs to the same Package only where as a Protected member can be accessed (through Inheritance) by a SubClass even if the SubClass is in a different Package.

13) What is the difference between static and instance variable in Java?

In case of static variable, each and every instance of the class will share the same variable, so that if you change it in one instance, the change will reflect in all instances, created either before or after the change.

Instance variables belong to the instance of a class, thus an object. And every instance of that class (object) has it’s own copy of that variable. Changes made to the variable don’t reflect in other instances of that class.

14) What is the difference between static and non-static methods in Java?

– A static method can access only static members and can not access non-static members but a non-static method can access both static as well as non-static members.
– Static method uses complie time binding or early binding but Non-static method uses run time binding or dynamic binding.
– A static method cannot be overridden being compile time binding but a non-static method can be overridden being dynamic binding.
– Static method occupies less space and memory allocation happens once but a non-static method may occupy more space.
– A static method is declared using static keyword but a normal method is not required to have any special keyword.

15) What happens when we specify the final non-access modifier with variables and methods in Java?

A final variable can be explicitly initialized only once. A reference variable declared final can never be reassigned to refer to a different object.
A final method cannot be overridden by any subclasses. The final modifier prevents a method from being modified in a subclass.

16) What is the difference between abstract classes and interfaces in Java?

– Type of methods: Interface can have only abstract methods. Abstract class can have abstract and non-abstract methods.
– Final Variables: Variables declared in a Java interface are by default final. An abstract class may contain non-final variables.
– Type of variables: Abstract class can have final, non-final, static and non-static variables. Interface has only static and final variables.
– Implementation: Abstract class can provide the implementation of interface. Interface can’t provide the implementation of abstract class.
– Inheritance vs Abstraction: A Java interface can be implemented using keyword “implements” and abstract class can be extended using keyword “extends”.
– Multiple implementation: An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces.
– Accessibility of Data Members: Members of a Java interface are public by default. A Java abstract class can have class members like private, protected, etc.

17) What is the keyword used for inheriting the interfaces in Java?

Extends keyword is used for inheriting the interfaces.

class A
int i = 0,j = 1;

class B
//Accessing variables of class A by creating an instance of the class A
A a1 = new A():
int a = a1.i;
int b = a1.j;


18) How to handle exceptions in Java?

There are 5 keywords which are used in handling exceptions in Java – try, catch, finally, throw and throws.

class A
int i = 0,j = 1;

class B
//Accessing variables of class A by creating an instance of the class A
A a1 = new A():
int a = a1.i;
int b = a1.j;


19) What is the difference between checked and unchecked exceptions in Java?

The classes which directly inherit Throwable class except RuntimeException and Error are known as checked exceptions e.g. IOException, SQLException etc. Checked exceptions are checked at compile-time.
The classes which inherit RuntimeException are known as unchecked exceptions e.g. ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc. Unchecked exceptions are not checked at compile-time, but they are checked at runtime.

20) What is the disadvantage of Arrays and how to overcome it in Java?

Arrays are of fixed size and cannot be increased or decreased once declared. This can be overcome by using ArrayList.

21) What is the difference between ArrayList and HashSet in Java ?

– Implementation : ArrayList implements List interface while HashSet implements Set interface in Java.
– Internal implementation: ArrayList is backed by an Array while HashSet is backed by an HashMap.
– Duplicates : ArrayList allows duplicate values while HashSet doesn’t allow duplicates values.
– Ordering : ArrayList maintains the order of the object in which they are inserted while HashSet is an unordered collection and doesn’t maintain any order.
– Indexing : ArrayList is index based we can retrieve object by calling get(index) method or remove objects by calling remove(index) method while HashSet is completely object based. HashSet also does not provide get() method.
– Null Object: ArrayList does not apply any restriction, we can add any number of null value while HashSet allow one null value.

22) What are the predefined methods of HashMap, which are used for adding the value and retrieving the value in Java?

get – It is used to retrieve or fetch the value mapped by a particular key.
put – It is used to insert a particular mapping of key-value pair into a map.

23) What will this Java code print: String x = “Latest version”; String y = “of Selenium”; int z = 3; System.out.println(“We are learning Selenium”+” and the “+x+” “+y+” is “+z); ?

Output – We are learning Selenium and the Latest version of Selenium is 3

24) Is Java case sensitive?

Yes, it is case-sensitive.

25) Which keyword is used for defining / creating a Class in Java?

Class keyword is used for creating a class in Java.

Next Steps:

> More interview questions and answers on Java, continue to the next post (Click on Next Post link below)

> Check complete Java interview questions and answers here (Click here)

Please leave your questions/comments/feedback below.

Happy Learning 🙂

About Me > Bijan Patel

Connect to me on Linked In (Click here)

On a mission to contribute to the Software Testing Community in all possible ways.


Comments (0)

Leave a Reply

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