Here are some Java interview related questions and answers which will help freshers and developers to crack interviews.
1. What is the difference between JDK and JRE?
- JDK stands for Java Development Kit. It contains the tools and libraries for development of Java programs. It also contains compilers and debuggers needed to compile Java program
- JRE stands for Java Runtime Environment. This is included in JDK. JRE provides libraries and JVM that is required to run a Java program.
2. What is Java Virtual Machine (JVM)?
Java Virtual Machine (JVM) is an abstract machine that executes Java Bytecode. There are different JVM for different hardware and software platforms. So JVM is platform dependent. JVM is responsible for loading, verifying and executing the Bytecode on a platform.
3. What are the different types of memory areas allocated by JVM?
In java, JVM allocates memory to different processes, methods and objects. Some of the memory areas allocated by JVM are:
- ClassLoader: It is a component of JVM used to load class files.
- Class (Method) Area: It stores per-class structures such as the runtime constant pool, field and method data, and the code for methods.
- Heap: Heap is created a runtime and it contains the runtime data area in which objects are allocated.
- Stack: Stack stores local variables and partial results at runtime. It also helps in method invocation and return value. Each thread creates a private JVM stack at the time of thread creation.
- Program Counter Register: This memory area contains the address of the Java virtual machine instruction that is currently being executed.
- Native Method Stack: This area is reserved for all the native methods used in the application.
4. What is JIT compiler?
Just In Time compiler also known as JIT compiler is used for performance improvement in Java. It is enabled by default. It is compilation done at execution time rather earlier. Java has popularized the use of JIT compiler by including it in JVM.
5. How Java platform is different from other platforms?
Java is a platform independent language. Java compiler converts Java code in to byte code that can be interpreted by JVM. There are JVM written for almost all the popular platforms in the world.
Java byte code can run on any supported platform in same way. Where as other languages require libraries compiled for a specific platform to run
6. Why people say that Java is ‘write once and run anywhere’ language?
- You can write Java code on Windows and compile it in Windows platform. The class and jar files that you get from Windows platform can run as it is on Unix environment. So it is a truly
platform independent language. - Behind all this portability is Java byte code. Byte code generated by Java compiler can be interpreted by any JVM. So it becomes much easier to write programs in Java and expect those to run on any platform.
- Java compiler javac compiles java code and JVM java runs that code.
7. How does ClassLoader work in Java?
In Java, ClassLoader is a class that is used to load files in JVM. ClassLoader loads files from their physical file locations e.g. Filesystem, Network location etc.
There are three main types of ClassLoaders in Java.
- Bootstrap ClassLoader: This is the first ClassLoader. It loads classes from rt.jar file.
- Extension ClassLoader: It loads class files from jre/lib/ext location.
- Application ClassLoader: This ClassLoader depends on CLASSPATH to find the location of class files. If you specify your jars in CLASSPATH, then this ClassLoader will load them.
9. What is the difference between byte and char data types in Java?
- Both byte and char are numeric data types in Java. They are used to represent numbers in a specific range.
- Major difference between them is that a byte can store raw binary data where as a char stores characters or text data.
Usage of char is E.g.char ch = ‘x’;
- Byte values range from
-128
to127
. - A byte is made of 8 bits. But a char is made of 16 bits. So it is equivalent to 2 bytes.
10. Why constructors cannot be final, static, or abstract in Java?
- If we set a method as final it means we do not want any class to override it. But the constructor (as per Java Language Specification) cannot be overridden. So there is no use of marking it final.
- If we set a method as abstract it means that it has no body and it should be implemented in a child class. But the constructor is called implicitly when the new keyword is used. Therefore it needs a body.
- If we set a method as static it means that it belongs to the class, but not a particular object. The constructor is always called to initialize an object. Therefore, there is no use of marking constructor static.
11. What is the purpose Class.forName method?
This method is used to method is used to load the driver that will establish a connection to the database.
13. What is the purpose of garbage collection in Java, and when is it used?
The purpose of garbage collection is to identify and discard those objects that are no longer needed by the application, in order for the resources to be reclaimed and reused.
14. Explain the concept of Inheritance?
Inheritance is an important concept in Object Oriented Programming. Some objects share certain characteristics and behavior. By using Inheritance, we can put the common behavior and characteristics in a base class which also known as super class. And then all the objects with common behavior inherit from this base class. It is also represented by IS-A relationship.
Inheritance promotes, code reuse, method overriding and polymorphism
15. Why Java does not support multiple inheritance?
Multiple Inheritance means that a class can inherit behavior from two or more parent classes.
The issue with Multiple Inheritance is that both the parent classes may have different implementation for the same method. So they have different ways of doing the same thing. Now which implementation should the child class choose?
This leads to ambiguity in Multiple Inheritance. This is the main reason for Java not supporting Multiple Inheritance in implementation.