Java for Testers Interview Questions And Answers – Part 8

Java for Testers Interview Questions And Answers – Part 8

Java for Testers – Interview Questions and Answers Part-8

1) What are the additional features in Java 8?

– Lambda Expressions: Lambda expressions gives the ability to pass a functionality as a method argument. Lambda expression help us reduce the code clutter in using a single method class.
– Pipelines and Streams: Pipeline and streams will make our task easier in accessing the elements from collections and applying operations on it.
– Date and Time API: This helps in handling date and time operations in an easier way.
– Default Methods: Default methods gives the ability to add default implementation for methods in an interface.
– Type Annotations: Annotations can be applied wherever a type is used like in new instance creates, exception throws clause.

2) What is the difference between for and for-each loops in Java?

– for loop is a control structure for specifying iteration that allows code to be repeatedly executed but foreach loop is a control structure for traversing items in an array or a collection.
– for loop can be used to retrieve a particular set of elements but foreach loop cannot be used to retrieve a particular set of elements.
– for loop is harder to read and write than the foreach loop but foreach loop is easier to read and write than the for loop.
– for loop is used as a general purpose loop but foreach loop is used for arrays and collections.

3) Can we have multiple public classes inside a single class in Java?

Yes. There are two ways of implementing multiple classes:
– Nested Classes
– Multiple non-nested classes

4) What are the different types of inheritance in Java?

– Single Inheritance : In single inheritance, subclasses inherit the features of one superclass.
– Multilevel Inheritance : In Multilevel Inheritance, a derived class will be inheriting a base class and as well as the derived class also act as the base class to other class.
– Hierarchical Inheritance : In Hierarchical Inheritance, one class serves as a superclass (base class) for more than one sub class.

5) What is Polymorphism, give example and how can we achieve it?

Polymorphism allows us to perform a single action in different ways. In other words, polymorphism allows you to define one interface and have multiple implementations.
In Java polymorphism is mainly divided into two types:
– Compile Time Polymorphism
– Run Time Polymorphism

6) Can we achieve method overloading when the two methods have only difference in return type?

No. The compiler will give error as the return value alone is not sufficient for the compiler to figure out which function it has to call. Only if both methods have different parameter types (so, they have different signature), then Method overloading is possible.

7) What is Garbage Collection in Java and how exactly it is done?

Garbage collection (GC), as its name implies, is a means of freeing space occupied by waste materials, or garbage, and avoid memory leaks. Through performing the GC mechanism, available memory can be effectively used. Moreover, through this process, objects that are dead or unused for a long time in the memory heap will be deleted and the memory space used by these objects will be reclaimed.
Garbage collection in Java is an automatic process and the programmer does not have to explicitly mark objects to be deleted. The implementation mainly lives in the JVM. Each JVM can implement garbage collection. The only requirement is that it should meet the JVM specification.

8) What is encapsulation in Java?

Encapsulation is defined as the wrapping up of data under a single unit. It is the mechanism that binds together code and the data it manipulates.
In encapsulation, the variables or data of a class is hidden from any other class and can be accessed only through any member function of own class in which they are declared.
Encapsulation can be achieved by declaring all the variables in the class as private.

9) What is IS-A and Has-A relationship in Java with examples?

In Object oriented programming, IS-A relationship denotes “one object is type of another”. IS-A relation denotes Inheritance methodology.
In Object orientation design, We can say “class one is in Has-A relationship with class B if class A holds reference of Claas B”.

10) What is super and final keywords in Java and the difference between them?

Super is a keyword of Java which refers to the immediate parent of a class and is used inside the subclass method definition for calling a method defined in the superclass. A superclass having methods as private cannot be called. Only the methods which are public and protected can be called by the keyword super. It is also used by class constructors to invoke constructors of its parent class.
Final is a keyword in Java that is used to restrict the user and can be used in many respects. Final can be used with:
– Class
– Methods
– Variables

11) Explain run time polymorphism and compile time polymorphism with examples?

– Compile time Polymorphism: It is also known as static polymorphism. This type of polymorphism is achieved by function overloading or operator overloading.

