Java frequently asked Interview questions for Selenium

1.       What is the default value of the local variables?
A. The local variables are not initialized to any default value, neither primitives nor object references.

2.       What is constructor?
A. Constructor is just like a method that is used to initialize the state of an object. It is invoked at the time of object creation.

3.       What is the purpose of default constructor?
·         Default constructor provides the default values to the objects. The java compiler creates a default constructor only if there is no constructor in the class

4.       Does constructor return any value?
·         Yes, that is current instance (You cannot use return type yet it returns a value).

5.       What is static variable?
·         Static variable is used to refer the common property of all objects (that is not unique for each object) e.g. company name of employees, college name of students etc.
·         Static variable gets memory only once in class area at the time of class loading.

6.       What is static method?
·         A static method belongs to the class rather than object of a class.
·         A static method can be invoked without the need for creating an instance of a class.
·         Static method can access static data member and can change the value of it.

7.       What is this in java?
·         It is a keyword that that refers to the current object

8.       Which class is the superclass for every class?
·         Object class

9.       What is super in java?
·         It is a keyword that refers to the immediate parent class object

10.   What is method overloading?
·         If a class has multiple methods by same name but different parameters, it is known as Method Overloading. It increases the readability of the program


11.   What is method overriding:
·         If a subclass provides a specific implementation of a method that is already provided by its parent class, it is known as Method Overriding. It is used for runtime polymorphism and to provide the specific implementation of the method

12.   What is final variable?
·         If you make any variable as final, you cannot change the value of final variable(It will be constant).

13.   What is final method?
·         Final methods can't be overridden.

14.   What is final class?
·         Final class can't be inherited

15.   What is abstraction?
·         Abstraction is a process of hiding the implementation details and showing only functionality to the user

16.   What is the difference between abstraction and encapsulation?
·         Abstraction hides the implementation details whereas encapsulation wraps code and data into a single unit

17.   Is it possible to instantiate the abstract class?
·         No, abstract class can never be instantiated.

18.   What is interface?
·         Interface is a blueprint of a class that has static constants and abstract methods. It can be used to achieve fully abstraction and multiple inheritance.

19.   What is difference between abstract class and interface?
Abstract class
Interface
An abstract class can have method body (non-abstract methods).
Interface have only abstract methods.
An abstract class can have instance variables.
An interface cannot have instance variables.
An abstract class can have constructor.
Interface cannot have constructor.
An abstract class can have static methods.
Interface cannot have static methods.
You can extend one abstract class.
You can implement multiple interfaces.

20.   Do I need to import java.lang package any time? Why?
No. It is by default loaded internally by the JVM.

21.   What is Exception Handling?
·         Exception Handling is a mechanism to handle runtime errors.It is mainly used to handle checked exceptions.

22.   What is difference between Checked Exception and Unchecked Exception?
·         Checked Exception: The classes that extend Throwable class except RuntimeException and Error are known as checked exceptions e.g.IOException, SQLException etc. Checked exceptions are checked at compile-time.

·         Unchecked Exception: The classes that extend RuntimeException are known as unchecked exceptions e.g. ArithmeticException, NullPointerException etc. Unchecked exceptions are not checked at compile-time.

23.   What is the base class for Error and Exception?
·         throwable

24.   What is finally block?
·         finally block is a block that is always executed

25.   What is difference between throw and throws?
throw keyword
throws keyword
throw is used to explicitly throw an exception.
throws is used to declare an exception.
checked exceptions can not be propagated with throw only.
checked exception can be propagated with throws.
throw is followed by an instance.
throws is followed by class.
throw is used within the method.
throws is used with the method signature.
You cannot throw multiple exception
You can declare multiple exception e.g. public void method()throws IOException, SQLException.

26.   What is difference between final, finally and finalize?
·         final: final is a keyword, final can be variable, method or class.You, can't change the value of final variable, can't override final method, can't inherit final class.
·         finally: finally block is used in exception handling. finally block is always executed.
·         finalize():finalize() method is used in garbage collection.finalize() method is invoked just before the object is garbage collected.The finalize() method can be used to perform any cleanup processing.

27.   What is the purpose of the Runtime class?
·         The purpose of the Runtime class is to provide access to the Java runtime system.

28.   How will you invoke any external process in Java?
·         By Runtime.getRuntime().exec(?) method.

29.   What is the difference between the Reader/Writer class hierarchy and theInputStream/OutputStream class hierarchy?
·         The Reader/Writer class hierarchy is character-oriented, and the InputStream/OutputStream class hierarchy is byte-oriented.

30.   What is thread?
·         A thread is a lightweight subprocess.It is a separate path of execution.It is called separate path of execution because each thread runs in a separate stack frame.

31.   What is the difference between List and Set?
·         List can contain duplicate elements whereas Set contains only unique elements.

32.   What is the difference between Set and Map?
·         Set contains values only whereas Map contains key and values both.

33.   What is the difference between HashSet and HashMap?
·         HashSet contains only values whereas HashMap contains entry (key,value). HashSet can be iterated but HashMap need to convert into Set to be iterated.

34.   What is the difference between HashMap and Hashtable?
HashMap
Hashtable
HashMap is not synchronized.
Hashtable is synchronized.
HashMap can contain one null key and multiple null values.
Hashtable cannot contain any null key or null value.


35.   What is the advantage of Properties file?
·         If you change the value in properties file, you don't need to recompile the java class. So, it makes the application easy to manage.

36.   How you will define the constant in java
·         Final Keyword

37.   Main method can be override or overload?
·         Cannot override main method but it can be overloaded.

38.   From main method if we remove (String [] args) it will work or not?
Public static void main ( )
{
}
·         Yes, Overloading

39.   If I rearrange the order of main method it will work or not?
Static public Void main (String[] args)

{
}
·         Yes, this will work

40.   Why it is called main method not any other method is not main?
·         JVM looks for a method called main to start execution

41.   How to come out of the for loop?
·         break;

42.   Hierarchy of the exception handling?
                Throwable

Error                      Exception
                                IOException       RuntimeException

43.   What is the difference between String and StringBuffer?
·         String is immutable, meaning that when you perform an operation on a String you are really creating a whole new String.
·         StringBuffer is mutable, and you can append to it as well as reset its length to 0.
·         StringBuffer is faster than String when performing simple concatenations.

44.   What is default Boolean value in java?
·         False