Top 50 Core Java Interview Questions

In this article, we have covered almost 50+ important core Java interview questions for freshers and experienced candidates. In today’s IT business, Java is the most extensively use programming language. One key reason for a large number of beginners and professionals in the field of programming is the employment opportunities that come with Java expertise. 

Basics Java Interview Questions for Beginners

In this session, we will see Java Interview Questions which were ask by beginner people who have experience of 0 to 1 year in the field.

Q 01: What is the Difference Between C++ and Java

C++JAVA
It supports pointers, unions, operator overloading, and structure.It does not support pointers, unions, operator overloading, and structure.
C++ does not support garbage collection.Java supports garbage collection.
It is platform dependent.It is platform-independent.
C++ supports inheritance including multiple inheritancesJava supports inheritance except for multiple inheritances
It is compiled.It is interpreted.
C++ supports destructors.Java does not support destructor

Q 02: What is a ClassLoader?

In Java, a class loader is a component of the Java Virtual Machine that is responsible for loading class files when a program is execute; ClassLoader is the first to load the executable file.

Q 03: What are the Memory Allocations available in Java?

Java has five significant types of memory allocations.

  1. Class Memory
  2. Heap Memory
  3. Stack Memory
  4. Program Counter-Memory
  5. Native Method Stack Memory

Q 04: What are the features of JAVA?

  • OOP concepts:
    • Object-oriented
    • Inheritance
    • Encapsulation
    • Polymorphism
    • Abstraction
  • Platform independence: A single program may run on several systems without modification.
  • High Performance: JIT (Just In Time compiler) allows for great speed in Java. JIT translates bytecode to machine language, and then the JVM begins execution.
  • Multi-Threading: A Thread is a flow of execution that is multi-threading. The JVM generates a thread known as the main thread. By extending the thread class or implementing the Runnable interface, the user can create many threads.

Q 05: How does Java enable high performance?

Java uses a Just In Time compiler to provide high performance. It’s use to convert instructions into bytecodes.

Q 06: Name the Java IDEs?

Eclipse and NetBeans are the IDEs of JAVA.

Q 07: What do you mean by Constructor?

  • When a new object is create in a program a constructor gets invoke corresponding to the class.
  • The constructor is a method that has the same name as the class name.
  • If a user doesn’t create a constructor implicitly a default constructor will be create.
  • The constructor can be overloaded.
  • If the user created a constructor with a parameter then he should create another constructor explicitly without a parameter.

Q 08: What is meant by the Local variable and the Instance variable?

  • Local variables are declare in the method, as is the scope of variables that exist within the method itself.
  • Instance variables are declare both within and outside of methods, and their scope extends across the class.

Q 09: What is a Class?

A Class is where all Java code is define. It includes variables and methods.

  • Variables are characteristics that specify a class’s state.
  • Methods are where the specific business logic must be perform. It consists of a series of statements (or) instructions designed to meet the specific requirement.
public class Multiplication{ //Class name declaration
int x = 5; //Variable declaration
int y= 15;
public void multi(){ //Method declaration
int z = x*y;
}
}

Q 10: What is an Object?

An object is an instance of a class. The object has a state as well as behavior. When the JVM encounters the “new()” keyword, it will create an instance of that class.

public class Multiplication{
public static void main(String[] args){
Multiplication multi = new Multiplication();//Object creation
}
}

Q 11: What are the OOPs concepts?

  • Object-oriented
  • Inheritance
  • Encapsulation
  • Polymorphism
  • Abstraction

Q 12: What is Inheritance?

Inheritance means that one class can extend to another. As a result, the codes may be reuse from one class to another. The current class is refer to as the Superclass, and the derive class is refer to as a subclass.

Super class:
public class Multi(){
}
Sub class:
public class Multiplication extends Maltil(){
}

Note: Inheritance is only applicable to the public and protected members only. Private members can’t be inherited.

Q 13: What is Encapsulation?

Purpose of Encapsulation.

  • Protects the code from others.
  • Code maintainability.

‘x’ is declare as an integer variable, and it cannot be negative.

public class Multiplication(){
int x=5;
}

It is fatal if someone changes the exact variable to “x = -5”.

