Java Interview Questions


Java Interview Questions

 

What is Java?

Java is a high-level, object-oriented programming language developed by Sun Microsystems (now owned by Oracle). It is designed to be platform-independent and is commonly used for developing web, mobile, and enterprise applications.

What are the main features of Java?

Some main features of Java include its platform independence, object-oriented nature, robustness, security, and ease of use.

What is the difference between JDK, JRE, and JVM?

Some key diffrences between JDK, JRE, and JVM in Java are:

Feature JDK (Java Development Kit) JRE (Java Runtime Environment) JVM (Java Virtual Machine)
Purpose Provides environment for developing Java applications Provides environment to run Java applications Executes Java bytecode
Components Contains JRE, development tools (compilers, debuggers, etc.) Contains JVM, core libraries, and other runtime components Part of JRE, provides the runtime environment to execute bytecode
Usage Used by developers for writing, compiling, and debugging Java code Used by end-users to run Java applications Used by both JDK and JRE to execute Java programs
Includes JRE, compiler (javac), debugger, and other development tools JVM, core libraries, and other runtime components Class loader, bytecode verifier, and runtime interpreter
Installation Requirement Required to develop Java applications Required to run Java applications Not installed separately, part of JRE and JDK

What is inheritance in Java?

Inheritance allows a class (subclass) to inherit properties and behaviors from another class (superclass), promoting code reuse and organization. It's achieved using the 'extends' keyword, enabling subclasses to inherit non-private members from superclasses, facilitating hierarchical relationships between classes.

What is the difference between abstraction and encapsulation?

Some key diffrences between abstraction and encapsulation in Java are:

Feature Abstraction Encapsulation
Purpose Hides complexity by providing a simplified interface Hides the internal state and behavior of an object
Focus On what an object does On how an object is protected and controlled
Implementation Achieved using abstract classes and interfaces Achieved using access modifiers (private, public, protected)
Example Abstract methods and interfaces Private fields with public getter and setter methods
Usage To show essential features and hide background details To restrict direct access to some of an object's components

What is polymorphism in Java?

Polymorphism is the ability of an object to take on different forms. In Java, it can be achieved through method overriding (runtime polymorphism) and method overloading (compile-time polymorphism).

What is method overloading and method overriding?

Method overloading and method overriding are two important concepts in Java that involve the use of methods:

Method Overloading:

  • Definition: Method overloading occurs when a class has multiple methods with the same name but different parameters.
  • Purpose: It allows a class to have multiple methods with the same name, providing flexibility and convenience to the programmer.
class Calculator {
    // Method with two integer parameters
    public int add(int a, int b) {
        return a + b;
    }

    // Method with three integer parameters
    public int add(int a, int b, int c) {
        return a + b + c;
    }
}

public class MethodOverloadingExample {
    public static void main(String[] args) {
        Calculator calc = new Calculator();
        System.out.println(calc.add(2, 3)); // Calls the first add method
        System.out.println(calc.add(2, 3, 4)); // Calls the second add method
    }
}

Method Overriding:

  • Definition: Method overriding occurs when a subclass provides a specific implementation of a method that is already defined in its superclass.
  • Purpose: It allows a subclass to provide its own implementation of a method, which may be different from the implementation in the superclass.
class Animal {
    public void sound() {
        System.out.println("Animal makes a sound");
    }
}

class Dog extends Animal {
    // Overriding the sound method in the superclass
    @Override
    public void sound() {
        System.out.println("Dog barks");
    }
}

public class MethodOverridingExample {
    public static void main(String[] args) {
        Animal animal = new Dog(); // Upcasting
        animal.sound(); // Calls the overridden method in Dog class
    }
}

What is a constructor in Java?

A constructor in Java is a special type of method that is automatically called when an object of a class is created. It is used to initialize the object's state.

What is the difference between constructors and methods?

Some key diffrences between constructors and methods in Java are:

Feature Constructors Methods
Purpose Used to initialize a new object Used to perform operations, computations, or actions
Name Same as the class name Can be any valid identifier
Return Type No return type, not even void Must have a return type (void or a specific data type)
Invocation Called automatically when an object is created Called explicitly using the object's reference
Overloading Can be overloaded (multiple constructors with different parameters) Can be overloaded (multiple methods with different parameters)
Inheritance Not inherited by subclasses, but can be called using super() Inherited by subclasses if not private
Default Availability Compiler provides a default no-argument constructor if none is defined No default methods are provided by the compiler
Special Keywords Uses this() to call another constructor in the same class, super() to call a superclass constructor Uses this to refer to the current object, super to refer to superclass methods

What are access modifiers in Java?

Access modifiers are keywords used to specify the accessibility of classes, methods, and variables in Java. The main access modifiers are public, private, protected, and default (package-private).

