Java interview questions
Language
- What are the differences between C++ and Java?
C++ is not platform-independent; the principle behind C++ programming is “write once, compile anywhere.”
In contrast, because the byte code generated by the Java compiler is platform-independent, it can run on any machine, Java programs are written once and run everywhere.
- Explain JVM, JRE, and JDK.
JDK | JRE |
---|---|
JavaDevelopment Kit | Java Runtime Environment |
JDK is a dedicated kit for solely software development | JRE is a set of software and library designed for executing Java Programs |
Unlike JVM, JDK is Platform Dependent | Unlike JVM, JRE is also Platform Dependent |
JDK package is a set of tools for debugging and Developing | JRE Package is one that only supports files and libraries for a runtime environment |
JDK package will be provided with an installer file | JRE Package does not get an installer but has only a runtime environment |
- What is a ClassLoader?
A classloader in Java is a subsystem of Java Virtual Machine, dedicated to loading class files when a program is executed; ClassLoader is the first to load the executable file.
Java has Bootstrap, Extension, and Application classloaders.
- What are the Memory Allocations available in JavaJava?
Java has five significant types of memory allocations.
Class Memory Heap Memory Stack Memory Program Counter-Memory Native Method Stack Memory
- What are the differences between Heap and Stack Memory in Java?
Stack memory in data structures is the amount of memory allocated to each individual programme. It is a fixed memory space. Heap memory, in contrast, is the portion that was not assigned to the Java code but will be available for use by the Java code when it is required, which is generally during the program's runtime.
- Explain Java String Pool.
A collection of strings in Java's Heap memory is referred to as Java String Pool. In case you try to create a new string object, JVM first checks for the presence of the object in the pool. If available, the same object reference is shared with the variable, else a new object is created.
- What are the differences between Heap and Stack Memory in Java?
Stack memory in data structures is the amount of memory allocated to each individual programme. It is a fixed memory space. Heap memory, in contrast, is the portion that was not assigned to the Java code but will be available for use by the Java code when it is required, which is generally during the program's runtime.
- Which among String or String Buffer should be preferred when there are a lot of updates required to be done in the data?
Because StringBuilder is quicker than StringBuffer, it is advised to utilize it wherever possible. However, StringBuffer objects are the best choice if thread safety is required.
- Can you explain the Java thread lifecycle?
A thread can be in any of the following states in Java. These are the states:
New: A new thread is always in the new state when it is first formed. The function hasn't been run yet, thus it hasn't started to execute for a thread in the new state. Active: A thread switches from the new state to the active state when it calls the start() method. The runnable state and the running state are both contained within the active state. Blocked or Waiting: A thread is either in the blocked state or the waiting state when it is inactive for a while (but not indefinitely). Timed waiting: When we use the sleep () method on a particular thread, we are actually engaging in timed waiting. The thread enters the timed wait state using the sleep () function. The thread awakens when the allotted time has passed and resumes execution where it left off. Termination: A thread that has been terminated means it is no longer active in the system. In other words, the thread is inactive and cannot be revived (made active again after being killed).
- What is a Memory Leak? Discuss some common causes of it.
A memory leak is the slow degradation of system performance over time brought on by the fragmentation of a computer's RAM as a result of shoddy application design or programming that fails to release memory chunks when they are no longer required. These memory leaks frequently result from session items in excess, insertion into Collection objects without deletion, infinite caches, excessive page switching on the operating system, listener methods that are not called, and bespoke data structures that are poorly written.
redis/rabbitmq persistence
nginx / docker / elasticsearch
mysql, transaction (Atomicity, Consistency, Isolation, Durability)
- start transaction/begin
- savepoint
- rollback to
- rollback
- commit
Unit test
Project architecture, general middlewares, system cache
What lifecycles do you separate your project, and on what aspects do you focus on in each cycle
Page Source