To solve the problem, we must do the steps outlined below:

  • We can make the variable private or protected.
  • Use public accessor methods such as asset and get.
public class Multiplication(){
private int x = 5; //Here the variable is marked as private
}

The code below shows the getter and setter.

get X(){
}
set X(int x){
if(x>0){// Here condition is applied
.........
}
}

To create encapsulation, we must make all instance variables private and define setter and getter methods for them. As a result, people will be force to use the setters rather than directly access the data.

Q 14: What is Polymorphism?

Polymorphism means many forms. Polymorphism refers to the fact that a single object can refer to either the super-class or the sub-class depending on the reference type.

Public class Multi(){ //Super class
public void multi(){
}
}
public class Multiplication extends Multi(){ // Sub class
public void multi(){
}
public static void main(String args[]){
Multi multiplication= new Multiplication();//Multi is reference type and Multiplication is reference type
multiplication.multi();
}
}

We may call the Multiplication class’s “multi()” function using the Multi reference type. Polymorphism refers to this ability. Polymorphism is only useful for overriding, not overloading.

Q 15: What is meant by Method Overriding?

Method overriding occurs when the sub-class method meets the following requirements with the Super-class method:

  • Method name should be the same
  • The argument should be the same
  • Return type should also be the same

The main advantage of overriding is that the Sub-class can offer more detailed information about the sub-class type than the super-class.

public class Multi{ //Super class
public void multi(){
………………
}
}
 
Public class Multiplication extends Multi(){
Public void multi(){
………..
}
Public static void main(String args[]){
Multi multiplication = new Multiplication(); //Polimorphism is applied
multiplication.multi(); // It calls the Sub class add() method
}
}

multiplication.multi() method calls the multi() method in the Sub-class and not the parent class. So it overrides the Super-class method and is known as Method Overriding.

Q 16: What is meant by Overloading?

Method overloading can occur across classes or within the same class.

For method overloading, the sub-class method must meet the following requirements with the Super-class method (or) methods in the same class:

  • Same method name
  • Different argument types
  • There may be different return types
public class Multi{ //Super class
public void multi(String name){ //String parameter
………………
}
}
 
Public class Multiplication extends Multi(){
Public void multi(){//No Parameter
………..
}
Public void multi(int x){ //integer parameter
 
}
Public static void main(String args[]){
Multiplication multiplication = new Multiplication();
multiplication.multi();
}
}

The multi() method in the Multiplication class has distinct parameters and is overloaded in the same class as the super-class.

Note: Polymorphism is not applicable for method overloading.

Q 17: What is meant by Interface?

Multiple inheritances are not possible in Java. The Interface concept is introduce to resolve this problem. An interface is a template that just contains method declarations and does not include method implementation.

Public abstract interface Multi{ //Interface declaration
Public abstract void multi();//method declaration
public abstract void subtract();
}
  • All the methods in the interface are internally public abstract void.
  • All the variables in the interface are internally public static final that is constants.
  • Classes can implement the interface and not extends it.
  • The class which implements the interface should provide an implementation for all the methods declared in the interface.
public class Multipli implements Multi{ //Manupulation class uses the interface
Public void multi(){
……………
}
Public void subtract(){
…………….
}
}

Q 18: What is meant by Abstract class?

We may create the Abstract class by adding the term “Abstract” before the class name. An abstract class can have both “Abstract” and “Non-abstract” concrete class methods.

Abstract method: The abstract method is one that has simply the declaration and no implementation, and it has the term “abstract” attached to it. Declarations are follow by a semicolon.

public abstract class Multi{
public abstract void multi();//Abstract method declaration
Public void subtract(){
}
}
  • An abstract class may have a non-abstract method also.
  • The concrete Subclass which extends the Abstract class should provide the implementation for abstract methods.

Intermediate Java Interview Questions

In this session, we will see Java Interview Questions which were ask by intermediate people who have experience of 1 to 2 years in the field.

Q 19: What is JDK? Mention the variants of JDK?

Java Development Kit is abbreviate as JDK. It is a bundle of JRE and developer tools for creating Java applications and applets. Oracle comes in the following variations.

  • Standard Edition
  • Enterprise Edition
  • Micro Edition