What is the 'static' keyword in Java?

The 'static' keyword in Java is used to create variables and methods that belong to the class, rather than instances of the class. Static variables and methods are shared among all instances of the class.

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

Some key diffrences between an abstract class and an interface in Java are:

Feature Abstract Class Interface
Nature Can have both abstract and concrete methods Can only have abstract methods (until Java 8), default methods (from Java 8), and static methods (from Java 8)
Multiple Inheritance Supports single inheritance (a class can extend only one abstract class) Supports multiple inheritance (a class can implement multiple interfaces)
State Can have instance variables (state) Cannot have instance variables, only constants (implicitly public, static, and final)
Constructor Can have constructors Cannot have constructors
Method Modifiers Can have methods with any visibility (public, protected, private) All methods are public by default (until Java 9, private methods allowed from Java 9)
Implementation Can provide method implementations Cannot provide implementations for methods until Java 8 (default and static methods)
Use Case Suitable for shared base functionality among related classes Suitable for defining a contract for what a class can do, but not how it does it

What is a package in Java?

A package in Java is a grouping of related classes and interfaces. It helps in organizing and managing classes and avoids naming conflicts.

What is the 'final' keyword in Java?

The 'final' keyword in Java is used to restrict the user. It can be applied to classes, methods, and variables. When a class is declared as final, it cannot be subclassed. When a method is declared as final, it cannot be overridden. When a variable is declared as final, its value cannot be changed once initialized.

What is the difference between abstract class and interface?

Abstract classes and interfaces are both important concepts in Java, but they have some key differences:

Feature Abstract Class Interface
Definition Cannot be instantiated directly; may have abstract methods and concrete methods Defines a contract for classes; contains only abstract methods (before Java 8) and default/static methods (from Java 8)
Usage Provides a common base for related classes, offering both common and specific behaviors Defines capabilities a class can provide, allowing multiple inheritance
Inheritance Supports single inheritance; a class can extend only one abstract class Supports multiple inheritance; a class can implement multiple interfaces
Method Implementation Can have both abstract and concrete methods; subclasses must implement abstract methods Contains only abstract methods (before Java 8); can have default/static methods with implementations (from Java 8)
Constructor Can have constructors Cannot have constructors
Fields Can have instance variables Cannot have instance variables

What is a thread in Java?

A thread in Java represents a separate path of execution within a program. It allows concurrent execution of tasks, improving the performance and responsiveness of the application.

How do you create a thread in Java?

There are two ways to create a thread in Java: by extending the Thread class and by implementing the Runnable interface and passing it to a Thread object.

What is the 'try-with-resources' statement in Java?

The 'try-with-resources' statement in Java is used to automatically close resources (such as streams, connections, etc.) that are used within a try block. It ensures that the resources are closed properly, even if an exception occurs.

What is the difference between checked and unchecked exceptions in Java?

Some key diffrences between checked and unchecked exceptions in Java are:

Feature Checked Exceptions Unchecked Exceptions
Inheritance Subclass of Exception except for RuntimeException Subclass of RuntimeException
Compile-Time Checking Checked at compile-time Checked at runtime
Handling Requirement Must be either caught or declared in the method signature Not required to be caught or declared
Common Examples IOException, SQLException, ClassNotFoundException NullPointerException, ArrayIndexOutOfBoundsException, ArithmeticException
Purpose Represent conditions that a well-written application should anticipate and recover from Indicate programming errors or unexpected conditions that could be avoided with proper code logic
try { int[] array = new int[5]; System.out.println(array[10]); }
catch (ArrayIndexOutOfBoundsException e) {}
finally { System.out.println("Finally block executed"); }

What is the 'volatile' keyword in Java?

The 'volatile' keyword in Java is used to indicate that a variable's value may be changed by multiple threads simultaneously. It ensures that the variable is always read from and written to the main memory, rather than the thread's cache.

What is exception handling in Java?

Exception handling in Java is a mechanism to handle runtime errors (exceptions) gracefully. It involves catching and handling exceptions using try-catch blocks, and specifying exception handling code using the 'throws' keyword.

What is the difference between ArrayList and LinkedList?

Some key diffrences between ArrayList and LinkedList in Java are:

Feature ArrayList LinkedList
Data Structure Dynamic array Doubly linked list
Element Access Fast random access (O(1) for get) Slow random access (O(n) for get)
Insertion/Deletion Slow for inserting/removing in the middle (O(n)) Fast for inserting/removing in the middle (O(1) for add/remove)
Memory Usage Less memory overhead More memory overhead due to storing pointers
Iteration Performance Faster for sequential access due to better cache locality Slower for sequential access
Methods Implements only List interface Implements List, Deque, and Queue interfaces
Use Case Best for applications with frequent access and few modifications Best for applications with frequent insertions/deletions

