Top 20 Java Interview Questions for Investment Banking (Answers Included)
This article compiles 20 common Java interview questions asked by investment banks, covering topics such as multithreaded HashMap usage, hashCode/equals contracts, String substring memory issues, singleton patterns, design patterns, deadlock avoidance, and performance considerations, each accompanied by concise answers and code examples.
Java Interview Questions for Investment Banking
Question 1: Using HashMap in a multithreaded environment, when can get() cause an infinite loop?
Answer: Generally no problem if the map is only read after being populated in a single thread. Issues arise when at least one thread modifies the map (put, update, remove) because resizing can cause loops; use Hashtable or ConcurrentHashMap instead.
Question 2: What happens if you do not override hashCode() ?
Answer: A poor hashCode implementation leads to frequent collisions in a HashMap, increasing insertion time. Since Java 8, collisions are mitigated by converting buckets to balanced trees, reducing worst‑case from O(n) to O(log n).
Question 3: Should all immutable fields be declared final ?
Answer: Not required. You can achieve immutability by keeping fields private and only assigning them in the constructor, without providing setters. Declaring a reference final only prevents reassignment, not mutation of the referenced object.
Question 4: How is String.substring() implemented and what memory risk does it have?
Answer: substring creates a new String that originally shared the original character array. In Java 1.7 this caused potential memory leaks because a small substring could keep a large array alive. From Java 7 onward the implementation copies the needed characters, changing the time complexity from O(1) to O(n).
Question 5: Write the critical‑section code for a singleton (answer).
Answer: The interview expects a double‑checked locking implementation using a volatile instance field.
public class Singleton {
private static volatile Singleton _instance;
/** Double‑checked locking */
public static Singleton getInstance() {
if (_instance == null) {
synchronized (Singleton.class) {
if (_instance == null) {
_instance = new Singleton();
}
}
}
return _instance;
}
}Knowing common patterns such as Singleton, Factory, and Decorator is also valuable.
Question 6: How to handle errors when calling stored procedures in Java?
Answer: Stored procedures should return error codes on business errors. If the procedure itself fails, catch SQLException. Refer to *Effective Java* for best practices.
Question 7: Difference between Executor.submit() and Executor.execute() ?
Answer: submit() returns a Future that can retrieve the task result or exception. execute() does not return a result; any exception is printed to System.err unless explicitly handled.
Question 8: Difference between Factory and Abstract Factory patterns?
Answer: Abstract Factory provides a higher‑level abstraction with multiple related factories (e.g., AutomobileFactory, UserFactory) that share a common AbstractFactory. Each concrete factory creates a family of related objects.
Question 9: Which is better for a singleton: synchronizing the whole method or only the critical section?
Answer: Synchronize only the critical section. Locking the entire method forces every call to wait, even when the instance is already created, leading to 10‑20× slower performance compared to synchronizing just the creation block.
Question 10: How to iterate a HashMap in Java 4/5?
Answer: Four common ways: keySet() with get(), entrySet() with enhanced for, Iterator over entrySet(), and using forEach (Java 8+). Iterating via entrySet() is usually most efficient because both key and value are available without extra lookups.
Question 11: When should you override hashCode() and equals() ?
Answer: Whenever logical equality is needed, especially when objects are used as keys in HashMap or elements in a Set. Both methods must be overridden together to satisfy the hash‑code contract.
Question 12: Problems that arise when overriding hashCode() without equals() ?
Answer: The contract breaks: two objects considered equal by equals() may have different hash codes, causing incorrect behavior in hash‑based collections (duplicate keys, lost entries).
Question 13: Difference between synchronizing the whole getInstance() method and only the critical section?
Answer: Synchronizing only the critical section reduces contention; the whole‑method lock forces every call to wait even after the singleton is created.
Question 14: When do equals() and hashCode() get invoked during a HashMap.get() ?
Answer: hashCode() is called to locate the bucket. Within the bucket, equals() is used to compare the provided key with existing entries to find the correct value.
Question 15: How to avoid deadlocks in Java?
Answer: Deadlock requires four conditions: mutual exclusion, hold‑and‑wait, no preemption, and circular wait. Break any condition, e.g., acquire locks in a consistent order, use time‑outs, or interrupt waiting threads.
Question 16: Difference between string literals and new String() ?
Answer: Literals are interned in the String constant pool; new String() creates a distinct object on the heap. Use String.intern() to add a heap‑created string to the pool.
Question 17: What is an immutable object and how to create one?
Answer: An immutable object cannot change after construction (e.g., String). Typically declare the class final, make all fields private final, set them only in the constructor, and avoid exposing mutable internal references.
Question 18: Simple way to measure method execution time without profiling tools?
Answer: Record System.nanoTime() (or currentTimeMillis()) before and after the method, compute the difference, and repeat many times if the duration is too short.
Question 19: Which two methods must be implemented when using Object as a HashMap key?
Answer: hashCode() and equals() must be overridden to ensure correct key behavior.
Question 20: How to prevent clients from directly instantiating concrete cache classes?
Answer: Make constructors private and provide factory methods or use dependency injection frameworks to control object creation.
Now you are ready for Java interviews
This collection of questions and answers helps you prepare for technical interviews at investment banks, covering data structures, algorithms, concurrency, and design patterns.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
