Java Interview Questions for Selenium
In this tutorial, we have collected 100+ frequently asked Java interview questions for Selenium testers. We have covered almost all the important Java interview questions which are often asked during Java technical interview round for Selenium automation testing.
During an interview, the same question can be asked in many different ways. Therefore, be confident during your interview. Interviewers are interested in listening not just the correct answer, but also test your knowledge about the topic.
Here, we have explained Java interview questions for Selenium automation testing with the best possible answers that will be helpful to get complete knowledge and tackle the interview.
Your approach towards understanding the questions and sharing your answers plays an extremely important role in an interview success.
Core Java Interview Questions based on OOPs Concepts
1. What are the core OOPs concepts or main principles of object-oriented programming?
π ° The core concepts of OOPs are
Abstraction: Protecting data of a class from being accessed by members of another class
Encapsulation: Hiding data of class from other classes
Inheritance: Using code written in a class inside other classes
Polymorphism: Using various methods with same name
2. What is Encapsulation in Java?
π ° The process of binding data and corresponding methods (behavior) together into a single unit is called encapsulation.
3. How to achieve or implement encapsulation in Java?Β
π ° There are two points thereby we can achieve or implement encapsulation in Java program.
- Declare instance variable of class as private so that it cannot be accessed directly by anyone from outside the class.
- Provide the public setter and getter methods in the class to set/modify the value of variable.
4. What is Data hiding in Java? How to achieve it programmatically?
π ° Data hiding is a technique by which we prevent to access data members (variables) directly from outside the class so that we can achieve security on data. By declaring data members (variables) as private, we can achieve or implement data hiding.
5. What is tightly encapsulated class in Java?
π ° If each variable is declared as private in the class, it is called tightly encapsulated class in Java.
6. What would happen if we do not use Encapsulation in a program?
π ° If we donβt use encapsulation in a program, fields will not be private and could be accessed by anyone from outside the class.
7. What is getter and setter method in Java?
π
° A method that is used to retrieve/get the value of a variable or return the value of the private member variable is called getter method. A method that is used for updating or setting the value of a variable is called setter method.
8. Explain inheritance.
π ° The technique of creating a new class by using an existing class functionality is called inheritance. In other words, inheritance is a process where a child class acquires all the properties and behaviors of parent class. The existing class is called parent class and the new class is called child class.
9. What is Is-A relationship in Java?
π ° Is-A relationship represents inheritance. It is implemented using βextendsβ keyword. It is used for code reusability. All the classes extends java.lang.object by default. This is a very good example of Is-A relationship.
10. Which class in Java is super class of every other class?
π ° Object class is the superclass of every other class in Java.
11. How is Inheritance implemented/achieved in Java?
π ° Inheritance can be implemented or achieved by using two keywords:
extends: extends is a keyword that is used for developing the inheritance between two classes and two interfaces.
implements: implements keyword is used for developing the inheritance between a class and interface.
12. What are the uses of inheritance?
π ° We use inheritance in Java for the following reasons:
- We can reuse the code from the base class.
- Using inheritance, we can increase features of class or method by overriding.
- Inheritance is used to use the existing features of the class.
- It is used to achieve runtime polymorphism i.e. method overriding.
13. What are the advantages of Inheritance?
π ° The advantages of inheritance are as follows:
- One of the main advantages is that we can minimize the length of the duplicate code in an application by putting the common code in superclass and sharing amongst several subclasses.
- Due to reducing the length of the code, redundancy of the application is also reduced.
14. Why Java does not support multiple inheritance through class?
π ° Multiple inheritance means that one class extends two superclasses or base classes but in Java, one class cannot extend more than one class simultaneously. At most, one class can extend only one class. Therefore, to reduce ambiguity, complexity, and confusion, Java does not support multiple inheritance through class.
For more detail, go to this tutorial: Types of Inheritance in Java
15. How does multiple inheritance implement in Java?
π ° Multiple inheritance can be implemented in Java by using the interface. A class cannot extend more than one class but a class can implement more than one interface.
16. What are the uses of super keyword in Java?
π ° super keyword is a reference variable that refers to an immediate superclass object. It is used for the following purposes:
- To refer immediate parent class instance variable.
- To call immediate parent class constructor.
- To invoke immediate superclass method.
17. Why do we use this keyword?
π ° “this” keyword is a reference variable that refers to the current class object. It holds the reference to current class object or same class object. There are six usages of Java this keyword. They are as follows:
- this reference can be used to refer to the current class instance variable.
- The keyword this is used to call the non-static method of the current class.
- this() can be used to invoke the current class constructor.
- this keyword can be used as a parameter in the method call.
- The keyword βthisβ can be used as a parameter in the constructor call.
- It can also be used to return the object of the current class from the method.
18. Is it possible to use this() and super() both in same constructor?
π ° No, Java does not allow using both super() and this() together in same constructor. As per Java specification, super() or this() must be the first statement in a constructor.
19. What are the differences between super and this keyword?
π ° Go to this tutorial: Difference between super and this keyword
20. What is method overloading in Java?
π ° When a class has more than one method having the same name but different in parameters, it is called method overloading in Java.
21. How method overloading is implemented in Java?
π ° Here is the list of rules by which method overloading can be implemented in Java. They are:
- The method name must be the same.
- Parameters must be different as
a. Data types of parameters
b. Number of parameters
c. Sequence of data type of parameters - Access specifiers can be anything or different.
- Return type can be anything or different.
- Exception thrown can be anything.
22. What are the features of method overloading?
π ° The features of method overloading are as follows:
- The call to overloaded method is bonded at compile time.
- The concept of method overloading is also known as compile-time polymorphism in java.
- Method overloading is generally done in the same class. But it can also be done in the subclass. We will have to make a relationship between the parent class and child class by using extends keyword for it.
- Method overloading in Java cannot be done by changing the return type of the method because there may occur ambiguity. But the overloaded methods can change the return type.
- The private methods can be overloaded in Java.
- The final methods can be overloaded in Java.
- The main method can also be overloaded in Java.
- Both static and instance methods can be overloaded in Java.
23. When to use method overloading in Java?
π ° Method overloading is used for the following purpose. They are:
- Method overloading is used when we need to perform same task with different parameters.
- Method overloading is done to reuse the same method name.
- It is used to achieve the compile-time polymorphism in Java.
24. Why method overloading is not possible by changing return type of method?
π ° In Java, Method overloading cannot be done when the return type, method name, and argument list are the same because there may occur ambiguity.
25. Can we overload main() method in Java?
π ° Yes, we can overload the main() method in Java. A class can have any number of main() methods but the execution always starts from public static void main(String[] args) only.
26. What is type conversion in Java?
π ° The process of converting a value from one data type to another is known as type conversion.
27. What is implicit conversion or implicit casting?
π ° When two data types are compatible with each other, Java compiler performs the conversion automatically or implicitly. This is called implicit casting.
28. What is widening?
π ° The process of converting lower data type into higher data type is called widening in java.
29. What is explicit type casting or narrowing conversion?
π ° The conversion of a higher data type into a lower data type is called narrowing conversion.
30. What is class casting in Java?
π ° The process of converting a class type into another class type having relationship between them through inheritance is called class casting.
31. What is upcasting and downcasting in Java?
π ° When the reference variable of super class refers to the object of sub class, it is known as upcasting.
When subclass reference refers to super class object, it is called downcasting.
32. What is method overriding in Java?
π ° When the method of superclass is overridden in the subclass to provide more specific implementation, it is called method overriding.
33. Why we need method overriding in Java program?
π ° We need method overriding due to mainly three reasons. They are:
- To add a new feature or properties.
- To override or change the existing functionality.
- To inherit the existing functionality.
34. What are the features of method overriding?
π
° The features of method overriding are as follows:
- Method overriding technique supports the runtime polymorphism.
- It allows a subclass to provide its own implementation of the method which is already provided by the superclass.
- Only the instance method can be overridden in Java.
- An instance variable can never be overridden in Java.
- The overriding method can not be more restrictive than the overridden method of the superclass.
- Overriding concept is not applicable for private, final, static, and main method in Java.
- Method overriding can be done by changing the covariant return type only.
- Overriding method cannot throw any checked exception
35. How do we implement method overriding in Java?
π ° To implement method overriding in a program, we need to follow the following rules. They are:
- Subclass method name must be the same as superclass method name.
- The parameters of subclass method must be the same as superclass method parameters. i.e. the method signature must be the same or matched.
- Must be Is-A relationship (Inheritance).
- Subclass methodβs access modifier must be the same or higher than superclass method access modifier.
36. Why can’t private method be overridden?
π ° We cannot override private method because when superclass method is private that will not be visible to subclass, whatever methods we writing in the subclass will be treated as a new method but not an overridden method.
37. Can we override static method in Java?
π ° No, we cannot override a static method. We cannot also override a static method with an instance method because static method is bound with class whereas an instance method is bound with an object.
38. Can we stop method overriding in Java?
π ° Yes, we can stop method overriding by declaring method as final.
39. What is the use of method overriding?
π ° Method overriding is used to achieve runtime polymorphism. It is used to change the existing functionality of the superclass method.
40. What are the differences between method overloading and method overriding?
π ° Go to this tutorial: Difference between overloading and overriding
41. Is it allowed to override an overloaded method?
π ° Yes, we can override an overloaded method in Java.
42. What is Abstraction in OOPs?
π ° Abstraction is a technique by which we can hide the data that is not required to a user. It hides all unwanted data so that users can work only with the required data.
43. What is the difference between abstraction and encapsulation?
π ° Abstraction is used to hide the implementation details whereas, encapsulation binds code and data into a single unit.
44. How to achieve or implement abstraction in Java?
π ° There are two ways by which we can achieve abstraction in java. They are:
- Abstract class (0 to 100%)
- Interface (100%)
45. What is abstract class in Java?
π ° A class that is declared with an abstract keyword is called abstract class. An abstract class has to be extended and its abstract methods must be implemented by a child class.
46. What is abstract method in Java?
π ° A method which is declared with abstract modifier and has no implementation (means no body) is called an abstract method. It does not contain any body.
47. Can an abstract class be defined without any abstract methods?
π ° Yes, it is possible. It is basically done to avoid instance creation of the class.
48. Can there be any abstract method without abstract class in Java?
π ° No, if there is any abstract method in a class then class must be declared with abstract keyword.
49. Is it allowed to declare a method abstract as well as final?
π ° No, because abstract method needs to be overridden by child class whereas, a final method cannot be overridden. Therefore, a method can be either abstract or final in Java.
50. Can we create an object of abstract class in Java?
π ° No, we cannot create an instance of abstract class in Java. This is because abstract class is not a concrete class. If you try to instantiate, you will get compile time error.
51. Can we create a reference for an abstract class?
π ° Yes, we can create a reference for an abstract class only when the object being provided the implementation for the abstract class.
52. When to use abstract class and abstract method in Java?
π ° An abstract method is generally used when the same method has to perform different tasks depending on the object calling it.
An abstract class is used when we need to share the same method to all non-abstract subclasses with their own specific implementations.
53. What is interface in Java?
π ° An interface in Java is a mechanism that is used to achieve complete abstraction. It is basically a kind of class but can have only abstract methods declaration and constants as members.
54. Is it allowed to declare an interface method as static?
π ° Yes, from Java 8 onwards, we can declare static and default methods in an interface. Prior to Java 8, it was not allowed.
55. Can an interface be final?
π ° No, because its implementation is provided by another class. A final method cannot be overridden. Therefore, an interface method cannot be declared as final.
56. What is marker interface?
π ° An interface that has no variable and method is known as marker interface. For example, serializable, cloneable, remote, etc.
57. What is the difference between abstract class and interface?
π ° Go to this tutorial: 12 Difference between abstract class and interface
58. What is the difference between class and interface?
π ° Refer to this tutorial: Class vs Interface
59. What is the difference between abstract class and class?
π
° a. We cannot create an instance of abstract class whereas, we can create an instance of class.
b. In abstract class, methods have no body. But in class, methods have body.
60. Why abstract class has constructor even though you cannot create object?
π ° We cannot create an object of abstract class but we can create an object of subclass of abstract class. When we create an object of subclass of an abstract class, it calls the constructor of subclass.
This subclass constructor has super in the first line that calls constructor of an abstract class. Thus, the constructors of an abstract class are used from constructor of its subclass.
If the abstract class doesnβt have constructor, a class that extends that abstract class will not get compiled.
61. Does Java allow us to declare private and protected modifiers for variables in an interface?
π ° No, all the variables in an interface are by default public.
62. What is polymorphism in Java?
π ° Polymorphism is a concept by which we can perform a single task in different ways. There are two types of polymorphism in java. They are: Static polymorphism and Dynamic polymorphism.
63. Explain static and dynamic polymorphism with example.
π ° A polymorphism that exhibited during compilation is called static polymorphism in java. In the static polymorphism, the behavior of a method is decided at compile-time. Therefore, this type of polymorphism is also called compile-time polymorphism.
Compile-time polymorphism can be achieved/implemented by method overloading in java. Another example of static polymorphism is constructor overloading and method hiding.
A polymorphism that is exhibited at runtime is called dynamic polymorphism in java. In dynamic polymorphism, the behavior of a method is decided at runtime, therefore, it is called runtime polymorphism.
Dynamic polymorphism can be achieved/implemented in java using method overriding. An example of runtime polymorphism is method overriding.
64. What is binding in Java?
π ° The connecting (linking) between a method call and method body/definition is called binding in java.
65. What is the difference between static binding and dynamic binding?
π ° The difference between static and dynamic binding are as follows:
- Static binding occurs at compile-time while dynamic binding occurs at runtime.
- In static binding, actual object is not used whereas, actual object is used in the dynamic binding.
- Static binding is resolved at compile time whereas, dynamic binding is resolved at runtime.
- Static binding is also called late binding because it happens during compilation whereas, dynamic binding is called early binding because it happens during runtime.
- An example of static binding is method overloading whereas, example of dynamic binding is method overriding.
- Private, static, and final methods show static binding because they cannot be overridden whereas, except private, static, and final methods, other methods show dynamic binding because they can be overridden.
Core Java Interview Questions based on Final Keyword
66. What is final in Java?
π ° Final is a keyword that is used to restrict the user in Java programming. It is a non-access modifier that can be applied to a variable, method, or class.
67. What is the final keyword in Java?
π ° Java Final keyword has three different uses:
- To create a constant.
- To prevent inheritance.
- To prevent the method from being overridden.
68. Is it allow to change the value of final variable in Java?
π ° No, Java does not allow to change the value of final variable. Once the value is defined, it cannot be changed.
69. Can a class be declared as final in Java?
π ° Yes, a class can be declared as final in Java. Once a class is declared final, it cannot be extended.
70. What is blank final variable? Can we initialize the blank final variable?
π ° A variable that is declared as final and not initialized at a time of declaration is known as a blank final variable.
Yes, if it is non-static then we can declare in the constructor. If it is static blank final variable, it can be initialized only in the static block.
71. Can we change state of object to which a final reference is pointing?
π ° Yes, we can change the state of an object to which a final reference variable is pointing but we cannot re-assign a new object to this final reference variable.
72. Can we declare a method as final in Java?
π ° Yes, we can declare a method as final but a final method cannot be overridden by a child class.
73. How can inheritance be prohibited in Java?
π ° If a class is declared as final, it cannot be extended. This will prevent the inheritance of that class in Java.
74. Is it allowed to mark main method as final?
π ° Yes, the main method can be marked as final.
75. What is the use of final class in Java?
π ° Three important uses of a final class in Java are as follows:
- To prevent inheritance because the final class cannot be extended.
- To create an immutable class like the predefined String class. We cannot make a class immutable without making it final.
- A final class is very useful when we want high security in any application.
76. Which class is the most common predefined final class object in Java?
π ° String class
Core Java Interview Questions based on Static
77. What is static in Java?
π ° Static is a keyword that is used for memory management mainly. Static means single copy storage for variables or methods.
78. Can a class be declared as static?
π ° No, a class cannot be marked as static but an inner class can be static.
79. What is the use of static keyword in Java?
π ° The purpose of using static keyword in Java programming is that we can access the data, method, or block of the class without any object creation.
80. Is it allowed to change the value of static variable in Java?
π ° Yes, we can change the value of the static variable by using a constructor, or static block but not inside a static method.
81. What is the difference between static variable and instance variable?
π ° The difference between static variable (class variable) and instance variable are as follows:
- A static variable is also known as class variable whereas, instance variable is also known as non-static variable.
- Class variables can be accessed inside the static block, instance block, static method, instance method, and method of inner class whereas instance variable can be accessed inside the instance block, instance method, and method of inner class.
- Class variable is always resolved during compile time whereas, instance variable is resolved during the runtime.
- They are not serialized in Java whereas, instance variables are serialized in Java.
82. Can we use this or super keyword in static method in Java?
π ° In entire core java, this and super keyword is not allowed inside the static method or static area.
83. Can static method be overloaded in Java?
π ° Yes, a static method can be overloaded in Java but not override it.
84. In Java, why do we use static variable?
π ° If we have a common property for all objects of a class, we use a class-level variable i.e. a static variable. This variable is loaded in memory only once at the time of class loading. So, it saves memory.
85. What is the difference between static method and instance method?
π ° The difference between static method (class method) and instance method are as follows:
- A static method is also known as class method whereas instance method is also known as non-static method.
- The only static variable can be accessed inside static method whereas, in the instance method, both static and instance variables can be accessed.
- We do not need to create the object of the class for accessing static method whereas, in the case of an instance method, we need to create the object for access.
- Class method cannot be overridden whereas, an instance method can be overridden.
86. Why a static method cannot be overridden in Java?
π ° No, we cannot override static method because a static method is resolved at compile time by Java compiler whereas method overriding is resolved at runtime by JVM because objects are only available at runtime.
You can declare static methods with the same signature in subclass, but it is not considered as overriding. It is considered a method hiding.
87. What is the use of static block in Java?
π ° The purpose of using a static block is to write that logic inside static block that is executed during the class loading. It is also used for changing the default value of static variable.
Java Interview Questions based on Constructor
88. What is the use of constructor in Java?
π ° The constructor is mainly used in a program to assign the default value of instance variables.
89. What is the use private constructor in Java?
π ° Private constructor is used to preventing other classes from accessing objects of a class. It can be also used in single tone classes where the object of the class cannot be created outside the class.
90. What is the difference between constructor and method?
π ° The difference between constructor and method is as follows:
- Constructor is used to initialize the state of an object whereas, method is used to expose the behavior of an object.
- Constructor has no return type even void also where method has both void and return type.
- If we donβt define any constructor in the class, Java Compiler provides a default constructor for that class. Whereas, method is not provided by the compiler in any case.
- Constructor name must be same as name of the class whereas, method name may or may not be the same name as the class name.
- The purpose of a constructor is to create an object of a class whereas, the purpose of a method is to execute the functionality of the application.
- Constructor can not be inherited by subclass whereas method can be inherited in subclass.
91. Can a constructor be final?
π ° Not, a constructor cannot be final.
92. What is constructor overloading in Java?
π ° Constructor overloading is a technique in Java in which a class can have more than one constructor that differ in the parameters list.
93. What is constructor chaining in Java?
π ° Constructor chaining is a technique of calling one constructor from another constructor using this and super keyword. The keyword βthisβ is used to call a constructor from another constructor within the same class whereas, keyword βsuperβ is used to call parent (super) class constructor from child (subclass) class constructor. It occurs through inheritance.
94. Why do we need default constructor in a class?
π ° Default constructor is a no-argument constructor that is automatically generated by JVM if we do not define any constructor in a class. We need at least one constructor to create an object. Therefore, Java provides a default constructor.
95. Can we inherit a constructor?
π ° No, Java does not support the inheritance of constructor.
Java Interview Questions based on Methods
96. Why do we need or use method in Java?
π ° The purpose of using methods in the Java program is to write the logic of the application which performs some specific task.
97. What is main method in Java?
π ° A main() method is an entry point to start the execution of a program. Every Java application has at least one class and at least one main method.
98. Why do we need to declare main method as static in Java?
π ° The main method is declared as static. It is called by JVM when we run a class. JVM does not know how to create an object of a class. It needs a standard way to start the execution of a program.
Therefore, the main method is declared as static so that JVM can call it using the class name which is passed on the command line.
99. Can we have more than one main() method in class?
π ° Yes, a class can have any number of main() methods but the execution always starts from public static void main(String[ ] args) only.
100. What is the difference between argument and parameter in Java?
π ° A parameter is a variable in the definition of a method whereas an argument is an actual value of this variable that is passed to the methodβs parameter.
Java Interview Questions based on Access Modifiers
101. What is the use of access modifiers in Java?
π ° Access modifiers is used to restrict the visibility/accessibility of classes, fields, methods, or constructors.
102. Which access modifiers can be used with a class?
π ° Public and default access modifiers.
103. What is the difference between public and private in Java?
π ° The difference between public and private access modifiers is as follows:
- Public can be applied to a class but private cannot be applied to class.
- Public members can be accessed from anywhere outside class whereas, private members can be accessed only within the class.
- Public members of a class can be inherited to any subclass whereas, private members cannot be inherited to subclass.
104. Can we reduce the visibility of inherited or overridden method?
π ° No, we can reduce the visibility of overridden method.
105. What will happen if we make a constructor of class private?
π ° If we make any constructor as a private, we cannot create the object of that class from another class and also cannot create the subclass of that class.
106. Can we create an object of class if we make a constructor protected?
π ° Yes, if we make constructor as protected then we can create the subclass of that class within the same package but not outside the package.
107. Which modifiers are applicable to the outer class?
π ° Public, default, final, abstract, and strictfp are five modifiers that can be applied to outer class.
108. Which modifiers cannot be applied to the method?
π ° Transient and volatile are two modifiers that cannot be applied to the method.
109. Which modifiers are not applicable to variables?
π ° Abstract, synchronized, native, and strictfp are not applicable to variables.
110. Which modifier is applicable to local variable?
π ° The only applicable modifier with local variable is final.
111. Which modifiers are applicable to the constructor?
π ° Only access modifiers are applicable to the constructor. Non-access modifiers cannot be applied.
112. What are modifiers applicable to the outer interface?
π ° Public, default, abstract, and strictfp are such modifiers that are applicable for outer interface.
113. What are non-access modifiers in Java?
π ° Abstract, final, native, static, strictfp, synchronized, transient, and volatile are non-access modifiers in Java.
Java Questions based on Class, Object, Data types, and Variables
114. What are the types of primitive data types in Java?
π ° Primitive data types are those data types whose variables can store only one value at a time. Java defines eight primitive data types: boolean, char, byte, short, int, long, char, float, and double.
115. What are the non-primitive data types in Java?
π ° Non-primitive data types are created by programmers. These data types are used to store a group of values or several values. Class, object, string, array, and interface are five non-primitive data types in Java.
116. What is the default value of the local variable?
π ° Java does not initialize local variable with any default value. Therefore, local variable will be null by default.
117. What is the difference between an object and object reference?
π ° An object is an instance of a class whereas object reference is a pointer to the object. There can be many references to the same object.
118. What are the default values of primitive data types?
π ° For boolean data type, it is false. For byte, short, int, and long, it is 0. For float and double, it is 0.0. For char, the default value is ‘u000’.
Recommended Java Interview Coding Questions
If you want more practice on Java coding questions for Selenium interview, go to this tutorial: Core Java Interview Questions
Recommended Selenium Interview Questions
- 100+ Top Selenium Interview Questions
- Automation Testing Interview Questions
- 50+ TestNG Interview Questions
- Selenium Framework Interview Questions
Hope that 100++ Category wise most important frequently asked Core Java interview questions for Selenium automation testing will be helpful to tackle interview. These questions can be asked for fresher and experienced candidates by the interviewer.
All the best!!!