Back to IQs

Java Interview Questions

What is Java?
Java is a high-level, object-oriented programming language that is platform-independent and widely used for building applications.
What are Java's main features?
Java features include platform independence, object-oriented design, robustness, security, multithreading, and automatic garbage collection.
What is the JVM?
The Java Virtual Machine (JVM) is an engine that provides a runtime environment to execute Java bytecode. It helps achieve platform independence.
What is the difference between JDK and JRE?
JDK (Java Development Kit) is used for developing Java applications, while JRE (Java Runtime Environment) is used for running them.
What is the use of the 'final' keyword?
'final' is used to declare constants, prevent method overriding, and prevent inheritance in Java.
What are Java collections?
Java Collections are a set of classes and interfaces that implement commonly reusable collection data structures like List, Set, and Map.
What is a constructor in Java?
A constructor is a special method used to initialize objects. It has the same name as the class and does not have a return type.
What is method overloading?
Method overloading in Java is when two or more methods in the same class have the same name but different parameters.
What is the difference between '== 'and 'equals()' in Java?
'==' checks reference equality (whether two references point to the same object), while 'equals()' checks content equality (whether the two objects are logically equal).
What are access modifiers in Java?
Access modifiers in Java are keywords like 'public', 'private', 'protected', and 'default' that define the visibility of classes, methods, and variables.
What is multithreading in Java?
Multithreading is the concurrent execution of more than one sequential set of instructions, or thread. It improves the performance of applications by doing multiple tasks at the same time.
What is exception handling in Java?
Exception handling in Java is a mechanism that allows the handling of runtime errors to maintain the normal flow of application execution using 'try', 'catch', 'finally' blocks.
What is a lambda expression?
Lambda expressions are a way to provide clear and concise syntax for writing anonymous methods (functional interfaces).
What are Java Streams?
Java Streams allow functional-style operations on sequences of elements (like arrays or collections), such as filtering, mapping, and reducing.
What is an interface in Java?
An interface in Java is an abstract type that defines a contract of methods which implementing classes must provide implementations for.
What is a package in Java?
A package in Java is a namespace that organizes classes and interfaces into a directory structure.
What is the difference between an ArrayList and LinkedList?
ArrayList provides fast random access with O(1) average get operations and is the usual default. LinkedList has O(1) insertion or deletion only when the node is already located, but traversal and memory overhead often make it slower in practice.
What is the difference between String and StringBuilder?
String is immutable, meaning its value cannot be changed once created. StringBuilder is mutable, and its value can be modified.
What is the significance of the 'super' keyword?
The 'super' keyword refers to the immediate parent class of the current object and is used to invoke superclass methods or constructors.
What is garbage collection in Java?
Garbage collection in Java is the process of automatically reclaiming memory by removing objects that are no longer in use.
What is a thread in Java?
A thread in Java is a lightweight process that can run concurrently with other threads, enabling multitasking in applications.
What is serialization in Java?
Serialization is the process of converting an object into a byte stream, allowing it to be saved to a file or transmitted over a network.
What is the 'this' keyword in Java?
The 'this' keyword refers to the current instance of the class. It is often used to distinguish instance variables from parameters.
What is reflection in Java?
Reflection in Java allows you to inspect and modify the properties, methods, and behaviors of classes at runtime.
What is the difference between 'throw' and 'throws'?
'throw' is used to explicitly throw an exception, while 'throws' is used to declare that a method may throw an exception.
What is the contract between equals() and hashCode()?
If two objects are equal according to equals(), they must return the same hashCode(). A class that overrides equals() should therefore override hashCode() too, otherwise HashMap and HashSet can behave incorrectly.
What is the difference between synchronized and volatile in Java?
volatile guarantees visibility of a variable across threads but does not make compound operations atomic. synchronized provides mutual exclusion and visibility, so it is needed when multiple operations must happen as one critical section.
What is CompletableFuture used for?
CompletableFuture represents an asynchronous computation that can be composed with stages such as thenApply, thenCompose, and exceptionally. It is useful for coordinating non-blocking workflows, but its executor, timeout, and error-handling policies must be designed explicitly.
What are records and sealed classes in modern Java?
A record is a concise, shallowly immutable data carrier that supplies accessors, equals(), hashCode(), and toString(). A sealed class or interface restricts which types may extend or implement it, making a closed domain model easier to validate and pattern-match.
How do you investigate high memory usage in a Java service?
Confirm the growth with metrics, inspect heap usage and garbage-collection pauses, then capture a heap dump and identify dominators or retained references. Common causes include unbounded caches, static collections, listener leaks, and oversized request data; fix the ownership or eviction policy rather than only increasing the heap.
What are virtual threads, and when should you use them?
Virtual threads are lightweight Java threads designed for high-concurrency, mostly blocking I/O workloads. They can simplify request-per-task code, but they do not make CPU-heavy work faster; bound database connections and external resources, avoid pinning on long synchronized sections, and measure throughput before migrating.
What is the difference between a platform thread and a virtual thread?
Platform threads are typically backed one-to-one by operating-system threads and are relatively expensive. Virtual threads are managed by the JVM and are mounted on a smaller pool of carrier threads, allowing many waiting tasks, but native calls, pinning, and limited downstream resources can still constrain a service.
How do you design a thread pool for a Java service?
Separate CPU-bound and I/O-bound work, size pools from measured workload and downstream limits, use bounded queues, define rejection or backpressure behavior, and propagate timeouts and cancellation. An unbounded executor can turn traffic spikes into memory exhaustion instead of protecting the service.