Java Backend Developer Interview Questions(Pt. 11–20)

DN Tech
3 min readDec 17, 2021

--

11. What are the differences between thread and process?
Thread is a small unit within a process. A process can have multiple threads. A process is an executing program but a thread is a part of the program. Each process has its own address space which multiple threads within that process share. A process is a heavyweight task but a thread is a lightweight task.

12. Explain what is ReentrantLock?
ReentrantLock is a lock where a process can claim the lock multiple times without blocking on itself. If a lock is non-reentrant, when you grab it the second time, you will be blocked by yourself which effectively causes a deadlock.

13. Explain what is ThreadPool and how does it benefit an application?
Creating and starting a thread can be an expensive process in JVM. By repeating this process every time we need to execute a task, we’re incurring a significant performance cost. A thread pool in Java encompasses a number of threads that are executed when needed and returned to the pool after execution for reuse later. A threadpool consists of the pool of worker threads, a thread factory producing new threads, and a queue of tasks to be executed. A threadpool helps to improve the performance of the application.

14. What is the process of loading a Java class?
1) Load the bytecode file
2) Verify whether the bytecode is machine safe
3) Prepare memory for local variables, initial values in the method area
4) Resolve the symbol references in the constant pool to direct references
5) Initialize the class by executing the class source code

15. What are the differences between Array and ArrayList in Java?
Array only contains the same type of value whereas ArrayList can contain different types of values. Array size must be specified in the declaration whereas ArrayList size can be dynamically changed. Array can contain primitive data types such as int, byte, char, but ArrayList can only contain objects.

16. How many bytes does an object take in Java?
The minimum object size is 16 bytes for modern 64-bit JDK since the object has a 12-byte header, padded to a multiple of 8 bytes.

17. What are the differences between optimistic and pessimistic locks?
Optimistic lock implements CAS algorithm (Compare-And-Swap). Before it updates the new value to the target resource, it will check if the target value matches the expected value it retrieved earlier. If match, no threads have modified it and it proceeds to change the target value to the new value. If not, it will read the target value again and perform CAS until finishing with the update.

Pessimistic lock means the thread will lock the resource for exclusive use such that other threads are unable to access it at the same time. Synchronized keyword is a pessimistic lock. Compared to optimistic lock, the level of concurrency is reduced.

18. Is optimistic lock always better?
Depends on the situation. If multiple threads are waiting to fetch the state of the object, it will cause high occupancy on the CPU. CAS algorithm has another pitfall where when the target value changes from “A” to “B” then to “A”, the lock will treat it as no change. One solution is to use version control on the target value.

19. What are wrapper classes in Java?
Wrapper classes change primitive data types such as int, char, double to object classes Integer, Character, Double.

20. What are the overloading and overriding?
Overloading means multiple methods have the same name but the number of arguments is different or the types of arguments are different. It is a compile-time polymorphism. Overriding occurs when a subclass inherits from the parent class, it would like to change the behavior of one of the parent methods but persists the number and type of arguments and return type. It is a run-time polymorphism.

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. 1–10)
Java Backend Developer Interview Questions (Pt. 21–30)
Java Backend Developer Interview Questions (Pt. 31–40)

--

--

DN Tech

Backend Software Engineer who shares about my daily work bits