Hello Friends,
Below I have listed frequently asked core java interview questions. Hope that it will help you to gain knowledge in Java and crack your next interview in Java.
1) What is JVM ?
Ans - The Java Virtual machine (JVM) is the virtual machine that run the Java bytecodes. The JVM doesn't understand Java source code, that's why you compile your *.java files to obtain *.class files that contain the bytecodes understandable by the JVM.
JVM is responsible for converting byte code into machine readable code.
2) What is JRE ?
Ans - The Java Runtime Environment (JRE) provides the libraries, the Java Virtual Machine, and other components to run applets and applications written in the Java programming language.
3) What is JDK ?
Ans - The JDK is a superset of the JRE, and contains everything that is in the JRE, plus tools such as the compilers and debuggers necessary for developing applets and applications.
4) What is a pointer and does Java support pointers?
Ans - Pointer is a reference handle to a memory location. Improper handling of pointers leads to memory leaks and reliability issues hence Java doesn't support the usage of pointers.
5) Can a main() method be declared final?
Ans - Yes. Any inheriting class will not be able to have it's own default main() method.
6) What is Externalizable?
Ans - Externalizable is an Interface that extends Serializable Interface. And sends data into Streams in Compressed Format.
7) Difference between StringBuffer and StringBuilder
Ans - StringBuffer is synchronized, StringBuilder is not. Hence it is faster than StringBuffer.
StringBuilder is faster than StringBuffer because it's not synchronized.
8) Difference between Comparable Vs Comparator
Comparable
A comparable object is capable of comparing itself with another object. The class itself must implements the java.lang.Comparable interface in order to be able to compare its instances.
Comparator
A comparator object is capable of comparing two different objects. The class is not comparing its instances, but some other class’s instances. This comparator class must implement the java.util.Comparator interface.
9) Can we run abstract class which has main method ?
Ans - Yes, definitely, because main() is a concrete method and abstract class allows concrete methods.
10) Can we override a static method ?
Ans - No, we cannot override a static method in Java.
11) Can we execute a program without main() method ?
Ans - Yes. Using static block we can do this.
12) What is aggregation ?
Ans - Aggregation can be defined as the relationship between two classes where the aggregate class contains a reference to the class it owns. Aggregation is best described as a has-a relationship. For example, The aggregate class Employee having various fields such as age, name, and salary also contains an object of Address class having various fields such as Address-Line 1, City, State, and pin-code. In other words, we can say that Employee (class) has an object of Address class.
13) What is the difference between aggregation and composition ?
Ans - Aggregation represents the weak relationship whereas composition represents the strong relationship. For example, the bike has an indicator (aggregation), but the bike has an engine (composition).
14) Can we override the private methods ?
Ans - No, we cannot override the private methods because the scope of private methods is limited to the class and we cannot access them outside of the class.
15) What is Producer-Consumer problem ?
Ans -
Problem Statement -
There are two processes, a producer and a consumer, that share a common buffer with a limited size. The producer “produces” data and stores it in the buffer, and the consumer “consumes” the data, removing it from the buffer.
Having two processes that run in parallel, we need to make sure that the producer will not put new data in the buffer when the buffer is full and the consumer won’t try to remove data from the buffer if the buffer is empty.
Solution -
For solving this concurrency problem, the producer and the consumer will have to communicate with each other. If the buffer is full, the producer will go to sleep and will wait to be notified. After the consumer will remove some data from the buffer, it will notify the producer, and then, the producer will start refilling the buffer again.
The same process will happen if the buffer is empty, but in this case, the consumer will wait to be notified by the producer.
If this communication is not done properly, it can lead to a deadlock where both processes will wait for each other.
If you want to go through this kind of 100+ quality questions then checkout below course -
I am offering @ 50% Discount.