Java Backend Developer Interview Questions (Pt. 1–10)

DN Tech
Geek Culture
Published in
4 min readDec 16, 2021

--

1. What is the role of JVM?
JVM is short for Java Virtual Machine. It provides a runtime environment for Java code to run in. It makes sure that Java is compiled once and runs everywhere regardless of operating systems and processors.

2. How is the compilation process of Java source code?
Java source code undergoes source code development -> lexical analyzer -> syntax analyzer -> sematic analyzer -> bytecode generator.

3. What is the structure of JVM?
JVM consists of mainly three subsystems: Class Loader subsystem, Runtime Data area, and Execution Engine (refer to the diagram below for detailed elaboration).

https://dzone.com/articles/jvm-architecture-explained

4. In JVM Runtime Data Area, what are the components that are shared between multiple threads?
Method area is shared among threads. Heap area stores global variables, object instances, and things that could be accessed anywhere within the application. Therefore, heap area is also a shared space. The stack area which stores the methods, local variables within a thread is not shared as each thread has different methods of calling and variables. PC register tracks the command on how the thread should progress so it is independent for each thread as well. Native method stack is similar to stack area which also contains local variables and methods. So native method stack is not shared across multiple threads.

5. What are the communication methods between threads in Java?
1) Shared memory
volatile keyword, synchronized keyword

2) wait and notify mechanism
wait() and notify() are the methods of Java Object. wait() method makes object convert from running state to blocking state. Once a certain condition fulfills, another thread will call the notify() method to wake up the first thread, and let it enter a runnable state.

3) Lock/Condition mechanism
Lock is class provided by Java to limit access to an object. Condition is invoked by Lock class. One lock can create multiple conditions. Using Condition.await() and Condition.signal() methods, we can put a thread to sleep or wake up a thread to do its work.

6. What are volatile and synchronized keywords?
Volatile keyword targets at a field, or a variable in a method, such that when multiple threads access the same variable, data consistency is ensured. This is done by forcing the variable to be accessed through main memory instead of cache memory.

Synchronized keyword is used for a block of code. It allows only one thread to access the resources at a given point of time. When one thread is manipulating the resources, other threads who want to access the same object are not allowed to execute.

7. What are the differences between volatile and synchronized?
Volatile is a lightweight lock whereas synchronized is a heavyweight lock. After synchronized block is writing/modifying the resource, the variable value is flushed back to the shared memory space. This causes concurrency issue as threads on the same object protected by synchronization can’t execute concurrently. In contrast, volatile allows concurrent executions from multiple threads by forcing them to read the variable from main memory directly instead of CPU cache. Moreover, synchronized is implemented based on operating system which causes the thread to fall into the kernel mode instead of user mode which is a time-consuming process.

8. What is the use of ThreadLocal?
ThreadLocal is a mechanism to ensure thread safety. It allows developer to store variables pertaining to a particular thread so that multiple threads do not need to access shared variables, causing hazard to data consistency.
Each Thread class has a field called threadLocals of type ThreadLocal.ThreadLocalMap. The key for threadLocals is the reference to the current ThreadLocal and the value is the variable developer wants to store (of class type T). To write to or read from the variable, ThreadLocal.get() or set() method are called.

9. What is reflection in Java?
Java is a static language. Reflection in Java gives the program ability to introspect itself, making it more dynamic. It allows developer to examine or modify the behavior of methods, classes or interfaces at runtime.

10. Why is HashMap thread-unsafe?
1) Multiple puts from multiple threads may cause the loss of the element
2) When put and get execute concurrently, the return value for get may be null. This happens when a thread puts an element which exceeds threshold, leading to a rehash operation, a get method will lead to null value.
3) Concurrent put in jdk1.7 may lead to circular linked list which causes infinite loop in get.
To make sure thread-safety, use ConcurrentHashMap instead.

I hope you find this article helpful. If you are like me who’s aspiring to learn something related to technology, or reflect on work and life on a regular basis, follow my channel and stay updated to my inspirations from my daily work and life.

Related:
Java Backend Developer Interview Questions (Pt. 11–20)
Java Backend Developer Interview Questions (Pt. 21–30)
Java Backend Developer Interview Questions (Pt. 31–40)

--

--

DN Tech
Geek Culture

Backend Software Engineer who shares about my daily work bits