Real Time Automation Testing Interview Questions - Part 1 Java

Java

Can you tell Oops concepts and relate it with your Framework?
We have Polymorphism, Inheritance, Encapsulation and Abstraction in Oops. So, we will start with

1) Abstraction-
Data Abstraction means to handle complexity by hiding unnecessary details from the user. In java, abstraction is achieved by interfaces and abstract classes. We can achieve 100% abstraction using interfaces.
In Selenium, WebDriver itself acts as an interface. Consider the below statement:
WebDriver driver = new ChromeDriver();
We initialize the Chrome Browser using Selenium Webdriver. It means we are creating a reference
variable (driver) of the interface (WebDriver) and creating an Object. Here WebDriver is an Interface and ChromeDriver is a class.
We can apply Abstraction in a Selenium framework by using the Page Object Model design pattern.
We define all our locators and their methods in the page class. We can use these locators in our tests but
we cannot see the implementation of their underlying methods. So we only show the locators in the tests but hide the implementation. This is a simple example of how we can use Data Abstraction in our
Automation Framework.

2) Encapsulation-
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. Encapsulation can be achieved by: Declaring all the variables in the class as private and writing public methods in the class to set and get the values of variables.All the classes in an Automation Framework are an example of Encapsulation. In Page Object Model classes, we declare the data members using @FindBy and initialization of data members will be done using Constructor to utilize those in methods.

3) Inheritance-
Inheritance is the mechanism in java by which one class is allowed to inherit the features (fields and methods) of another class.
We can apply Inheritance in our Automation Framework by creating a Base Class to initialize the WebDriver interface, browsers, waits, reports, logging, etc. and then we can extend this Base Class and its methods in other classes like Tests or Utilities. This is a simple example of how we can apply Inheritance in
our framework.

4) Polymorphism
Polymorphism allows us to perform a single action in different ways. In Java polymorphism can be achieved by two ways:
Method Overloading: When there are multiple methods with same name but different parameters then these methods are said to be overloaded. Methods can be overloaded by change in number of arguments or/and change in type of arguments.
In Selenium Automation, Implicit wait is an example of Method Overloading. In Implicit wait we use different time stamps such as SECONDS, MINUTES, HOURS etc.

Method Overriding: It occurs when a derived class has a definition for one of the member functions of the base class. That base function is said to be overridden. 
In Selenium Automation, Method Overriding can be achieved by overriding any WebDriver method. For example, we can override the findElement method.
In assertion we have used overload because in assertion we used to like asset.true(actual, expected) and second time we can use same assert.true(actual, expected, message).

• How can you use interface and how it is different from Abstract class?
Abstract class may have Abstract and concrete methods, and there is not any compulsion in adding abstract method in abstract class. But in Interface, we do have only abstract methods and we don’t need to write abstract keyword in Interface this is by default public and abstract.

• What do you mean by Static keyword in Java?
Static means it is at class level not at instance level, we have static method, static variable & static inner class. When we have any variable as static so it will remain same for all the instance of our classes, and static/Private/Final methods can’t be over-ridden like if we have initialized any method as Static so we cannot override it in any child class.

• How to call static method and variable in java?
Direct calling, Calling by class name.

• Can I access Static method by using object reference?
Yes we can, but we got one warning that you need to access it via Direct or By class name.

• How to call non-static method and variable in java?
For calling non static method we need to create object first.

• Can we overload & override main method?
Overload-Yes, Override-No

• What do you mean by wrapper class and how will you do data conversion?
Wrapper class in java are used for data conversion. In data conversion if user wants to convert Int to
string, String to int, Boolean, double then we use Wrapper class.
integer.parseInt(); - To convert string to Integer
Double.parseDouble(); - To convert string to Double
Boolean.parse Boolean(); - To convert string to Boolean
String.valueof(); - To convert Integer to String.

• Can you convert string a =”a2201” in integer?
No we get NumberFormatException while converting the above string.

• What do you mean by Call by Value & Call by Reference in Java?
Call by value means suppose we have created one sum method with input parameter int a, int b. So while calling the creating the object and running we provide values that is know as call by value.

• What do you mean by Exceptions in Java?
Exception is like any interruption in our normal flow. Like if we are running anything and we get issues in our script this is we called exception, we have 2 types of exception Run Time & Compile Time. (checked & Unchecked exceptions)

• Can you tell me about difference between Throw and Throws keyword?
Throw is a keyword used inside a body of function. And Throws used while initializing any method. By
using Throw we can throw only one exception while for Throws we can declare multiple exceptions which might occur in that particular function. Throws keyword followed by instance name and Throw keyword is followed by class name of that exception.

