Interview logo

Java Interview Question and Answers

For Beginners

By srikanth yandrapuPublished 3 years ago 7 min read

What is a class in Java?

A class in Java is a blueprint for creating objects. It is a blueprint for the object and it holds a bunch of methods and variables that describe the state and behavior of the object.

class ClassName {

// fields

// methods

}

What is an object in Java?

An object in Java is an instance of a class, which has its own state and behavior. You can create an object by using the "new" keyword followed by the constructor of the class.

ClassName objectName = new ClassName();

What is inheritance in Java?

Inheritance in Java is a mechanism where a class can inherit properties and methods from a parent class. The child class is called the subclass, and the parent class is called the superclass. This allows you to reuse code and structure in a more organized way.

class SuperClass {

// fields

// methods

}

class SubClass extends SuperClass {

// fields

// methods

}

What is polymorphism in Java?

Polymorphism in Java is the ability of an object to take on multiple forms. It allows objects of different classes to be treated as objects of the same class. This is achieved through method overriding.

class Shape {

void draw() {

System.out.println("Drawing a shape");

}

}

class Circle extends Shape {

void draw() {

System.out.println("Drawing a circle");

}

}

class Square extends Shape {

void draw() {

System.out.println("Drawing a square");

}

}

What is the difference between overloading and overriding in Java?

Overloading in Java is when a class has multiple methods with the same name but different parameters. This allows you to reuse method names with different functionality.

Overriding in Java is when a subclass provides a new implementation for a method that is already defined in its superclass. This allows you to change the behavior of a method inherited from a superclass.

class Parent {

void display() {

System.out.println("Parent display");

}

}

class Child extends Parent {

void display() {

System.out.println("Child display");

}

void display(int i) {

System.out.println("Child display with int parameter");

}

}

What is the difference between String, StringBuilder, and StringBuffer in Java?

String is an immutable class in Java, which means once a string object is created, its value can't be changed.

StringBuilder and StringBuffer are mutable classes in Java, which means their values can be changed.

The difference between StringBuilder and StringBuffer is that StringBuilder is not thread-safe, whereas StringBuffer is thread-safe. This means if multiple threads are accessing and modifying a StringBuilder object simultaneously, it may lead to inconsistent data, whereas this is not the case with StringBuffer.

String str = "Hello";

str = str + " World";

StringBuilder sb = new StringBuilder("Hello");

sb.append(" World");

StringBuffer sbf = new StringBuffer("Hello");

sbf.append(" World");

What is the difference between == and equals() method in Java?

The '==' operator is used to compare the references of two objects to check if they refer to the same object in memory.

The 'equals()' method is used to compare the values of two objects to check if they are equal.

String str1 = new String("Hello");

String str2 = new String("Hello");

System.out.println(str1 == str2); // Output: false

System.out.println(str1.equals(str2)); // Output: true

What is the purpose of the finally block in Java?

The finally block is used to ensure that a piece of code is executed regardless of whether an exception is thrown or not. It is typically used for cleanup activities like closing a file, releasing resources, etc.

try {

// Code that may throw an exception

} catch (Exception e) {

// Code to handle the exception

} finally {

// Cleanup code that will be executed regardless of whether an exception was thrown or not

}

Can you explain the difference between abstract class and interface in Java?

An abstract class is a class that can't be instantiated on its own and must be subclassed. It can contain both abstract methods (methods without a body) and concrete methods (methods with a body).

An interface, on the other hand, is a blueprint for classes and can only contain abstract methods. A class that implements an interface must provide a concrete implementation for all the methods defined in the interface.

abstract class Shape {

abstract void draw();

void fillColor() {

System.out.println("Filling color");

}

}

interface Shape {

void draw();

}

class Circle implements Shape {

@Override

void draw() {

System.out.println("Drawing Circle");

}

}

Can you explain the difference between a HashMap and a TreeMap in Java?

A HashMap is an unordered map that uses a hash table for storage, while a TreeMap is a sorted map that uses a red-black tree for storage. HashMap has faster lookups and insertion, but TreeMap has slower operations and maintains the order of elements.

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

hashMap.put(1, "Value1");

hashMap.put(2, "Value2");

TreeMap<Integer, String> treeMap = new TreeMap<>();

treeMap.put(1, "Value1");

treeMap.put(2, "Value2");

What is the difference between a Stack and a Queue in Java?

A Stack is a Last-In-First-Out (LIFO) data structure, while a Queue is a First-In-First-Out (FIFO) data structure.

