Top 20 Java Interview Questions and Answers
Top 20 Java Interview Questions and Answers
1. What is the difference between JDK, JRE and JVM?
JVM: JVM is an acronym for Java Virtual Machine, it is an abstract machine which provides the runtime environment in which java bytecode can be executed. It is a specification. JVMs are available for many hardware and software platforms (so JVM is platform dependent).
JRE: JRE stands for Java Runtime Environment. It is the implementation of JVM.
JDK: JDK is an acronym for Java Development Kit. It physically exists. It contains JRE + development tools.
2. How many memory areas are allocated by JVM?
Memory areas are:
- Class(Method) Area
- Heap
- Stack
- Program Counter Register
- Native Method Stack
3. What is JIT compiler?
Just-In-Time (JIT) compiler: It is used to improve the performance. JIT compiles parts of the byte code that have similar functionality at the same time, and hence reduces the amount of time needed for compilation. Here the term “compiler” refers to a translator from the instruction set of a Java virtual machine (JVM) to the instruction set of a specific CPU.
4. What is the difference between object-oriented programming or object-based programming?
Object-based programming languages follow all the features of OOPs except Inheritance. Examples of object-based programming languages are JavaScript, VBScript etc.
5. 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.
6. What is overloading and overriding in java?
When we have more than one method with the same name in a single class but the arguments are different, then it is called as method overloading.
The overriding concept comes in picture with inheritance when we have two methods with the same signature, one in parent class and another in child class. We can use @Override annotation in the child class overridden method to make sure if parent class method is changed, so as child class.
7. Can we overload main method?
Yes, we can have multiple methods with name “main” in a single class. However, if we run the class, java runtime environment will look for main method with syntax as public static void main (String args[]).
8. What is java package and which package is imported by default?
Java package is the mechanism to organize the java classes by grouping them. The grouping logic can be based on functionality or modules based. A java class fully classified name contains package and class name.
For example, java.Lang.Object is the fully classified name of the Object class that is part of java. Lang package.
Java. Lang package is imported by default and we don’t need to import any class from this package explicitly.
9. What is Inheritance? And its types?
Inheritance is a mechanism in which one object acquires all the properties and behavior of another object of another class. It represents IS-A relationship. It is used for Code Reusability and Method Overriding.
There are four types of inheritance
- Single inheritance
- Multiple inheritance
- Multilevel inheritance
- Hierarchical inheritance
10. What is difference between inner class and a subclass?
An Inner class is a class which is nested within another class. An Inner class has access rights for the class which is nesting it and it can access all variables and methods defined in the outer class.
A sub-class is a class which inherits from another class called super class. Sub-class can access all public and protected methods and fields of its super class.
Are You Looking for Java Training in Bangalore?
11. What is the purpose of static methods and static variables?
When there is a requirement to share a method or a variable between multiple objects of a class instead of creating separate copies for each object, we use static keyword to make a method or variable shared for all objects.
12. What is data encapsulation? What its significance?
Encapsulation is a concept in Object Oriented Programming for combining properties and methods in a single unit.
Encapsulation helps programmers to follow a modular approach for software development as each object has its own set of methods and variables and serves its functions independent of other objects. Encapsulation also serves data hiding purpose.
13. What are Loops in java? What are three types of Loop?
Looping is used in programming to execute a statement or a block of statement repeatedly. There are three types of loops in Java:
1) For Loops
For loops are used in java to execute statements repeatedly for a given number of times. For loops are used when number of times to execute the statements is known to a programmer.
2) While Loops
While loop is used when certain statements need to be executed repeatedly until a condition is fulfilled. In while loops, condition is checked first before execution of statements.
3) Do While Loops
Do While Loop is same as while loop with only difference that condition is checked after execution of block of statements. Hence in case of do while loop, statement are executed at least once.
14. What’s the difference between an Abstract class and Interface in java?
The primary difference between an abstract class and interface is that an interface can only possess declaration of public static methods with no concrete implementation while an abstract class can have members with any access specifies (public, private etc.) with or without concrete implementation.
Another key difference in the use of abstract classes and interfaces is that a class which implements an interface must implement all the methods of the interface while a class which inherits from an abstract class doesn’t require implementation of all the methods of its superclass.
A class can implement multiple interfaces but it can extend only one abstract class.
15. What are the performance implications of interfaces over abstract classes?
Interfaces are slower in performance as compared to abstract classes as extra indirections are required for interfaces. Another key factor for developers to take into consideration is that any class can extend only one abstract class while a class can implement many interfaces.
Use of interfaces also puts an extra burden on the developers at any time an interface is implemented in a class; the developer is forced to implement each and every method of interface.
16. Does importing a package imports its sub-packages as well in java?
In java, when a package is imported, its sub-packages aren’t imported and developer needs to import them separately if required.
For example, if a developer imports a package university.*, all classes in the package named university are loaded but no classes from the sub-package are loaded. To load the classes from its sub-package (say department), developer has to import it explicitly as follows:
Import university.department.*
17. What is Multithreading?
Multithreading is a programming concept to run multiple tasks in a concurrent manner within a single program. Threads share same process stack and running in parallel. It helps in performance improvement of any program.
18. String and String Buffer both represent String object. Can we compare String and String Buffer in java?
Although String and String Buffer both represent String objects, we can’t compare them with each other and if we try to compare them, we get an error.
19. What is the base class of all exception classes?
In Java, Java. Lang. Throwable is the superclass of all exception classes and all exception classes are derived from this base class.
20. Which API is provided by java for operation on the set of objects?
Java provides a Collection API which provides many useful methods which can be applied on a set of objects. Some of the important classes provided by Collection API include Array List, Hash Map, Tree Set and Tree Map.