Q 20: What is the difference between JDK, JRE, and JVM?

  • JVM includes a Just in Time (JIT) compiler tool that translates all Java source code into low-level machine language. As a result, it runs quicker than the standard application.
  • JRE includes class libraries as well as other JVM-supporting resources. However, it lacks any Java programming tools such as a compiler or debugger.
  • JDK includes the tools needed to develop Java programs, which are then executed by JRE. It includes a compiler, a launcher for Java applications, and an applet viewer.

Q 21: What is a JIT compiler?

JIT stands for Just in Time compiler. It is the most basic method of executing computer code since it includes compilation during programme execution rather than before. JIT frequently employs bytecode translation to machine code. It is then carry out immediately.

Q 22: What are Brief Access Specifiers and Types of Access Specifiers?

Access Specifiers are preset keywords that assist the JVM in determining the scope of a variable, function, or class. We have four different access specifiers.

  • Public Access Specifier
  • Private Access Specifier
  • Protected Access Specifier
  • Default Access Specifier

Q 23: How many types of constructors are used in Java?

There are two types of constructors that are use in Java.

Parameterized constructor: This type accepts the parameters with which users can initialize the instance variables. Users can dynamically initialize class variables when they instantiate the class.

Default constructors: This type takes no parameters and instead instantiates the class variables with their default values. It is mostly used for object generation.

Q 24: Can a constructor return a value?

Yes, A constructor can return a value. It replaces the class’s current instance implicitly; you cannot make a constructor return a value explicitly.

Q 25: Explain the ‘this’ keyword in Java.

The term “this” refers to a certain keyword that has been select as a reference keyword. The term “this” refers to current class attributes like methods, instances, variables, and constructors.

Q 26: Explain ‘super’ keyword in Java.

The term “super” refers to a certain keyword that has been select as a reference keyword. The “super” keyword refers to the class object’s immediate parent.

Q 27: Can we overload a static method?

No, Java does not enable static method overloading. The process would fail with the error “static method cannot be referenced.”

Q 28: Define Late Binding

Binding is the process of tying together the method call with the method’s code section. Late binding occurs when the code segment of a method is unknown until it is called during runtime.

Q 29: Define Dynamic Method Dispatch.

Dynamic method dispatch is a technique that executes the method call at runtime. The super-class is accessed through a reference variable. This is sometimes referred to as Run-Time Polymorphism.

Q 30: Why is the delete function faster in the linked list than in an array?

In linked lists, the delete function is quicker since the user only has to make a modest modification to the pointer value so that the node can refer to the next successor in the list.

Advanced Java Interview Questions

In this session, we will see Java Interview Questions which are asked by experienced people who have experience of 3 to 4 years in the field.

Q 31: Give a briefing on the life cycle of a thread.

The life cycle of a thread includes five stages, as mentioned below.

  • New Born State
  • Runnable State
  • Running State
  • Blocked State
  • Dead State

Q 32: Explain the difference between >> and >>> operators.

Although they look similar, there is a massive difference between them.

  • >> operator does the job of right shifting the sign bits
  • >>> operator is used in shifting out the zero-filled bits

Q 33: Brief the life cycle of an applet.

The life cycle of an applet involves the following.

  • Initialization
  • Start
  • Stop
  • Destroy
  • Paint

Q 34: Why are generics used in Java Programming?

The use of generics provides compile-time type safety. Compile-time type safety allows users to detect superfluous incorrect types during the compilation process. Generic methods and classes enable programmers to define a single method declaration, a group of related methods, or a set of related types with an accessible class declaration.

Q 35: Explain the Externalizable interface.

The Externalizable interface helps in controlling the serialization process. ReadExternal and writeExternal methods are included in an “externalizable” interface.

Q 36: What is the Daemon Thread?

The Daemon thread is the thread with the lowest priority. This Daemon thread is intended to operate in the background during Java’s garbage collection. In Java, the setDaemon() function generates a Daemon thread.

Q 37: Explain the term enumeration in Java.

Enumeration, sometimes known as enum, is a Java interface. In Java, enum provides for the sequential access of elements stored in a collection.

Q 38: Why is Java is Dynamic?

