In this article, we'll share some of the most common Senior Java developer interview questions with answers. This list will grow since we plan to add more questions.

 

1. Q: How can we catch an exception thrown by another Thread?

A: This can be done using Thread.UncaughtExceptionHandler.

2. Q: In which situations will finally block not be executed in Java?

A: There are just a few situations where finally block will not be executed and these are the following:

  • System.exit() is invoked
  • If the underlying Operating System forcibly terminates the JVM process - for example kill -9 <processId> is called
  • Runtime.getRuntime().halt(exitStatus) is invoked
  • If the JVM reaches an infinite loop (or some other non-interruptible, non-terminating statement) in the try or catch block
  • If the host system stop working - power failure, hardware error, etc.
  • JVM crashes before getting to finally block
  • If the finally block is going to be executed by a daemon thread and all other non-daemon threads exit before finally is called

3. Q: What is a "Double Brace" initialization in Java?

A: This type of initialization creates an anonymous class derived from the specified class (the outer braces), and provides an initializer block within that class (the inner braces). For example:

new ArrayList<String>() {{
  add("String 1");
  add("String 2");
}};

4. Q: What is ThreadLocal class and when should we use it?

A: Instance of this class store data for each Thread independently, which means that other Threads can not access specific Thread's data. ThreadLocal instance lasts as associated thread - as soon as the thread is done, ThreadLocal instance is ready to be collected by Garbage Collector.

5. Q: Would you store sensitive data (like social security number, password, etc) into String or char array and why? 

A: Char array. Strings are immutable and are stored in a String pool until the Garbage collector picks them. This means they will stay and wait in memory even after we're done with processing them. This Strings could be retrieved and exploited in case that someone has access to memory dump. Char array is a mutable object and could be set to blank after we're done with processing so it's not stored in memory more than needed.

6. Q: What will the following code return?

public static boolean check() {
  try {
    return true;
  } finally {
      return false;
    }
}

A: False. The return statement in the finally block will override any returns or exception throws in the try/catch blocks.

7. Q: When should we use the volatile keyword?

A: The volatile keyword in Java is used to mark a Java variable as being stored in memory accessed by multiple threads. This means, that every read of a volatile variable will be read from the computer's main memory, and not from the CPU cache and that every time volatile variable is changed the change will be written to main memory, and not just to the CPU cache.

Since Java 5 the features around volatile variables are extended comparing to previous versions where they could be only written to and read from shared memory. 

8. Q: What is Continuous Integration (CI)?

A: The idea of CI is to continuously integrate all changes committed to the repository by team members and to check for errors. 

CI process will use different tools to check the new code's correctness before integration - automated build and tests are executed.  The key goals of continuous integration are to quickly find and address bugs, improve software quality, and reduce the time used for validation and release of software updates.

9. Q: What's the difference between fail-safe and fail-fast operators?

A: The main difference between fail-safe and fail-fast iterators is whether or not the collection can be modified while it's being iterated through - fail-safe iterators allow this behavior while this is not a case with fail-fast iterators.

Fail-safe iterators are working with a clone of collection they're iterating, so they would work just fine even if the collection is modified. Examples would be ConncrurentHashMap and CopyOnWriteArrayList.

Fail-fast iterators will fail as soon as it's notified that collection is being modified during the iteration and ConcurrentModificationException will be thrown. Examples would include ArrayList, HashMap, and HashSet.

10. Q: What is the difference between ApplicationContext and BeanFactory in Spring?

A: The BeanFactory comes with only basic features while ApplicationContext has advanced features that support enterprise application development. ApplicationContext included all functionalities of the BeanFactory container.

BeanFactory provides the basic support for DI (dependency injection) and is defined by the org.springframework.beans.factory.BeanFactory interface. BeanFactory should only be used when memory consumption is critical (e.g. applet-based applications and light applications for mobile devices).

Spring ApplicationContext adds more enterprise-specific functionality such as the ability to publish application events to interested event listeners. This container is defined by the org.springframework.context.ApplicationContext interface.

11. Q: What is a Busy Spinning in Java and when should we use it?

A:  Busy Spinning is a waiting strategy in multithreading where a thread just waits in a loop, without releasing the CPU for going to sleep. This is an advanced waiting strategy where the wait time between two messages should be minimal.

12. Q: What is SQL injection and what are common approaches to protect your code from it?

A: SQL Injection is a kind of attack where the malicious sender sends a value to your API that, when concatenated with the fixed part of the SQL query, change its intended behavior. JPA does not protect us from this kind of attack. Common approaches that exploit-safe are using Parametrized Queries, JPA Criteria API, and filtering data provided by the user which is called data sanitization. Data sanitization filters could be whitelist and blacklist. Whitelist filters define exactly what a user can provide as input data, while blacklist filters define what user should not provide as input.

13. Q: How to pass an object from one thread to another?

A: One approach would be to use java.utils.concurrent.BlockingQueue.

14. Q: What’s the difference between unit, integration, and functional testing?

A: A unit test checks an isolated piece of code, which is usually a method. An integration test tests how your code integrates with other systems, for example, a database or a third-party API. A functional test tests the actual functionality of an application and uses automated tools to carry out and simulate interactions with users.

15. Q: What is a deadlock in Java?

A: Deadlock is a situation where two or more threads are blocked forever, waiting for each other. In this situation, program will hang forever because neither of the threads in position to proceed and waiting for each other to release the lock.