15 Most Frequently Asked Core Java Interview Questions
1.Differentiate between JDK, JRE and JVM
JVM is an acronym for Java Virtual Machine. An abstract machine, JVM provides runtime environment in which java bytecode can be implemented. Bytecode is the machine language for the Java Virtual Machine. Tasks like Loads code, Verifies code, Executes code and providing runtime environment are performed by the Java Virtual Machine.
JRE is an acronym of Java Runtime Environment which is the execution of JVM. It physically exists and is used to provide runtime environment. It comprises of set of libraries + other files that JVM uses at runtime.
JDK is an acronym of Java Development Kit. Comprising of JRE + Development tools, it physically exists.
2.What is garbage collection in Java?
The process of removing the objects which are no longer required by the Java application and freeing the memory is known as garbage collection. Java Garbage Collector comes in handy whilst garbage collection.
3.Is it possible to override private or static method in Java?
No, overriding of private or static method in Java is not possible. If you create similar method with same return type and same method arguments, then it’s called method hiding.
4.Differentiate between StringBuffer and StringBuilder in Java?
The only difference between StringBuffer and StringBuilder is that of synchronization. StringBuffer is synchronized whereas StringBuilder is not. Also, being synchronized makes StringBuffer thread safe whereas StringBuilder is not thread safe.
5.State the difference between object oriented programming language and object based programming language?
Object oriented language support all features of OOPs whereas Object based programming language does not support all the features of OOPs like Inheritance and Polymorphism. Java script is an example of Object based programming language and Java is an example of Object oriented programming language.
6.Define multithreaded program?
The process of implementing multiple threads simultaneously is known as multithreaded program and each thread defines a separate path of implementation.
7.Name the two ways in which thread can be created
Thread can be created either by implementing Runnable interface or extending the thread class.
8.What is the difference between factory and abstract factory pattern?
With Abstract Factory pattern, the responsibility of object instantiation to another object is delegated by a class via composition whereas inheritance is used by the Factory method pattern and the handling of the desired object relies on a subclass.
9.Define immutable object. Can you write immutable object?
An object which cannot be modified once created is known as an immutable object. For example, String class is an immutable class. Since, we cannot modify immutable objects so they are thread safe in concurrent execution.
10.Differentiate between CyclicBarrier and CountDownLatch in Java.
Both CyclicBarrier and CountDownLatch are used to implement a scenario where one Thread waits for one or more Thread to complete their job before the processing begins but there is one difference between the two; the same CountDownLatch cannot be reused once the count reaches zero and the latch is open. On the contrary, by resetting the barrier, we can reuse the CyclicBarrier, once the barrier is broken.
11.When do you override hashcode and equals()?
We override hashcode and equals when we want to do equality check or want to use our object as key in HashMap.
12.How many bits are used to represent Unicode, ASCII, UTF-8 and UTF-16 characters?
Unicode requires 16 bits and ASCII requires 7 bits. ASCII character is usually represented as 8 bits though. UTF-8 exhibits characters using 8, 16 and 18 bit patterns. UTF-16 uses 16-bit and larger bit patterns.
13.What things should be considered whilst creating your own exceptions in Java?
- All exceptions must be a child of Throwable
- Extend the exception class to write a checked exception which is by default enforced by the Handle or Declare Rule.
- Extend the RuntimeException class to write a runtime exception.
14.What do you understand by the following line under Java Program?
public static void main (String args)
- public – the access specifier
- static – it allows main() to be called without instantiating a particular instance of a class
- void – it confirms 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
15.What is JAR file?
JAR file aggregates many files in a single place. It holds Java classes in a library. JAR files are built on ZIP file format and have .jar file as an extension.