Java Interview Questions for selenium

What are the principle concepts of OOPS ?

There are four principle concepts upon which object-oriented design and programming rest. They are:
1. Abstraction
2. Polymorphism
3. Inheritance
4. Encapsulation
(i.e., easily remembered as A-PIE).

What is Abstraction?

Abstraction refers to the act of representing essential features without including the background details or explanations.

What is Encapsulation?

Encapsulation is a technique used for hiding the properties and behaviors of an object and allowing outside access only as appropriate. It prevents other objects from directly altering or accessing the properties or methods of the encapsulated object.

What is the difference between abstraction and encapsulation?

1. Abstraction focuses on the outside view of an object (i.e., the interface) Encapsulation (information hiding) prevents clients from seeing it's inside view, where the behavior of the abstraction is implemented.
2. Abstraction solves the problem on the design side while Encapsulation is the Implementation.
3. Encapsulation is the deliverables of Abstraction. Encapsulation barely talks about grouping up your abstraction to suit the developer needs

What is Inheritance?

1. Inheritance is the process by which objects of one class acquire the properties of objects of another class.
2. A class that is inherited is called a superclass.
3. The class that does the inheriting is called a subclass.
4. Inheritance is done by using the keyword extends.
5. The two most common reasons to use inheritance are:

i. To promote code reuse

ii. To use polymorphism

What is Polymorphism?

Polymorphism is briefly described as "one interface, many implementations." Polymorphism is a characteristic of being able to assign a different meaning or usage to something in different contexts - specifically, to allow an entity such as a variable, a function, or an object to have more than one form.

How does Java implement polymorphism?

(Inheritance, Overloading and Overriding are used to achieve Polymorphism in java). Polymorphism manifests itself in Java in the form of multiple methods having the same name.

1. In some cases, multiple methods have the same name, but different formal argument lists (overloaded methods).
2. In other cases, multiple methods have the same name, same return type, and the same formal argument list (overridden methods).

Difference between assert and verify in selenium web driver?

When an 'assert' fails, the test will be aborted. Assert is best used when the check value has to pass for the test to be able to continue to run login.

Where if a 'verify' fails, the test will continue executing and logging the failure.
Verify is best used to check non-critical things. Like the presence of a headline element.

Explain the different forms of Polymorphism.

There are two types of polymorphism one is Compile time polymorphism, and the other is run time polymorphism.
Compile-time polymorphism is method overloading. Runtime time polymorphism is done using inheritance and interface. Note: From a practical programming viewpoint, polymorphism manifests itself in three distinct forms in Java:

1. Method overloading
2. Method overriding through inheritance
3. Method overriding through the Java interface

What is runtime polymorphism or dynamic method dispatch?

In Java, runtime polymorphism or dynamic method dispatch is a process in which a call to an overridden method is resolved at runtime rather than at compile-time.

In this process, an overridden method is called through the reference variable of a superclass.

The determination of the method to be called is based on the object being referred to by the reference variable.

Learn about Inheritance in Java & Selenium

What is Dynamic Binding?

Binding refers to the linking of a procedure call to the code to be executed in response to the call.
Dynamic binding (also known as late binding) means that the code associated with a given procedure call is not known until the time of the call at run-time.

It is associated with polymorphism and inheritance.

What is method overloading?

Method Overloading means to have two or more methods with the same name in the same class with different arguments.

The benefit of method overloading is that it allows you to implement methods that support the same semantic operation but differ by argument number or type. Note:
1. Overloaded methods MUST change the argument list
2. Overloaded methods CAN change the return type
3. Overloaded methods CAN change the access modifier
4. Overloaded methods CAN declare new or broader checked exceptions
5. A method can be overloaded in the same class or a subclass

What is method overriding?

Method overriding occurs when the subclass declares a method that has the same type of arguments as a method declared by one of its superclass. The key benefit of overriding is the ability to define behavior that's specific to a particular subclass type.

