Introduction
Java is one of the most widely used programming languages, known for its versatility, reliability, and scalability. Professionals with 5 years of experience in Java are often expected to have a deep understanding of not only the core concepts but also advanced topics like multithreading, memory management, JVM internals, and design patterns. At this stage, interviewers are likely to focus on your ability to solve complex problems, optimize code, and apply design principles effectively.
This blog on Java Interview Question contains some of the tricky Core Java Interview questions for experienced professionals. Variables, collections, polymorphism, strings, data types, and threads are some of the critical Java topics we'll cover for java interview questions.
Let's get started with our Java Interview Questions for 5 years Experience in 2024.
Java Interview Questions for Experienced
1. What's the difference between path and classpath variables?
Classpath is used to locate class files in Java executables, whereas path is used to locate executables in an operating system.
2. Is it possible to make an array volatile in Java?
Yes, an array can be made volatile in Java, but only the reference to the array, not the entire array. If one thread changes the reference variable to point to another array, the volatile guarantee will be provided. Still, if multiple threads change individual array elements, the volatile warranty will not be provided.
3. Is it possible to convert a non-atomic operation to an atomic one using volatile?
To make non-atomic 64-bit operations such as long and double atomic, the volatile keyword is used. (Atomicity is already guaranteed for all other primitive accesses).
4. What are some of the applications of volatile modifiers?
Volatile modifiers in Java have practical uses, such as ensuring atomic reads for double and long types, providing memory barriers, and guaranteeing visibility of written values across threads. The Java Memory Model inserts write and read barriers for volatile variables, allowing consistent and synchronized access to their values. This ensures that changes made to a volatile variable are visible to other threads, and any preceding actions are also guaranteed to have occurred.
5. Which is the most straightforward to write? 10 or 2 threads synchronization code?
Because synchronization code is independent of several threads, both will be of equal complexity in code writing. Because the number of lines causes more contention, advanced synchronization techniques such as lock stripping, which require more complex code and expertise, are used.
6. What are some of the most common Java classes?
A Java Class is a grouping of Java objects that share common properties or methods. In object-oriented languages, it's commonly used as a design blueprint. Static, final, concrete, inner, abstract, and POJO are the most frequently used Java classes in app development.
7. What are the four Java access modifiers, and how do you use them?
Default, public, private, and protected are the four access modifiers in Java. They're used to figure out who has access to keywords or information in a class.
8. What are the most important concepts to know about wrapper classes?
The eight primitive types of wrapper classes and where they are best used are the most important concepts to grasp in wrapper classes. It's important to remember that once a wrapper class is assigned, it's final and unchangeable.
9. In Java, what is a final class?
This flag indicates that a subclass cannot extend or override a class. Declaring a class final prevents inheritance and ensures the security of data.
10. In terms of multithreading, what is false sharing?
False sharing is a well-known performance issue on multi-core systems with local caches. When threads on different processors modify variables on the same cache line, this is known as false sharing.
Because the thread may be accessing completely different global variables relatively close together in memory, false sharing is difficult to detect. The primary way to avoid false sharing, as with many concurrency issues, is to review your code carefully and align your data structure with the size of a cache line.
11. Could you describe platform independence?
When you create a program that runs on any operating system, you refer to platform independence. It facilitates the use and marketing of products. This is why I believe Java is more advanced than other programming languages.
12. How do you explain the difference between JDK and JVM?
The Java Development Kit (JDK) and the Java Virtual Machine (JVM) are the two components that allow Java to run on a device. The apps we make with JDK's tools wouldn't run on people's phones or tablets if they didn't have JVM.
13. Explain the method of overloading Java.
If the parameters of two or more methods in Java differ, they may have the same name (different number of parameters, different types of parameters, or both). Overloaded methods are what this feature is known as.
14. In Java, explain what a catch block is.
The Java catch block handles the exception by declaring the type of exception within the parameter. The parent class exception (Exception) or the generated exception type must be reported. However, stating the generated exception type is a good strategy.
15. What strategies can we employ to ensure thread safety?
In shared-memory concurrency, there are four ways to make variable access safe:
- Confinement: Keep the variable confined to a single thread by avoiding sharing it among multiple threads. This approach eliminates the need for synchronization since each thread operates on its own copy of the variable.
- Immutability: Make the shared data immutable, meaning it cannot be modified once created. Immutable objects can be safely accessed and shared across threads without the risk of data inconsistency or race conditions.
- Thread-Safe Data Types: Encapsulate shared data in thread-safe data types provided by the programming language or libraries. These data types are designed to handle concurrent access properly and ensure thread safety. Examples include thread-safe collections or concurrent data structures.
- Synchronization: Use synchronization mechanisms, such as locks or synchronized blocks, to control access to shared resources. By acquiring and releasing locks, you can ensure that only one thread can access the critical section of code at a time, preventing concurrent access issues and maintaining data integrity.
16. What do you think the reason for the string's immutability is?
Immutable string is threadsafe. If a string were not immutable, changing one reference would change the value of others.
17. Explain how to call the run() method of the Thread class.
If the thread was created using a separate Runnable object, the run() method of the Thread class is called; otherwise, this method does nothing and returns. The code specified in the run() method is executed when the run() method is called. The run() method can be used multiple times.
The start() method or the run() method can both be used to call the run() method. However, using the run() method to call itself causes issues.
18. What does context switching imply?
A context switching in computing stores the state of a process or thread so that it can be restored and executed later at a later time. This is an essential feature of a multitasking operating system because it allows multiple processes to share a single central processing unit (CPU).
19. How is the Java Thread Dump of a program located?
Break + Ctrl (Windows): The CTRL and Break key combination in Windows operating systems can be used to capture a thread dump. Go to the console where the Java application was launched and press the CTRL and Break keys simultaneously to take a thread dump.
20. Describe the multithreading process in Java.
Multithreading is a Java feature that allows executing two or more parts of a program simultaneously to maximize CPU utilization. A thread is a component of such a program. Threads are, therefore, lightweight processes within processes.
Threads can be made using two different mechanisms:
1. Extending the Thread class
2. Implementing the Runnable Interface
21. What is an interface, exactly?
Because it's a reference type that defines methods, an interface is similar to a class. It differs from a class in that it doesn't implement any methods.
22. What distinguishes an interface from an abstract class?
A few distinctions exist between an interface and an abstract class. The most notable difference is that abstract classes can implement methods while interfaces cannot. Abstract classes can also implement interfaces.
23. What is the difference between the observer and observable classes?
Observers are managed by objects that inherit the "Observable class."
When an Observable object is upgraded, it calls each observer's update() method.
It then informs all observers that there has been a change of state.
Objects that observe Observable objects implement the Observer interface.
24. What is Java Session Management?
A session is defined as the dynamic state of a random conversation between the client and the server. Both sides send and receive responses and requests through the virtual communication channel. A popular method of implementing session management is to create a session ID in the client's communicative discourse with the server.
25. Explain the various authentication methods available in Java Servlets.
Servlets have the following authentication options: In a servlet, there are four different authentication options:
1. Basic Authentication: The client authenticates the user by providing usernames and passwords.
2. Form-based authentication: In this case, the programmer creates the login form using HTML.
3. Digest authentication: Basic authentication is similar to digest authentication, but passwords are encrypted using the Hash formula. The digest is more secure than the Hash Formula.
4. Client certificate authentication: Each client accessing the resource must have a certificate that it sends to verify its identity. The SSL protocol is required for client authentication.
26. Explain the FailFast and FailSafe iterators and provide examples for both.
In Java Collections, FailFast and FailSafe iterators are used.
FailFast iterators do not allow changes or modifications to Java Collections, so they will fail if the most recent element is added to the collection or an existing element is removed. FailFast iterators frequently fail and throw a ConcurrentModificationException exception.
ArrayList, HashMap, etc., are examples of FailFast.
FailSafe iterators, on the other hand, allow changes or modifications to be made to Java Collections. Because the FailSafe iterators usually operate on the cloned copy of the collection, this is possible. As a result, they do not make any notable exceptions. CopyOnWriteArrayList, for example.
27. What's the deal with the Daemon Thread?
The Daemon thread is the thread that has the lowest priority. This Daemon thread is intended to run in the background while Java performs garbage collection.
In Java, the setDaemon() method creates a Daemon thread.
28. In Java, define the term enumeration.
Enumeration, or enum, is a Java interface. Enum allows for sequential access to elements stored in a collection in Java.
29. What makes Java dynamic?
Java is built to adapt to changing conditions. Java programs contain a lot of runtime information used to resolve object access in real-time.
30. Is it possible to run some code before calling the primary method?
Yes, we can run any code before calling the primary method. When creating the objects at the class's load time, we'll use a static code block. Any statements in this static code block will be executed simultaneously as the class is loaded, even before the primary method creates objects.
31. How many times does the finalise method get invoked?
The Garbage Collector is the name of the finalized method. The Garbage Collector only calls the finalize() method once for each object.
Also see, System Design Interview Questions
32. What are the key differences between the final, finally, and finalize keywords in Java?
The final keyword is used to declare a variable, method, or class as unchangeable. finally is a block used in exception handling to ensure a piece of code is executed regardless of exceptions. finalize is a method that the garbage collector calls before reclaiming an object's memory.
33. Explain the concept of multithreading in Java and how it can be achieved.
Multithreading is the ability of a program to execute multiple threads concurrently. In Java, it can be achieved by extending the Thread class or implementing the Runnable interface and using the Thread class to create and start new threads.
34. What is the difference between the equals() and == methods in Java?
The equals() method compares the content or values of two objects for equality, typically overridden for custom comparison. The == operator checks if two objects refer to the same memory location, testing for reference equality.
35. What is the purpose of the transient keyword in Java?
The transient keyword is used to indicate that a variable should not be serialized during object serialization. It allows skipping certain fields during the serialization process.
36. Can you explain the concept of Java Generics?
Java Generics provide a way to create classes, interfaces, and methods that can work with any type. It enables type safety and avoids the need for explicit type casting.
37. What is the difference between the throw and throws keywords in Java?
The throw keyword is used to explicitly throw an exception within a method. The throws keyword is used in method declarations to indicate that the method may throw one or more specific exceptions and that the caller must handle or propagate them.
38. Explain the difference between ArrayList and LinkedList in Java.
ArrayList is an array-backed dynamic list that provides fast random access but slower insertion and deletion of elements. LinkedList is a doubly linked list where elements are linked with references and allows efficient insertion and deletion, but random access is slower.
39. What is the purpose of the volatile keyword in Java?
The volatile keyword is used to declare a variable as being stored in the main memory instead of the CPU cache. It ensures that changes made to the variable are visible to all threads, avoiding thread synchronization issues.
40. What is the role of the ClassLoader in Java?
The ClassLoader is responsible for loading Java classes into memory during runtime. It dynamically locates and loads classes as they are referenced in the program, providing a way to extend the functionality of Java applications at runtime.