Interview Q&A / Java
Java interview questions
Collections, the JVM, strings, exceptions and threads, the backbone of most campus placement rounds.
Prefer rapid revision? Practise Java short interview answers →
- What is the difference between orElse and orElseGet in Java Optional?
orElse takes a ready value that is always evaluated, even when the Optional has a value. orElseGet takes a Supplier that runs only when the Optional is empty, so use it when the fallback is expensive or has side effects.
IntermediateQuick answer1 min read - What is the difference between Runnable and Callable, and what is a Future?
Runnable.run returns nothing and cannot throw checked exceptions. Callable.call returns a value and may throw. Submitting a Callable to an ExecutorService gives a Future, a handle you use to fetch the result later.
IntermediateQuick answer1 min read - What is the difference between wait() and sleep() in Java?
sleep() pauses the current thread for a time and keeps any lock it holds. wait() must be called inside a synchronized block, releases that lock, and pauses until notify or notifyAll is called or the timeout ends.
IntermediateQuick answer1 min read - When should you use composition instead of inheritance in Java?
Use inheritance for a true is-a relationship and composition for has-a. Composition is usually safer because the classes stay loosely coupled and you can change the part at runtime, while inheritance ties the child tightly to the parent.
IntermediateQuick answer1 min read - Why does a Java stream do nothing until a terminal operation is called?
Intermediate operations like filter and map only describe the pipeline. Work starts when a terminal operation such as collect, count or forEach is called, and elements then flow through the whole pipeline one at a time.
IntermediateQuick answer1 min read - Why does Java not allow a class to extend more than one class?
To avoid the diamond problem, where two parents define the same method and the child cannot tell which one to inherit. Java allows multiple interfaces instead, and since Java 8 a conflicting default method must be resolved explicitly.
IntermediateQuick answer1 min read - Why is a char array recommended over String for storing passwords in Java?
A String is immutable and may stay in memory until garbage collection, so it cannot be wiped. A char array can be overwritten with blanks right after use, which shrinks the time a password sits in memory where a heap dump could expose it.
IntermediateQuick answer1 min read - Why use ExecutorService instead of creating threads with new Thread?
Thread creation is costly and unbounded threads can exhaust memory. An ExecutorService reuses a fixed set of worker threads, queues extra tasks, returns Futures, and offers an orderly shutdown.
IntermediateQuick answer1 min read - Write Java code to find the second highest distinct number in a list.
Sort the distinct values in descending order, skip the first, and take the next, returning an Optional because there may be no second value. The stream version is O(n log n); a single loop tracking two maximums is O(n).
IntermediateQuick answerCoding1 min read - Write Java code to group a list of names by their first letter using streams.
Use Collectors.groupingBy with a classifier that returns the first character, and pass TreeMap::new if you want the keys sorted. It makes one pass, so O(n) time and O(n) space.
IntermediateQuick answerCoding1 min read - Write Java code to sort employees by salary (highest first) and then by name.
Build a Comparator with Comparator.comparingDouble(Employee::salary).reversed().thenComparing(Employee::name) and pass it to list.sort. Sorting costs O(n log n) time.
IntermediateQuick answerCoding1 min read - What are strong, soft, weak and phantom references in Java?
A strong reference prevents collection. A soft reference is cleared only when memory is short, a weak reference is cleared at the next collection once no strong refs remain, and a phantom reference is used to know when an object has been reclaimed.
AdvancedQuick answer1 min read