1. The overriding method cannot have a more restrictive access modifier than the method being overridden (Ex: You can't override a method marked public and make it protected).
2. You cannot override a method marked final
3. You cannot override a method marked static

How do I declare and initialize an array in Java?

You can either use array declaration or array literal (but only when you declare and affect the variable right away, array literals cannot be used for re-assigning an array).

For primitive types:
int[] myIntArray = new int[3];
int[] myIntArray = {1,2,3};
int[] myIntArray = new int[]{1,2,3};

For classes, for example String, it's the same:
String[] myStringArray = new String[3];
String[] myStringArray = {"a","b","c"};
String[] myStringArray = new String[]{"a","b","c"};

Write a program to reverse a string using a recursive algorithm

Write a program to reverse a string using recursive methods.
You should not use any string reverse methods to do this.

public class StringRecursiveReversal {
	String reverse = "";
	public String reverseString(String str){
		if(str.length() == 1){
			return str;
		} else {
			reverse += str.charAt(str.length()-1)+reverseString(str.substring(0,str.length()-1));
			return reverse;

	public static void main(String a[]){
		StringRecursiveReversal srr = newStringRecursiveReversal();
		System.out.println("Result: "+srr.reverseString("SeleniumWebdriver"));
Write a program to reverse a string using a recursive algorithm

Write a program to reverse a number using numeric operations. The below example shows how to reverse a number using numeric operations.

public class NumberReverse {
	publicint reverseNumber(int number){
		int reverse = 0;
		while(number != 0){
			reverse = (reverse*10)+(number%10);
			number = number/10;
		return reverse;
	public static void main(String a[]){
		NumberReverse nr = newNumberReverse();
		System.out.println("Result: "+nr.reverseNumber(17868));
Can overloaded methods be overridden too?

Yes, derived classes still can override the overloaded methods. Polymorphism can still happen.
The compiler will not binding the method calls since it is overloaded because it might be overridden now or in the future.

Is it possible to override the main method?

NO, because the main is a static method. A static method can't be overridden in Java.

How to invoke a superclass version of an Overridden method?

To invoke a superclass method that has been overridden in a subclass, you must either call the method directly through a superclass instance or use the super prefix in the subclass itself.

From the point of view of the subclass, the super prefix provides an explicit reference to the superclass' implementation of the method.


How to decide to go for Static or non-static variables/methods?

There will be situations where we have to choose either to go for static or non-static. Answer below question in yes or no format, based on the result you choose to go for Static/Non-Static

1. Are you going to Edit the frequently?
2. Do you want to reflect the variable changes throughout your application?
3. Do you want to create an object for your Variable?
4. Do you want to support Multi-Threading?
5. Is it ok to have duplicate variables

Ask these Questions for Methods to be Static/NonStatic:
1. Are You going to access Methods using a class reference?
2. Is it ok to have duplicate objects?
3. Do you want to support Multi-Threading?
4. Are you planning for overriding ?
5. Are you planning for dynamic invoking using reflection API ?
6. Are you going to write these methods with TestNG ?

What is super?

super is a keyword that is used to access the method or member variables from the superclass.

If a method hides one of the member variables in its superclass, the method can refer to the hidden variable through the use of the super keyword.

In the same way, if a method overrides one of the methods in its superclass, the method can invoke the overridden method through the use of the super keyword.

1. You can only go back one level.
2. In the constructor, if you use super(), it must be the very first code, and you cannot access any this.path variables or methods to compute its parameters.

What is an Interface?

An interface is a description of a set of methods that conforming to implementing classes must-have.

1. You can't mark an interface as final.
2. Interface variables must be static.
3. An Interface cannot extend anything but other interfaces.

How do you prevent a method from being overridden?

To prevent a specific method from being overridden in a subclass, use the final modifier on the method declaration, which means "this is the final implementation of this method", the end of its inheritance hierarchy.

public final void exampleMethod() { // Method statements }

Can we instantiate an interface?

You can't instantiate an interface directly, but you can instantiate a class that implements an interface.

Can we create an object for an interface?

Yes, it is always necessary to create an object implementation for an interface. Interfaces cannot be instantiated in their own right, so you must write a class that implements the interface and fulfill all the methods defined in it.

Do interfaces have member variables?

Interfaces may have member variables, but these are implicitly public, static, and final- in other words, interfaces can declare only constants, not instance variables that are available to all implementations and may be used as key references for method arguments for example.

What modifiers are allowed for methods in an Interface?

Only public and abstract modifiers are allowed for methods in interfaces.

What is a marker interface?

Marker interfaces are those which do not declare any required methods but signify their compatibility with certain operations. interface and Cloneable are typical marker interfaces. These do not contain any methods, but classes must implement this interface in order to be serialized and de-serialized.

What is an abstract class?

Abstract classes are classes that contain one or more abstract methods. An abstract method is a method that is declared but contains no implementation.

1. If even a single method is abstract, the whole class must be declared abstract.
2. Abstract classes may not be instantiated, and require subclasses to provide implementations for the abstract methods.
3. You can't mark a class as both abstract and final.

Can we instantiate an abstract class?

An abstract class can never be instantiated. Its sole purpose is to be extended (subclassed).

When should I use abstract classes, and when should I use interfaces?

Use Interfaces when...
1. You see that something in your design will change frequently.
2. If various implementations only share method signatures, then it is better to use Interfaces.
3. you need some classes to use some methods which you don't want to be included in the class, then you go for the interface, which makes it easy just to implement and make use of the methods defined in the interface.

Use Abstract Class when...
1. If various implementations are of the same kind and use common behavior or status, then abstract class is better to use.
2. When you want to provide a generalized form of abstraction and leave the implementation task with the inheriting subclass.
3. Abstract classes are an excellent way to create planned inheritance hierarchies. They're also a good choice for nonleaf classes in class hierarchies.

When you declare a method as abstract, can other nonabstract methods access it?

Yes, other nonabstract methods can access a method that you declare as abstract.

Can there be an abstract class with no abstract methods in it?

Yes, there can be an abstract class without abstract methods.

What is Constructor?

1. A constructor is a special method whose task is to initialize the object of its class.
2. It is special because its name is the same as the class name.
3. They do not have return types, not even void, and therefore, they cannot return values.
4. They cannot be inherited, though a derived class can call the base class constructor.
5. Constructor is invoked whenever an object of its associated class is created.

How does the Java default constructor be provided?

If a class defined by the code does not have any constructor, the compiler will automatically provide one no-parameter-constructor (default-constructor) for the class in the byte code.

The access modifier (public/private/etc.) of the default constructor is the same as the class itself.

Can constructor be inherited?

If a class defined by the code does not have any constructor, the compiler will automatically provide one no-parameter-constructor (default-constructor) for the class in the byte code.

The access modifier (public/private/etc.) of the default constructor is the same as the class itself.

Can constructor be inherited?

No, a constructor cannot be inherited, though a derived class can call the base class constructor.

How is this() and super() used with constructors?

1. Constructors use this to refer to another constructor in the same class with a different parameter list.
2. Constructors use super to invoke the superclass's constructor. If a constructor uses super, it must use it in the first line; otherwise, the compiler will complain.

What are Access Specifiers?

One of the techniques in object-oriented programming is encapsulation. It concerns the hiding of data in a class and making this class available only through methods. Java allows you to control access to classes, methods, and fields via so-called access specifiers.

What are Access Specifiers available in Java?

Java offers four access specifiers, listed below in decreasing accessibility:

1. Public- public classes, methods, and fields can be accessed from everywhere.
2. Protected- protected methods and fields can only be accessed within the same class to which the methods and fields belong, within its subclasses, and within classes of the same package.
3. Default(no specifier)- If you do not set access to a specific level, then such a class, method, or field will be accessible from inside the same package to which the class, method, or field belongs, but not from outside this package.
4. Private- private methods and fields can only be accessed within the same class to which the methods and fields belong. private methods and fields are not visible within subclasses and are not inherited by subclasses.

What is the final modifier?

The final modifier keyword makes that the programmer cannot change the value anymore. The actual meaning depends on whether it is applied to a class, a variable, or a method. 1. final Classes- A final class cannot have subclasses.
2. final Variables- A final variable cannot be changed once it is initialized.
3. final Methods- A final method cannot be overridden by subclasses.

What are the uses of the final method?

There are two reasons for marking a method as final:

1. Disallowing subclasses to change the meaning of the method.
2. Increasing efficiency by allowing the compiler to turn calls to the method into inline Java code.

What is a static block?

A static block which exactly executed exactly once when the class is first loaded into JVM. Before going to the main method the static block will execute.

What are the static variables?

Variables that have only one copy per class are known as static variables. They are not attached to a particular instance of a class but rather belong to a class as a whole.

They are declared by using the static keyword as a modifier.
static type varIdentifier;

where the name of the variable is varIdentifier, and its data type is specified by type.

Note: Static variables that are not explicitly initialized in the code are automatically initialized with a default value.
The default value depends on the data type of the variables.

What is the difference between static and non-static variables?

A static variable is associated with the class as a whole rather than with specific instances of a class.

Non-static variables take on unique values with each object instance.

What are the static methods?

Methods declared with the keyword static as modifiers are called static methods or class methods.
They are so-called because they affect a class as a whole, not a particular instance of the class.

Static methods are always invoked without reference to a particular instance of a class.

Note:The use of a static method suffers from the following restrictions:

1. A static method can only call other static methods.
2. A static method must only access static data.
3. A static method cannot reference to the current object using keywords super or this.

What is an Iterator ?

1. The Iterator interface is used to step through the elements of a Collection.
2. Iterators let you process each element of a Collection.
3. Iterators are a generic way to go through all the elements of a Collection no matter how it is organized.
4. Iterator is an Interface implemented in a different way for every Collection.

How do you traverse through a collection using its Iterator?

To use an iterator to traverse through the contents of a collection, follow these steps:

1. Obtain an iterator to the start of the collection by calling the collection’s iterator()method.
2. Set up a loop that makes a call to hasNext(). Have the loop iterate as long ashasNext() returns true.
3. Within the loop, obtain each element by calling next().

How do you remove elements during the Iteration?

Iterator also has a method remove() when remove is called, the current element in the iteration is deleted.

How is ListIterator?

ListIterator is just like Iterator, except it allows us to access the collection in either the forward or backward direction and lets us modify an element.

What is the List interface?

1. The List interface provides support for ordered collections of objects.
2. Lists may contain duplicate elements.

What are the main implementations of the List interface ?

The main implementations of the List interface are as follows :

1. ArrayList : Resizable-array implementation of the List interface. The best all-around implementation of the List interface.

2. Vector : Synchronized resizable-array implementation of the List interface with additional "legacy methods."

3. LinkedList : Doubly-linked list implementation of the List interface. May provide better performance than the ArrayList implementation if elements are frequently inserted or deleted within the list. Useful for queues and double-ended queues (deques).

What are the advantages of ArrayList over arrays?

Some of the advantages ArrayList has over arrays are:

1. It can grow dynamically
2. It provides more powerful insertion and search mechanisms than arrays.

Why insertion and deletion in ArrayList are slow compared to LinkedList?

1. ArrayList internally uses and array to store the elements when that array gets filled by inserting elements a new array of roughly 1.5 times the size of the original array is created, and all the data of old array is copied to a new array.

2. During deletion, all elements present in the array after the deleted elements have to be moved one step back to fill the space created by deletion.

In linked list data is stored in nodes that have reference to the previous node and the next node, so adding element is simple as creating the node an updating the next pointer on the last node and the previous pointer on the new node.

Deletion in a linked list is fast because it involves only updating the next pointer in the node before the deleted node and updating the previous pointer in the node after the deleted node.

Why does ArrayList call Fail Fast return iterators?

Because, if a list is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException.

Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

How do you decide when to use ArrayList and When to use LinkedList?

If you need to support random access, without inserting or removing elements from any place other than the end, then ArrayList offers the optimal collection.

If, however, you need to frequently add and remove elements from the middle of the list and only access the list elements sequentially, then LinkedList offers a better implementation.

What is the Set interface?

1. The Set interface provides methods for accessing the elements of a finite mathematical set
2. Sets do not allow duplicate elements
3. Contains no methods other than those inherited from Collection
4. It adds the restriction that duplicate elements are prohibited<br style="box-sizing: in

About Author :

I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions
  • karthiQ [ admin]
    Thanks mike for kind words, 
    Please have a look at below urls
  • Mike
    I learned through everything in your site and I love it, just want to say thank you so much for your great work, God bless you and Chercher Tech :)
     Maybe you can add the Cucumber Framework and API Automation as well, they're getting popular nowadays