Java Interview Questions-2
Java Interview Questions
1. How finally used under Exception Handling?
The finally keyword is used to create a block of code that followsa try block. A finally block of code always executes, whether or not an exception has occurred.
2. What things should be kept in mind while creating your own exceptions in Java?
While creating your own exception −
All exceptions must be a child of Throwable.
If you want to write a checked exception that is automatically enforced by the Handle or
Declare Rule, you need to extend the Exception class.
You want to write a runtime exception, you need to extend the RuntimeException class.
3. Define Inheritance?
It is the process where one object acquires the properties of another. With the use of inheritance the information is made manageable in a hierarchical order.
4. When super keyword is used?
If the method overrides one of its superclass’s methods, overridden method can be invoked through the use of the keyword super. It can be also used to refer to a hidden field.
5. What is Polymorphism?
Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.
6. What is Abstraction?
It refers to the ability to make a class abstract in OOP. It helps to reduce the complexity and also improves the maintainability of the system.
7. What is Abstract class?
These classes cannot be instantiated and are either partially implemented or not at all implemented. This class contains one or more abstract methods which are simply method declarations without a body.
8. When Abstract methods are used?
If you want a class to contain a particular method but you want the actual implementation of that method to be determined by child classes, you can declare the method in the parent class as
9. What is Encapsulation?
It is the technique of making the fields in a class private and providing access to the fields via public methods. If a field is declared private, it cannot be accessed by anyone outside the class,
thereby hiding the fields within the class. Therefore encapsulation is also referred to as data hiding.
10. What is the primary benefit of Encapsulation?
The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code. With this Encapsulation gives maintainability, flexibility and
extensibility to our code.
11. What is an Interface?
An interface is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.
12. Define Packages in Java?
A Package can be defined as a grouping of related typesclasses, interfaces, enumerationsandannotations providing access protection and name space management.
13. Why Packages are used?
Packages are used in Java in-order to prevent naming conflicts, to control access, to make searching/locating and usage of classes, interfaces, enumerations and annotations, etc., easier.
14. What do you mean by Multithreaded program?
A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is called a thread, and each thread defines a separate path of execution.
15. What are the two ways in which Thread can be created?
Thread can be created by: implementing Runnable interface, extending the Thread class.
16. What is an applet?
An applet is a Java program that runs in a Web browser. An applet can be a fully functional Java application because it has the entire Java API at its disposal.
17. Explain garbage collection in Java?
It uses garbage collection to free the memory. By cleaning those objects that is no longer
reference by any of the program.
18. Define immutable object?
An immutable object can’t be changed once it is created.
19. Explain the usage of this with constructors?
It is used with variables or methods and used to call constructer of same class.
20. Explain Set Interface?
It is a collection of element which cannot contain duplicate elements. The Set interface contains
only methods inherited from Collection and adds the restriction that duplicate elements are
21. Explain TreeSet?
It is a Set implemented when we want elements in a sorted order.
22. What is Comparable Interface?
It is used to sort collections and arrays of objects using the collections.sort and java.utils. The
objects of the class implementing the Comparable interface can be ordered.
23. Difference between throw and throws?
It includes:Throw is used to trigger an exception where as throws is used in declaration of exception.Without throws, Checked exception cannot be handled where as checked exception can be
propagated with throws.
24. Explain the following line used under Java Program public
public − it is the access specifier.
static − it allows main to be called without instantiating a particular instance of a class.
void − it affirns the compiler that no value is returned by main.
main − this method is called at the beginning of a Java program.
String args[ ] − args parameter is an instance array of class String
25. Define JRE i.e. Java Runtime Environment?
Java Runtime Environment is an implementation of the Java Virtual Machine which executes Java
programs. It provides the minimum requirements for executing a Java application