What is synchronization in Java?

Synchronization in Java is the process of controlling the access of multiple threads to shared resources. It prevents data inconsistency and thread interference by allowing only one thread to execute a synchronized block of code at a time.

What is method overriding in Java?

Method overriding in Java is a mechanism where a subclass provides a specific implementation of a method that is already defined in its superclass. It allows the subclass to provide its own implementation of inherited behavior.

What is the difference between HashMap and HashTable in Java?

Some key diffrences between HashMap and Hashtable in Java are:

Feature HashMap Hashtable
Thread Safety Not thread-safe Thread-safe (synchronized methods)
Null Keys/Values Allows one null key and multiple null values Does not allow null keys or values
Performance Generally faster due to lack of synchronization Generally slower due to synchronization overhead
Inheritance Extends AbstractMap Extends Dictionary
Introduced in Java 1.2 Java 1.0
Iterators Fail-fast (throws ConcurrentModificationException if modified during iteration) Fail-safe (no guarantee for ConcurrentModificationException)
Initial Capacity Default initial capacity of 16 Default initial capacity of 11
Load Factor Default load factor of 0.75 Default load factor of 0.75

What is method hiding in Java?

Method hiding in Java occurs when a subclass defines a static method with the same signature as a static method in its superclass. The subclass's method hides the superclass's method, and the method to be invoked is determined at compile time based on the reference type.

What is the 'instanceof' operator in Java?

The 'instanceof' operator in Java is used to check whether an object is an instance of a particular class or interface. It returns true if the object is an instance of the specified type, otherwise false.

class Animal {} class Dog extends Animal {}
class InstanceOfExample { public static void main(String[] args) {} }

What is the 'forEach' method in Java?

The 'forEach' method in Java is used to iterate over elements in a collection (such as lists, sets, or maps) and perform a specified action for each element. It provides a more concise and expressive way to iterate over collections compared to traditional loops.

What is serialization and deserialization in Java?

Serialization in Java is the process of converting an object into a byte stream, which can be stored in a file, sent over a network, or saved in a database. Deserialization is the reverse process of converting a byte stream back into an object.

What are anonymous classes in Java?

Anonymous classes in Java are classes without a name that are defined and instantiated at the same time. They are typically used when you need to create a class only once and don't want to reuse it elsewhere in the code.

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

Some key diffrences between String, StringBuffer, and StringBuilder in Java are:

Feature String StringBuffer StringBuilder
Mutability Immutable Mutable Mutable
Thread Safety Thread-safe (implicitly due to immutability) Thread-safe (synchronized methods) Not thread-safe (unsynchronized methods)
Performance Slower for concatenation and modification operations Slower than StringBuilder due to synchronization Faster than StringBuffer due to lack of synchronization
Usage Context When strings are constant and will not change When strings are modified by multiple threads When strings are modified by a single thread
Default Length Internally uses a char[] array (length fixed on creation) Can be resized dynamically as needed Can be resized dynamically as needed
Introduced in Java 1.0 Java 1.0 Java 5

What is the 'this' keyword in Java?

The 'this' keyword in Java is a reference to the current object within a method or constructor. It is used to differentiate between instance variables and local variables with the same name, and to invoke current class constructors.

What is method chaining in Java?

Method chaining in Java is a technique where multiple methods are called on the same object in a single line, by returning the object itself from each method call. It allows for more concise.

What is a lambda expression in Java?

A lambda expression in Java is a concise way to represent an anonymous function (a function without a name). It allows you to pass functions as arguments, return functions, and store them in variables.

What are functional interfaces in Java?

Functional interfaces in Java are interfaces that have exactly one abstract method. They are used to implement lambda expressions and method references, enabling functional programming features in Java.

What is the difference between 'throw' and 'throws' in Java?

Some key diffrences between 'throw' and 'throws' in Java are:

Feature throw throws
Usage Used to explicitly throw an exception Used to declare an exception in a method
Syntax throw <exception> <method signature> throws <exception>
Purpose Signals that an exception occurred Declares that a method might throw an exception
Placement Inside a method to raise an exception In the method signature to indicate potential exceptions
Handling Directly raises an exception Indicates possible exceptions to be handled by callers
class CustomException extends Exception {}
class ThrowExample { void validate(int age) throws CustomException {} }
class ThrowsExample { public static void main(String[] args) {} }

What is the 'super' keyword in Java?

The 'super' keyword in Java is used to refer to the superclass's members (methods, variables, constructors) from within a subclass. It is also used to invoke the superclass's constructor from a subclass constructor.