• Why the main method is static?
Java main() method is always static, so that compiler can call it without the creation of an object or before the creation of an object of the class. Static method of a class can be called by using the class name only without creating an object of a class.

• What is Run time polymorphism ?
Run-Time Polymorphism: Whenever an object is bound with the functionality at run time, this is known
as runtime polymorphism. The runtime polymorphism can be achieved by method overriding. Java
virtual machine determines the proper method to call at the runtime, not at the compile time.

• Difference between list and set.
The main difference between List and Set is that Set is unordered and contains different elements,
whereas the list is ordered and can contain the same elements in it.

• Use of constructor.
The purpose of constructor is to initialize the object of a class while the purpose of a method is to
perform a task by executing java code. Constructors cannot be abstract, final, static and synchronised
while methods can be. Constructors do not have return types while methods do.

• Difference between static and non-static methods
Static method uses complie time binding or early binding. Non-static method uses run time binding or
dynamic binding. A static method cannot be overridden being compile time binding. A non-static
method can be overridden being dynamic binding.

• Can we declare many interfaces object class inside the interface class.
Yes, you can define a class inside an interface. In general, if the methods of the interface use this class and if we are not using it anywhere else we will declare a class within an interface.

• What is a super keyword in java?
The super keyword refers to superclass (parent) objects. It is used to call superclass methods, and to
access the superclass constructor. The most common use of the super keyword is to eliminate the
confusion between superclasses and subclasses that have methods with the same name.

• Difference between break and continue statement.
Break statement resumes the control of the program to the end of loop and made executional flow
outside that loop. Continue statement resumes the control of the program to the next iteration of that
loop enclosing 'continue' and made executional flow inside the loop again.

• Diff between Abstract class & interface?
Abstract class can inherit another class using extends keyword and implement an interface. Interface can inherit only an inteface. Abstract class can be inherited using extends keyword. Interface can only be implemented using implements keyword.

• What is a static keyword in Java?
In the Java programming language, the keyword static indicates that the particular member belongs to a
type itself, rather than to an instance of that type. This means that only one instance of
that static member is created which is shared across all instances of the class.

• What is the difference between checked and unchecked exceptions?
There are two types of exceptions: checked exception and unchecked exception.The main difference
between checked and unchecked exception is that the checked exceptions are checked at compile-time
while unchecked exceptions are checked at runtime.
Eg. checked exceptions – SQLException,IOException,ClassNotFoundException,InvocationTargetException
unchecked exceptions- NullPointerException,ArrayIndexOutOfBoundsException,ArithmeticException,IllegalArgumentException, NumberFormatException

Difference between this and super?
this keyword mainly represents the current instance of a class. On other hand super keyword represents
the current instance of a parent class. this keyword used to call default constructor of the same class.

• What is the difference between length and length() in Java?
The length is an instance variable of an array in Java whereas length() is a method of String class.

• What is an abstract class & Abstract Methods?
 Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class). 
Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by the subclass (inherited from).

• What are hashmap and HashSet? Explain?
HashMap and HashSet both are one of the most important classes of Java Collection framework.
HashMap Stores elements in form of key-value pair i.e each element has its corresponding key which is
required for its retrieval during iteration. HashSet stores only objects no such key value pairs maintained.

• What is a singleton class?
The Singleton's purpose is to control object creation, limiting the number of objects to only one. Since
there is only one Singleton instance, any instance fields of a Singleton will occur only once per class, just like static fields. Singletons often control access to resources, such as database connections or sockets. For example, if you have a license for only one connection for your database or your JDBC driver has trouble with multithreading, the Singleton makes sure that only one connection is made or that only one thread can access the connection at a time.

• When finally block get executed?
The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs.

• How many times you can write catch block?
maximum one catch block will be executed. No, we can write multiple catch block but only one is
executed at a time.

• 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:
1. this reference can be used to refer to the current class instance variable.
2.this keyword is used to call the non-static method of the current class.
3. this() can be used to invoke the current class constructor.
4. this keyword can be used as a parameter in the method call.
5. The keyword “this” can be used as a parameter in the constructor call.
6. It can also be used to return the object of the current class from the method.

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

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

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


I will keep adding more questions to this.

for java coding questions, please click here.
for selenium questions, please click here.
for Testng questions,please click here.

Comments

Popular posts from this blog

End-to-End Testing with Cypress using Cucumber precprocessor

Software Testing and Its Types

Real Time Automation Testing Interview Questions- Part 2- Selenium