Stack<Integer> stack = new Stack<>();

stack.push(1);

stack.push(2);

stack.pop(); // returns 2

Queue<Integer> queue = new LinkedList<>();

queue.add(1);

queue.add(2);

queue.remove(); // returns 1

What is the difference between static and non-static methods in Java?

Static methods belong to the class and can be called without creating an instance of the class. Non-static methods belong to instances of the class and require an instance to be created before they can be called.

class Example {

static void staticMethod() {

System.out.println("Static method");

}

void nonStaticMethod() {

System.out.println("Non-static method");

}

}

public class Main {

public static void main(String[] args) {

Example.staticMethod(); // Call static method

Example example = new Example();

example.nonStaticMethod(); // Call non-static method

}

}

What is batch processing in Java?

Answer: Batch processing is a technique for processing large volumes of data by breaking it down into smaller, manageable chunks and processing each chunk in a separate process or thread. In Java, batch processing is commonly used for tasks such as data migration, data import/export, and data aggregation.

Can you give an example of how to implement batch processing in Java?

Answer: One simple example of batch processing in Java is reading a large file in chunks, processing each chunk and writing the processed data to a separate file. Here's an example implementation:

public static void main(String[] args) throws IOException {

final int chunkSize = 100;

try (BufferedReader reader = new BufferedReader(new FileReader("input.txt"))) {

String line;

int chunkCounter = 0;

List<String> chunk = new ArrayList<>();

while ((line = reader.readLine()) != null) {

chunk.add(line);

chunkCounter++;

if (chunkCounter == chunkSize) {

processChunk(chunk);

chunkCounter = 0;

chunk.clear();

}

}

if (!chunk.isEmpty()) {

processChunk(chunk);

}

}

}

private static void processChunk(List<String> chunk) {

try (BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt", true))) {

for (String line : chunk) {

writer.write(line);

writer.newLine();

}

} catch (IOException e) {

e.printStackTrace();

}

}

What is multithreading in batch processing and why is it important?

Answer: Multithreading in batch processing refers to the use of multiple threads to process the data in parallel. It is important because it allows for faster processing times and improved resource utilization compared to a single-threaded batch process. By breaking the data into smaller chunks and processing each chunk in a separate thread, the program can utilize multiple cores and processors, resulting in a significant improvement in processing speed.

What is the difference between HashMap and Hashtable in Java?

HashMap is not thread-safe, while Hashtable is thread-safe. HashMap allows null values as key and value, while Hashtable doesn't allow nulls. HashMap has better performance than Hashtable.

Can you explain the difference between ArrayList and LinkedList in Java?

ArrayList uses an array to store elements, while LinkedList uses a linked list. ArrayList has better random access performance, while LinkedList has better insertion and deletion performance.

What is the use of the synchronized keyword in Java?

Answer: The synchronized keyword in Java is used to control access to shared resources by multiple threads, ensuring that only one thread can access a shared resource at a time. This is useful for avoiding race conditions and ensuring that data is not corrupted by concurrent access.

How does the Java memory management system work?

Answer: Java uses a garbage collector to automatically manage memory. When an object is no longer referenced, the garbage collector reclaims its memory. The Java heap is the area of memory used for dynamic allocation of objects, while the stack is used for storing method invocations.

What is the difference between private and protected in Java?

Answer: The private keyword in Java restricts access to members of a class to the class itself, while the protected keyword allows access to members from the same class and its subclasses.

What is the difference between Java and J2EE?

Answer: Java is a programming language used to develop software applications, whereas J2EE (Java 2 Enterprise Edition) is a platform for developing enterprise-level applications, specifically for server-side web applications. Java is the programming language used to write J2EE applications.

What is a Singleton class in Java?

Answer: A Singleton class in Java is a class that allows only one instance of itself to be created, ensuring that there is only one instance of the class in the JVM. This can be useful in situations where you want to ensure that there is only one instance of a class that controls access to a shared resource. Singleton classes are typically implemented by making the constructor private and providing a static method that returns the single instance of the class.

Creators

About the Creator

srikanth yandrapu

Technical Writer

Reader insights

Be the first to share your insights about this piece.

How does it work?

Add your insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

Sign in to comment

    Find us on social media

    Miscellaneous links

    • Explore
    • Contact
    • Privacy Policy
    • Terms of Use
    • Support

    © 2026 Creatd, Inc. All Rights Reserved.