What is autoboxing and unboxing in Java?

Autoboxing is the automatic conversion of primitive data types into their corresponding wrapper class objects, and unboxing is the automatic conversion of wrapper class objects back to their corresponding primitive data types.

What is the 'finalizer' method in Java?

The 'finalizer' method in Java is a special method provided by the Object class that is called by the garbage collector before reclaiming the memory occupied by an object. It is rarely used due to its unpredictability and performance implications.

What are annotations in Java?

Annotations in Java are a form of metadata that provide data about a program but do not directly affect the program's execution. They are used to provide information to the compiler, tools, or runtime environment.

What is the difference between '==', 'equals()', and 'hashCode()' methods?

Some key diffrences between '==', 'equals()', and 'hashCode()' methods are:

Feature == Operator equals() Method hashCode() Method
Purpose Compares references (memory addresses) Compares the actual content (state) of objects Returns an integer hash code representing the object
Default Behavior Compares if two references point to the same object Checks if two objects are the same object (default in Object) Generates a unique integer (default in Object)
Overrideable No Yes Yes
Usage Context Used for primitive data types and reference comparison Used for meaningful equality checks of object contents Used in hash-based collections like HashMap and HashSet
Performance Typically faster since it is a simple reference check Can be slower as it might involve complex comparison logic Usually fast, but must be consistent with equals()
Consistency Requirement N/A Must be consistent: if a.equals(b) is true, then a.hashCode() must equal b.hashCode() Consistent with equals(): if a.equals(b) is true, then a.hashCode() must equal b.hashCode()

What is the 'assert' statement in Java?

The 'assert' statement in Java is used to test assumptions in the code during development and debugging. It throws an AssertionError if the specified condition is false, indicating a bug in the program.

What is the 'stream' API in Java?

The 'stream' API in Java is a feature introduced in Java 8 that allows for functional-style operations on collections of objects. It provides a powerful and efficient way to process data in a declarative manner.

abstract class Shape {
    abstract void draw();
    void display() {
        // Concrete method
    }
}

interface Drawable {
    void draw();
    default void display() {
        // Default method
    }
}

class Circle extends Shape implements Drawable {
    public void draw() {
        // Implementation
    }
}

What is the 'Optional' class in Java?

The 'Optional' class in Java is a container object that may or may not contain a non-null value. It is used to avoid NullPointerExceptions and to represent the absence of a value in a more expressive way.

What is the 'DateTime' API in Java?

The 'DateTime' API in Java, introduced in Java 8, provides classes for representing dates, times, and time intervals. It offers improved functionality and better handling of date and time operations compared to the previous 'Date' and 'Calendar' classes.

What is the 'Comparator' interface in Java?

The 'Comparator' interface in Java is used to define custom sorting orders for objects. It provides methods for comparing two objects and determining their relative ordering based on certain criteria.

abstract class Vehicle {
    abstract void start();
    void stop() {
        System.out.println("Vehicle stopped");
    }
}

interface Shape {
    void draw();
    default void display() {
        System.out.println("Displaying shape");
    }
}

class Circle implements Shape {
    public void draw() {
        System.out.println("Drawing Circle");
    }
}

What is the 'Comparable' interface in Java?

The 'Comparable' interface in Java is used to define a natural ordering for objects of a class. It provides a single method, 'compareTo()', which is used to compare the current object with another object and determine their relative ordering.

What is the 'transient' keyword in Java?

The 'transient' keyword in Java is used to indicate that a variable should not be serialized when the object is serialized. It is typically used for variables that are derived from transient state or are not essential to persist.

What is the 'strictfp' keyword in Java?

The 'strictfp' keyword in Java is used to ensure consistent floating-point calculations across different platforms. It restricts floating-point calculations to ensure the same result is obtained regardless of the underlying platform's floating-point behavior.

public strictfp class Calculation {
    strictfp double performCalculation(double a, double b) {
        return a * b + 10.5;
    }
}

public class StrictfpExample {
    public static void main(String[] args) {
        Calculation calc = new Calculation();
        double result = calc.performCalculation(3.14, 2.0);
        System.out.println("Result: " + result);
    }
}

What is the 'ClassLoader' in Java?

The 'ClassLoader' in Java is a subsystem responsible for loading classes into memory at runtime. It is hierarchical and follows the parent-first delegation model, where a class is first searched in the parent class loader before searching in the current class loader.

What is the 'finalize()' method in Java?

The 'finalize()' method in Java is a method provided by the Object class that is called by the garbage collector before reclaiming the memory occupied by an object. It can be overridden by subclasses to perform cleanup operations before the object is garbage collected. However, it is not recommended to rely on 'finalize()' for resource cleanup due to its unpredictable nature.