// Java program for Method overloading
class OverloadedMethods {
// Method with 2 parameter
static int Multiply(int a, int b)
return a * b;
// Method with the same name but 3 parameter
static int Multiply(int a, int b, int c)
return a * b * c;
class Main {
public static void main(String[] args)


– Runtime Polymorphism: It is also known as Dynamic Method Dispatch. It is a process in which a function call to the overridden method is resolved at Runtime. This type of polymorphism is achieved by Method Overriding.

// Java program for Method overridding
class Parent {
void Print()
System.out.println("parent class");
class subclass1 extends Parent {
void Print()
class TestPolymorphism {
public static void main(String[] args)
Parent a;
a = new subclass1();

Output: subclass1

12) Can final methods be overloaded?

Yes. It is possible to overload the final methods.


public final void addNumbers(int a, int b){}
public final void addNumbers(float a, float b) {}


13) Can static methods be overloaded?

Yes. We can have two ore more static methods with same name, but differences in input parameters.

public class Test {
public static void print() {
System.out.println("Test.print() called ");
public static void print(int a) {
System.out.println("Test.print(int) called ");
public static void main(String args[])

Test.print() called
Test.print(int) called

14) Can final methods be overridden?
No. Any method that is declared as final in the superclass cannot be overridden by a subclass. If we try to override the final method of super class we will get an error in Java.

15) Can static methods be overridden?
No. If a derived class defines a static method with same signature as a static method in base class, the method in the derived class hides the method in the base class.

16) Can we overload a main method in Java?
Yes, We can overload the main method in java but JVM only calls the original main method, it will never call our overloaded main method.

17) Can we execute a class without a main method?
No. We cannot execute a class without a main method.

18) What is a Package in Java?
Package in Java is a mechanism to encapsulate a group of classes, sub packages and interfaces. Packages are used for:
– Preventing naming conflicts.
– Making searching/locating and usage of classes, interfaces, enumerations and annotations easier
– Providing controlled access: protected and default have package level access control.
– Data encapsulation (or data-hiding).

19) What is an Abstract Class in Java and explain with an example?
A class which contains the abstract keyword in its declaration is known as abstract class.
– Abstract classes may or may not contain abstract methods, i.e., methods without body.
– But, if a class has at least one abstract method, then the class must be declared abstract.
– If a class is declared abstract, it cannot be instantiated.
– To use an abstract class, you have to inherit it from another class, provide implementations to the abstract methods in it.
– If you inherit an abstract class, you have to provide implementations to all the abstract methods in it.

Ex –

// An example abstract class in Java
abstract class Shape {
int color;
// An abstract function
abstract void draw();


20) What is an Interface and how it is different from Abstract Class?

Like a class, an interface can have methods and variables, but the methods declared in an interface are by default abstract (only method signature, no body).
– Interfaces specify what a class must do and not how. It is the blueprint of the class.
– If a class implements an interface and does not provide method bodies for all functions specified in the interface, then the class must be declared abstract.

Interface can have only abstract methods. Abstract class can have abstract and non-abstract methods.

21) Can we use private and protect access modifiers inside an Interface?

Java interfaces are meant to specify fields and methods that are publicly available in classes that implement the interfaces. Therefore you cannot use the private and protected access modifiers in interfaces.

22) Can multiple inheritances supported in Interface?

Yes. Multiple inheritance is supported in Interfaces.

23) What is the difference between throw and throws?

– throw keyword is used to throw an exception explicitly but Throws keyword is used to declare one or more exceptions, separated by commas.
– Only single exception is thrown by using throw but multiple exceptions can be thrown by using throws.
– throw keyword is used within the method but throws keyword is used with the method signature.
– Unchecked exception can be propagated using throw but checked exception must use throws keyword followed by specific exception class name.

24) What is Exception and what is its base class?

An exception is an unwanted or unexpected event, which occurs during the execution of a program i.e at run time, that disrupts the normal flow of the program’s instructions.
All exception and errors types are sub classes of class Throwable, which is base class of hierarchy. This class is used for exceptional conditions that user programs should catch. NullPointerException is an example of such an exception.

25) What is Final, Finally and Finalize?

– Final is used to apply restrictions on class, method and variable. Final class can’t be inherited, final method can’t be overridden and final variable value can’t be changed.
– The finally keyword is used in association with a try/catch block and guarantees that a section of code will be executed, even if an exception is thrown.
– Finalize is used to perform clean up processing just before object is garbage collected.

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)

Visit me on my site (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 *