30+ Core Java Interview Questions For Freshers

Basics

Question 1: What is Java and Its Benefits

Java is one of the most popular and versatile programming language in the industry. Due to its nature of robustness, performance and platform-independent feature. Java can be used for the development of mobile applications, system development and many other platforms.

Question 2: What is the difference between JVM, JRE, JIT and JDK?

JDK: (Java Development Kit) provides all the tools used to executw or debug any Java application. It includes JRE and other development tools like javac, java, etc

JRE: (Java Runtime Environment) provides runtime environemt to Java application and it contains JVM + supporting library classes within it. To run any program we must have JRE installed on our machine.

JVM: (Java Virtual Machine) aka heart of any Java application, it is a virtual computing machine which is responsible for converting Byte Code to machine specific code. JVM makes Java platform independent because code written once can be executed anywhere with the help of JVM.

JIT: (Just in Time Compiler) is a part of JVM which improves the performance of Java application. JIT compiles similar part of Java Byte Code to save time. Therefore, when method with similar functionality has been called, JVM refers to pre-compiled code instead of interpreting again, resulting boost in performance.

In short, remember below formula:

JDK = JRE + Development Tools

JRE = JVM + Library Classes

Question 3: How Java is Platform Independent

Java follows the principle of WORA i.e Write Once Run Anywhere because code written on any operating system can be executed on any other operating system with the help of JVM i.e Java Virtual Machine which converts Java ByteCode to Machine level code. Once you have the ByteCode(.class) you can run this file on any other operating system by installing JVM on another OS.

Question 4: Why Java is not 100% Object Oriented

Java is not fully object oriented because it uses eight primitive data types which are not objects i.e boolean, byte, char, int, float, double, long, short

Question 5: Why Pointers are not used in Java?

The reason why Java doesn’t use pointers because pointers may lead to memory leakage and they are unsafe. To avoid direct access to memory pointers are not used in Java.

OOPs Concept

Question 6: What are the principle concepts of OOPS?

Object Oriented Programming is a programming style which follows below concepts:

Abstraction: It means “Hiding the Complexity and Showing the Functionality“. In Java, there are many ways to achieve abstraction. It could be though abstract class, inheritance or declaring Interface

Polymorphism: Polymorphism is a concept where an object behaves differently in a different situation. For example, a single object can have multiple behaviors. It can be achived in two ways i.e Method Overloading and Method Overriding.

Inheritance: It is a process where one class inherits property of another class.

Encapsulation: As the word defines, Encapsulation means encapsulating data with access modifiers. In other words, Encapsulation is a technique that binds data and code together.

You can easily remember these concept by A-PIE.

Question 7: What is Method Overloading and Method Overriding?

Method Overriding: Always remember, method overriding in Java occurs in case of Inheritance. which is also known as Run Time Polymorphism. In method overriding, child class has the same method as superclass with the same method name and same no. of parameters.

Method Overloading: In Java, for method overloading, it may or may not need Inheritance. It is also known as Compile Time Polymorphism. Most of the cases, it occurs in the same class where we repeat the same method in the same class but with different parameters.

Question 8: Is it possible to override the main method?

No, because static method can’t be overridden in Java.

Question 9: What is the difference between this() and super() in Java?

this()super()
1. this() represents the current instance of a class1. super() represents the instance of a parent class
2. It is used to call the default constructor of the same class2. It is used to call the default constructor of the parent class
3. It is used to access methods of the current class3. It is used to access methods of the parent class

Question 10: Can we create an object for an Interface?

No, you can’t instantiated Interface in their own, so you must write a class that implements the interface. After doing so, you can create the object of the class.

Question 11: What do you mean by Interface in Java?

In general, Interfaces are used to hide the complexity of the application and they are the blueprint of a class. Also, interfaces may only contain abstract methods with no definition. The interface is another way to achieve Abstraction and it specifies the behavior of a class.

public interface Car{
  public void drive();
  public void break();
}

Question 12: Why Multiple Inheritance is not supported in Java?

Basically, if child class inherits property from multiple classes then it is know as Multiple Inheritance. The reason why Java doesn’t support multiple inheritance is because if multiple parent classes have same method name, at this point it is pretty hard for the compiler to decide which method to call at runtime. To avoid runtime conflicts Java doesn’t support mulitple Inheritance.

Question 13: Can we Inherit Constructor in Java?

No, Constructor can not be inherited. Though, child class can call parent class constructor. Also there is no need to wrie final before constructor.

Question 14: What is a marker Interface?

