JAVA INTERVIEW QUESTIONS For Selenium Interview
JAVA INTERVIEW QUESTIONS For Selenium Interview
============================================
What are different types of access modifiers?
Access modifiers are used while creating class, method or variable.
public: Any thing declared as public can be accessed from anywhere.
private: Any thing declared as private can be accessed inside class only can’t be seen outside of its
class.
protected: Any thing declared as protected can be accessed by classes in the same package and
subclasses in the other packages.
default modifier(It is by default) : Can be accessed only to classes in the same package.
==================================================================
What is final, finalize() and finally?
Or Explain Final keyword Or Explain Finally() or Explain Finalize()
final :
final keyword can be used for class, method and variables.
A final class cannot be inherited
A final method can’t be override.
A final variable becomes constant, we can’t change its value
finally :
finally, keyword used in exception handling.
creates a block of code that will be executed after a try/catch block has completed
The finally block will execute whether or not an exception is thrown.
Means if exception is not throws then Try execute then Finally will execute, in case when exception is
thrown finally will execute after catch e
Example : Like we have written DB connection code in try block but exception comes
In that case connection persist with DB and and no other user are allowed to create connection, So in
that kind of scenario we want whatever the condition comes, may be exception or not, my DB
connection should break, for that i’ll paste connection breaking code in finally() block
finalize() : finalize() method is used to code something that we want to execute just before garbage
collection is performed by JVM
====================================================================
What is Garbage Collection and how to call it explicitly?
When an object is no longer referred to by any variable, java/JVM automatically reclaims memory used
by that object. This is known as garbage collection.
System. gc() method may be used to call it explicitly.
===================================================================
What is method overloading and method overriding?
Method overloading:
When more than 1 method in a class having the same method name but different signature
(different signature means different number and type of arguments) is said to be method overloading.
Method overriding :
When patent and child class have same name and same signature(signature means number and type of
arguments) method, this is called method overriding.
(Method overriding exist in case of inheritance only, when we are making parentchild relationship
between classes)
==========================================================
Inheritance
With the help of inheritance we can transfer the properties of a class to another class
Class which is inherited is called Parent Class or Super Class
Class which inherit other class is called Child Class or Sub Class
After inheritance, child class objects can access parent class method and variables
extends is the keyword which is used to inherit class
implements is the keyword which is used to inherit interface
Type of Inheritance
Single/Simple Multilevel Multiple Inheritance Hybrid Inheritance
Inheritance Inheritance
A A A B A
B C
B
C
B
C
D
================================================================
Difference Between String and StringBuffer
Strings can be handled by 3 classes in Java: String, StringBuffer, StringBuilder
String Class is immutable, object cannot be modified, all changes that we are doing(uppercase,
lowercase, substring etc) on string object, is creating a new string object and old data still persist and
become garbage(lot of garbage is generating, impact on performance)
StringBuffer is mutable, stringbuffer object can be modified
Difference between StringBuffer and StringBuilder
StringBuffer and StringBuilder are same in working, both are mutable, objects can be modified
Difference between StringBuffer and StringBuilder is that StringBuilder is not thread safe(not
synchronized) and fast
StringBuffer is thread safe(synchronized) but slow
===============================================================
Constructors
Constructor can be defined as a group of code(same like method) which is used for initialization
: initialization means anything that we want to perform at start
Points to remember:
Constructor name is always same as class name
Constructor does not return any value so we don’t write void while creating constructor
Constructors are automatically called when object is created we need not to call them explicitly as we
do in methods( as constructor is automatically called when object is created so all code pasted inside
constructed is executed before we use class object to call any other methods, that’s why it is called
initialization)
We can have more than 1 constructor in a class with different signature, this is called constructor
overloading
====================================================================
Static keyword in Java
Static keyword in Java can be used with variable, method and nested class(class inside another class)
static variable: static variable holds single memory location independent to number of objects we have
created, all objects will access data from same memory location
static methods: static methods are like static variable holds single memory location for all objects
STATIC methods/variable can be called by the class name itself without creating object of the
class
Static nested class object created without creating object of its outer class
* methods, variables which are not static is called instance variable/methods
static member instance member
Static Methods Can use Cannot use
Instance Method Can use Can use
===============================================================
Super keyword in Java
Super keyword in java is used to call parent class override method or parent class constructor
Super Class Method Calling
1> In case child class override, parent class method. When we create child class object and call
override method it will access child class method but if we want to access parent class method, we are
going to use super keyword
Super Class Constructor Calling
2> super keyword is used to call parent class constructor as well
=============================================================
This keyword in java
This keyword can be used with variable, with the help of this keyword we can set which one is
class variable and which one is local variable(this.variable name shows , this is a class variable)
This keyword can also be used to call current class overloaded constructor.
This keyword can be used to call current class methods
=============================================================
Collection in Java OR List in Java OR Set in Java or MAP
in Java
Collection is a interface, which gives architecture to hold multiple data by same name
Collection interface is implemented by
SET : is used to hold multiple data(same or different data type)
Cannot hold duplicate values
Can have maximum 1 null value
List : is used to hold multiple data(same or different data type)
Can hold duplicate values
Values can be accessed by index same like Array
Map: Map holds data in the form of key value pair
==================================================================
Difference between Array and ArrayList
Array ArrayList
Use to hold multiple data of single/different
Use to hold multiple data of single data type data type
Need to define size of array Need not to define size, size automatically
increase /decrease as we add/remove element
Can access data through index Can access data through Iterator
=====================================================================
Difference between Abstract and Concrete methods
Abstract Method Concrete Method
Methods which are just declared not defined Methods which are declared and defined as
or method without body well or method with body
Difference between Abstract and Concrete Class
Abstract Class Concrete Class
Class with abstract & concrete methods. Class with concrete methods only
Having atleast 1 abstract method
Cannot create object of abstract class Can create object of concrete class
Difference between Abstract Class and Interface
Abstract Class Interface
Class with Abstract + Concrete methods Interface can have only abstract methods
Can have constants as well as variable Variable declared are by default final(means
constants)
We can inherit only 1 class We can inherit multiple interface(by this way
java is supporting multiple inheritance)
Use extends keyword for inheritance Use implements keyword for inheritance
===================================================================
Exception handling in Java
For that we can use 2 ways
1 throws keyword : we can place throws keyword in front of our method definition and we can
mention classes of all exception which we want to handle or we can mention parent class Exception
2 Try –catch finally block
Code that can throw exception, need to be placed in try block
After getting exception, whenever the action we want to perform will be placed in catch block
If we want to perform some task at the end does not matter exception came or not we will paste that
code in finally block
We can use try block then catch block
try block then finally block(we can skip catch block)
try block then catch block then finally block
==================================================================
Difference Between Throw and Throws in Exception
handling
1> Throws is used in Method Signature while Throw is used in Java Code(send the instance of
Exception class)
2> Throws, we can mention multiple type of exception, separated by comma while in throw we can
set only 1 exception class instance
3>Throws keyword just throws exception need not to handle it, throw pass exception instance to
caller program
===================================================================
JAVA OOPS Concepts
Polymorphism : means same name multiple use.
Overloading and Overriding concept in java implements polymorphism
2 type polymorphism: Compile Time and RunTime
Overriding is a runtime polymorphism as 2 methods are having same name and signature so at the run
time it is decided which method to call
Overloading is a compile time polymorphism, as while compiling JVM knows which method is to call by
number and type of arguments
Inheritance
With the help of inheritance, we can transfer the properties of a class to child class
Data Encapsulation
Wrapping up of data and function in a single unit is called encapsulation.
In java class implements the concept of encapsulation
Data Abstraction
Make class/ methods abstract
Abstract class and Interface implements the concept of Data Abstraction
=====================================================================
How to Iterate on HashMap
Answer: Iterating to List and Set is very simple but in case of HashMap we have to use an extra method
"keySet" which pass hashmap object to Iterator
package mypack;
import java.util.HashMap;
import java.util.Iterator;
import org.junit.Test;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class MyClas {
public static void main(String aa[])
{
HashMap hp = new HashMap();
hp.put("K1", "Val1");
hp.put("K2", "Val2");
Iterator itr= hp.keySet().iterator();
while(itr.hasNext())
{
String k = itr.next().toString();
System.out.println(hp.get(k));
}
}
}
=================================================================
Generic Method/ Classes in Java
In java Generic can be used with Methods and Classes
Generic means we create generic method which serves our multiple purpose like we
can use same method for multiple task like, sorting an integer array, String array
etc
Generic Methods : We can create generic methods which can be called by diffent type of
arguments, methods will server request on the behalf of type of argument coming with rerquest