10 Java Interview Questions From Investment Banks
Getting ready for an interview at a bank? Here are some Java language questions you should be ready for as well as which topics in general you can expect.
Join the DZone community and get the full member experience.
Join For FreeThere are a lot of Java developers applying for Java development roles at investment banks like Barclays, Credit Suisse, Citibank etc, but many of them have no idea of what kind of questions are expected there.
In this article, I'll share some frequently asked questions from investment banks to Java developers of more than 3 years of experience. Yes, these questions are not for freshers or those with 1-2 years of Java experience — often, banks don't hire at that level via open interviews. Rather, they mostly join as graduate trainees.
It's not guaranteed that you will be asked these questions, but they will give you enough of an idea of what kind of questions you can expect in the real interview.
By the way, the more you prepare, the better your preparation will be. So, if you think 10 is not enough and you need more, then check out these 40 Java questions for the telephone interview and these 200+ questions for more thorough preparation.
Once you have gone through these questions, you should be much more confident when attending any Java interview.
Without any further ado, here is my list of some of the most frequently asked Java questions from investment banks:
Question 1: What’s wrong with using HashMaps in a multi-threaded environment? When does a get() method go into an infinite loop? (answer)
Well, nothing is wrong — it depends on how you use it. For example, if you initialize the HashMap with just one thread and all threads are only reading from it, then it's perfectly fine.
One example of this is a Map that contains configuration properties. The real problem starts when at least one of those threads is updating the HashMap, i.e. adding, changing, or removing any key-value pair.
Since a put() operation can cause re-sizing, which can further lead to infinite loop, that's why either you should use a Hashtable or ConcurrentHashMap (the latter is better).
Question 2. Does not overriding a hashCode() method have any performance implication? (answer)
This is a good question and open to all, as per my knowledge, a poor hashCode function will result in frequent collisions in HashMap, which eventually increases the time for adding an object into said HashMap.
From Java 8 onwards, though, collisions will not impact performance as much as in earlier versions because, after a threshold, the linked list will be replaced by the binary tree, which will give you O(log N) performance in the worst case, as compared to O(N) of a linked list.
This is one of the several tricky Java questions you will face, as many developers only know about equals hashcode contracts and don't think about their performance implications.
Question 3: Do all properties of immutable objects need to be final? (answer)
Not necessarily. As stated above, you can achieve the same functionality by making members non-final but private — and not modifying them except in constructors.
Don't provide a setter method for them, and if it is a mutable object, then don't ever leak any reference to that member.
Remember, making a reference variable final only ensures that it will not be reassigned a different value, but you can still change individual properties of an object pointed by that reference variable.
This is one of the key points an interviewer always likes to hear from candidates. By mentioning that, you can score some brownie points.
Question 4: How does a substring () inside a String work? (answer)
Many developers know the answer: “A substring creates a new object out of the source string by taking a portion of original string.”
But I think that answer is insufficient.
This question was mainly asked to see if the developer is familiar with the risk of memory leaks that substrings can create.
Until Java 1.7, a substring holds the reference of the original character array, which means even a substring 5 characters long can prevent a 1GB character array from garbage collection by holding a strong reference.
This issue is fixed in Java 1.7, where the original character array is not referenced anymore, but that change also made the creation of substrings a bit more costly in terms of time. Earlier, it was in the range of O(1), which could be O(N) in the worst case in Java 7.
Question 5: Can you write critical section code for a singleton? (answer)
This core Java question is a follow-up of the previous question and expects the candidate to write a Java singleton using the Double-Checked Locking Pattern.
Remember to use a volatile variable to make Singleton thread-safe.
Here is the code for a critical section of a thread-safe singleton class using the Double-Checked Locking idiom:
public class Singleton {
private static volatile Singleton _instance;
/**
* Double checked locking code on Singleton
* @return Singelton instance
*/
public static Singleton getInstance() {
if (_instance == null) {
synchronized(Singleton.class) {
if (_instance == null) {
_instance = new Singleton();
}
}
}
return _instance;
}
}
Question 6: How do you handle error conditions while writing a stored procedure or accessing stored procedures from Java? (answer)
This is one of the tough Java interview questions and it's open for all. I have a friend who didn't know the answer, and he didn't mind telling me.
My take is that stored procedure should return an error code if some operation fails, but if the stored procedure itself fails, then catching a SQLException is the only choice.
Question 7 : What is the difference between the Executor.submit() and Executer.execute() methods? (answer)
This Java interview question is from my list of 50 Java multi-threading questions from investment banks for experienced programmers.
It's getting popular day by day because of the huge demand of Java developers with good concurrency skills. Btw, if you are serious about building your concurrency skill then the Java Concurrency in Practice Course by Heinz Kabutz is a good place to start with. It's one the most advanced courses on Java concurrency and also based on the classic book, Java Concurrency in Practice by Brian Goetz and company.
Anyway, the answer to this Java interview question is that the former returns a Future object, which can be used to find the result from the worker thread.
There is a difference when looking at exception handling. If your tasks throw an exception (and if it was submitted while executing) this exception will go to the uncaught exception handler (when you don't have one provided explicitly, the default one will just print the stack trace to System.err).
If you submitted the task while submitting any thrown exception, checked exception or not, that is the part of the task's return status. For a task that was submitted that terminates with an exception, the Future.get() will re-throw this exception, wrapped in an ExecutionException.
Question 8: What is the difference between the Factory and Abstract Factory patterns? (answer)
Compared to the Factory pattern, Abstract Factory provides one more level of Abstraction.
Consider different factories each extended from an Abstract Factory and that are responsible for the creation of different hierarchies of objects based on the type of factory.
For example, AbstractFactory is extended by AutomobileFactory, UserFactory, RoleFactory, etc.
Each individual factory would be responsible for the creation of objects in that genre.
Here is a UML diagram of the Factory and Abstract Factory patterns:
Question 9: What is Singleton? Is it better to make the whole method synchronized, or only the critical section? (answer)
Singleton in Java is a class with just one instance in an entire Java application. For example, java.lang.Runtime is a Singleton class.
Creating a Singleton was tricky prior to Java 5, but once Java 5 introduced Enum, it becames very easy.
Please see my article How to create thread-safe Singletons in Java for more details on writing Singletons using Enums and double-checked locking, which is the purpose of this Java interview question.
Question 10: Can you write code for iterating over HashMaps in Java 4 and Java 5? (answer)
This is a tricky one, but as the answerer shows, he managed to write using while and a for loop. Actually, there are four ways to iterate over any Map in Java.
The method involves using keySet() and iterating over the key, then using a get() method to retrieve values, which is bit expensive.
The second method involves using entrySet() and iterating over them either by using a for each loop or while with the Iterator.hasNext() method.
This one is a better approach because both key and value objects are available to you during iteration and you don't need to call the get() method for retrieving the value, which could give O(N) performance in the case of a huge linked list in one bucket.
This could be slightly better if you are using Java 8 and there is a tree instead of the linked list.
Conclusion
That's all about some of the common Java interview questions from investment banks. If you are going for a Java developer role, then expect a lot of focus on Java concurrency, multithreading, collections, JVM internals, garbage collection, and how to increase the performance of Java applications.
I have not touched all the topics you need to be ready for during Java interviews in this article, but if you are serious about preparation, here are a couple of useful resources to find more structured and topical questions:
- 200+ Java Interview Questions - Guide
- 130+ Core Java Interview Questions for 2 to 5 Years Experienced
- Cracking the Coding Interview - 189 Problems and Solutions
- Java Programming Interview Exposed
All the best for your next job interview. If you have any difficulty in understanding any question or you are looking the answer for any Java questions you have seen in your recent interviews, feel free to share with us here.
I'll try my best to answer those questions here.
Opinions expressed by DZone contributors are their own.
Comments