Java is designed to adapt to a changing environment. Java applications contain a substantial quantity of runtime information that is utilized to resolve real-time access to objects.

Q 39: Can you run a code before executing the main method?

Yes, we may run any code before the main function. When we create the objects at the class’s load time, we shall use a static block of code. Any statements included within this static block of code will be performed at the same time as the class is loaded, even before instances are created in the main function.

Q 40: How many times is the finalized method called?

The Garbage collector technique is the name given to the finalized method. The Garbage Collector only calls the finalize () function once for each object.

Q 41: What is JDBC?

JDBC is an acronym that stands for Java Database Connector. JDBC is an abstraction layer that is used to link an existing database to a Java program.

Q 42: What is Session Management in Java?

A session is essentially defined as the dynamic state of a random communication between the client and the server. A string of replies and requests from both sides is included in the virtual communication channel. The most common method of implementing session management is to create a session ID in both the client’s and the server’s communicative discourse.

Q 43: Briefly explain the term Spring Framework.

Spring is primarily described as a Java application framework and inversion of control containers. The spring framework is used to develop Java corporate applications. It’s also important to remember that the spring framework’s core capabilities are generally applicable to any Java application.

Q 44: What is JCA in Java?

Java Cryptography Architectural (JCA) provides a framework for decryption and encryption, as well as architecture and application programming interfaces. It is used by developers to integrate the application with security applications. The Java Cryptography Architecture facilitates the implementation of third-party security rules and regulations.

To accomplish security, Java Cryptography Architecture uses hash tables, encryption message digests, and other techniques.

Q 45: Explain JPA in Java.

We may develop the persistence layer for desktop and online apps using the Java Persistence API. Java Persistence is concerned with the following:

  • Java Persistence API
  • Query Language
  • Java Persistence Criteria API
  • Object Mapping Metadata

Q 46: Write a Program to remove duplicates in an ArrayList.

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

public class ArrayDuplicate {
    public static void main(String args[]) {

List<Integer> num = new ArrayList<Integer>();
num.add(1);
num.add(2);
num.add(3);
num.add(4);
num.add(5);
num.add(6);
num.add(3);
num.add(4);
num.add(5);
num.add(6);

System.out.println("Your list of elements in ArrayList : " + num);
Set<Integer> primesWithoutDuplicates = new LinkedHashSet<Integer>(num);
num.clear();
num.addAll(primesWithoutDuplicates);
System.out.println("list of original numbers without duplication: " + num);

}
}

Output:

Your list of elements in ArrayList : [1, 2, 3, 4, 5, 6, 3, 4, 5, 6]
list of original numbers without duplication: [1, 2, 3, 4, 5, 6]

Q 47: Find the word count in a string using HashMap Collection.

import java.util.HashMap;

public class WordCount {
public static void main(String[] args) {

String str = "Hello World, Welcome to Softhunt";
String[] split = str.split(" ");

HashMap<String, Integer> map = new HashMap<String, Integer>();

for (int i = 0; i < split.length; i++) {
if (map.containsKey(split[i])) {
int count = map.get(split[i]);
map.put(split[i], count + 1);
} else {
map.put(split[i], 1);
}
}
System.out.println(map);
}
}

Output:

{Hello=1, Welcome=1, Softhunt=1, to=1, World,=1}

Q 48: What is the difference between System.out, System.err, and System.in?

System.out and System.err are the monitor’s default representations and may thus be used to deliver data or results to the monitor. System.out is used to output standard messages and results. Error messages are shown using System.err. System.in provides an InputStream object, which represents a conventional input device, such as a keyboard, by default.

Q 49: Explain Exception Propagation.

The method at the top of the stack throws the first exception. If it does not catch, it displays the technique and proceeds on to the preceding way, and so on until they are obtained.

Q 50: What is SerialVersionUID

When an item is Serialized, it is stamped with the object class’s version ID number. This ID is known as the SerialVersionUID. This is used during deserialization to ensure that the sender and recipient are Serializations compliant.

Java Interview Questions: Conclusion

That’s all for this article, if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn

Suggested Articles:

  1. Pyth10 Most Popular Programming Languages

Leave a Comment