Marker Interface are empty Interfaces with no field or methods. They are used while serializing and de-serializing in Java.

Question 15: What is an Abstract Class?

A class which is declared using abstract keyword know as Abstract Class. It can not be instantiated and it may have an abstract and non-abstract method. Abstract Class is another way to achieve Abstraction in Java

Java String

Question 16: Difference between String, StringBuffer, and StringBuilder.

FactorStringStringBufferStringBuilder
Storage AreaConstant String PoolHeap AreaHeap Area
MutabilityImmutableMutableMutable
Thread SafetyYesYesNo
PerformanceFastSlow because of Thread safetyFaster then StringBuffer

Question 17: What is String Constant Pool?

As the name suggests, it is the pool of strings stored in memory i.e Java Heap Memory. Whenever you create a new string object in Java, it is stored in String Pool. Later on, if you create a string object with the same value then it is checked in Java String Pool if a value exists then the same object reference is returned otherwise new object is created. It increases the runtime performance of the application.

Question 18: Where String Pool is Stored in Memory.

Heap Space. 

Java Exception Handling

Question 19: Difference beween Error and Exception?

ErrorException
1. An error can’t be predicted.1. An exception can be predicted.
2. You can’t recover from an error.2. You can handle exception during code.
3. All errors are unchecked type and thrown by JVM3. Exceptions are known to the compiler.
4. Example: StackOverFlowError, OutOfMemoryError4. Example: NullPointerException, ArrayIndexOutOfBoundsException

Question 20: How to handle Exception in Java?

To handle exception in Java, we use TRY-CATCH block. We place our code inside TRY block where an exception can occur, followed by a CATCH block.

public class Demo {
    public static void main(String args[]) {
        try {
            //code that may raise exception        
            int data = 100 / 0;
        } catch (ArithmeticException e) {
            //handling exception and printing it   
            System.out.println(e);
        }
        //rest code of the program     
    }
}

Question 21: Throw vs Throws Keyword

ThrowThrows
1. Used inside the method body1. Used in the method signature
2. Can’t throw multiple exceptions2. Can throw multiple exceptions from a method.

Additional

Question 1: How many types of memory areas are allocated by JVM?

  1. Classloader: It is a subsystem of JVM that is used to load class files.
  2. Class(Method) Area: It stores per-class structures such as field and method data.
  3. Heap: It is the runtime data area in which memory is allocated to the objects.
  4. Stack: Java Stack stores local variables and methods in the form of Frames. It plays a major role in the method invocation.
  5. Program Counter Register:  It contains the address of the Java virtual machine instruction currently being executed.
  6. Native Method Stack: It contains all the native methods used in the application.

Question 2: What is a ClassLoader?

The class loader is responsible for loading class files dynamically at runtime in the JVM. Also, the class loader is smart enough that it loads the Java classes only when they are needed by the program.

There are mainly 3 types of ClassLoader:

1. Bootstrap ClassLoader

This classloader serves as a parent classloader for all other classloaders and classes are loaded from rt.jar.

2. Extension ClassLoader

This is just a child of the bootstrap class loader and it takes care of loading Java classes found in the $JAVA_HOME/lib/ext directory.

3. System ClassLoader

The system class loader or application class loader takes care of loading all the Java class in the JVM and it loads all the class from the classpath. It is a child of Extension class loader.

classloader

Question 3: What if I write static public void instead of public static void?

The program will compile and runs correctly because the order of specifiers doesn’t matter in Java.

Question 4: What is the default value of the local variables?

There is no default value of any local variable in Java.

Question 5: Does constructor return any value?

Yes, but implicitly.

Constructor returns the current instance of the class.

Question 6: Can we execute a program without main() method?

Yes, it can be done through static block.

Question 7: Which class is the superclass for all the classes?

The object class is the superclass of all other classes in Java.

Question 8: Can you use this() and super() both in a constructor?

No, because this() and super() must be the first statement in the class constructor.

Question 9: What is Garbage Collection?

It is the process of removing unused objects from the memory. Garbage collection is done in heap memory for memory management in Java.

Question 10: Explain Thread Lifecycle?

Below is the lifecycle of a Thread.

  • NEW – A thread that has not yet started.
  • RUNNABLE – A thread executing in the Java virtual machine.
  • BLOCKED – If a thread is blocked because of any other thread.
  • WAITING – A thread that is waiting indefinitely for another thread.
  • TIMED_WAITING – A thread that is waiting for another thread up to a specified waiting time.
  • TERMINATED – A thread that has exited.

Remember: A thread can be only in one state at a time.

More Details.