TeachingBee

Must do Java Interview Questions 5 Years Experience that can be Asked

Java Interview Questions 5 Years Experience

The candidates preparing for interviews with 5+ years of experience specifically as a Java developer need not only a deep understanding of fundamentals of Java but also its design related aspects. Interviewers can ask you questions that could make you confused and may even shake your understanding of knowledge about Java. We will discuss a broad variety of crucial Java topics including multithreading, design patterns, garbage collection etc.

In this article we will look at the most frequently asked java interview questions 5 years experience candidates must prepare. 

11 Java Interview Questions 5 Years Experience People

All these questions have been collected from quite senior developers, which have at least 5 years of experience. They have seen these questions on different rounds of their core Java interviews, including telephonic and face-to-face rounds on different companies, mostly on Investment banks like Barclays, Morgan, RBS, and others.

Java Interview Questions 5 Years Experience Candidates Must Prepare:

1. Write a thread-safe singleton patterns in Java.

// Single-threaded version
class SingletonPattern {
    private Helper helper;

    public Helper getHelper() {
        if (helper == null) {
            helper = new Helper();
        }
        return helper;
    }

    // other functions and members...
}

2. What is the benefit of a multi-catch block in Java?

Multi-catch block is an improvement that came with the Java 7 release. Be ready to answer questions on this critical part of the programming language. “With multi-catch blocks, code is shorter. This makes code easier to create and easier for others to interpret. Also, making exception parameters final is less complex with this format.”

3. What is the importance of the hashCode() and equals() contract?

Consider the scenario of the HashMap object. We know that the Key of the HashMap uses the hashCode() and equals() methods for finding index or finding the value of a given key by uniquely identify the key-value pair in the map. If these methods are not implemented properly, then there are chances where two keys would produce the same output for these methods resulting in inaccuracy and a wrong key’s value might get updated at the time of updation.

Hence, it is very much important to implement the hashCode and the equals method correctly. This can be done properly if we follow the hashCode-equals contract. The contract states that If two objects are equal, then the hashCode method should produce the same result for both objects. To ensure this, we have to override the hashCode() method whenever we override the equals() method.

4. What is Read-Write Lock? Does ConcurrentHashMap in Java Use The ReadWrite Lock? 

ReadWrite Lock is an implementation of a lock stripping technique, where two separate locks are used for reading and write operations. Since the read operation doesn’t modify the state of the object, it’s safe to allow multiple thread access to a shared object for reading without locking, and by splitting one lock into the read and write lock, you can easily do that. 

Java provides an implementation of a read-write lock in the form of the ReentrantReadWritLock class in the java.util.concurrent.lock package. This is worth looking at before you decide to write your own read-write locking implementation. 

Also, the current implementation of java.util.ConcurrentHashMap doesn’t use the ReadWriteLock, instead, it divides the Map into several segments and locks them separately using different locks. This means any given time, only a portion of the ConcurrentHashMap is locked, instead of the whole Map.

5. What is the best possible way to call the wait() method – by using the if construct or the loop construct?

wait() should be called in loop construct always because whenever a thread gets resources to execute again, it is always better to check the condition before execution. The standard way of wait and notify usage is as shown below:

synchronized (resource) {
   while (wait condition)
     resource.wait(); // release the resource lock and reacquire it post wait
   // operations to perform
}

6. Which Design Patterns have You Used in Your Java Project?

Always expect some design patterns-related questions for the Core Java Interview of senior developer position. It’s a better strategy to mention any GOF design pattern rather than Singleton or MVC, which almost every other Java developer uses it. 

Your best bet can be a Decorator pattern or maybe a Dependency Injection Pattern, which is quite popular in the Spring Framework. It’s also good to mention only the design patterns which you have actually used in your project and know its tradeoffs. 

7. Can we use HashMap in a multi-threaded environment?

You can use the HashMap but the probability of working fine depends on the way we use it. For instance, consider the HashMap of configuration properties, if the HashMap initialization was done by using just one thread, and the remaining threads do the task of reading from the map, then HashMap would work perfectly well.

The problem arises when there is at least one thread that updates the Map by means of adding, updating, or deleting the map content. The put() method of the map resizes the map that can cause a deadlock or infinite loop while the threads operate. Hence, during such scenarios, we can use the HashTable or ConcurrentHashMap.

8. How to decide young generation and old generation size for your application?

It depends on nature of application.
If you have lots of temporary objects then there will be lot of minor gc. You can provide arguments XX:NewRatio=1 to distribute 50% to young generation and 50% to old.
By default, NewRatio=2 hence young Generation is 1/3 of total heap.
Similarly, If you have too many long-lived objects, then you might need to increase the size of tenure space by putting high value of NewRatio.

9. Tell me about different Reference types available in Java, e.g. WeakReference, SoftReference, or PhantomReference? and Why should you use them? 

Strong Reference is most simple as we use it in our day to day programming life e.g. in the code, String s = “abc” , reference variable s has strong reference to String object “abc”. Any object which has Strong reference attached to it is not eligible for garbage collection. Obviously these are objects which is needed by Java program. Weak Reference are represented using java.lang.ref.WeakReference class and you can create Weak Reference by using following code :

Counter prime = new Counter(); // prime holds a strong reference - line 2 SoftReference<Counter> soft = new SoftReference<Counter>(prime) ; //soft reference variable has SoftReference to Counter Object created at line 2 prime = null; // now Counter object is eligible for garbage // collection but only be collected when JVM absolutely needs memory

Read more: https://javarevisited.blogspot.com/2014/03/difference-between-weakreference-vs-softreference-phantom-strong-reference-java.html#ixzz7gS5KzHus

Phantom reference is the third kind of reference type available in java.lang.ref package. Phantom reference is represented by java.lang.ref.PhantomReference class. Object which only has Phantom reference pointing them can be collected whenever Garbage Collector likes it. Similar to WeakReference and SoftReference you can create PhantomReference by using the following code :

DigitalCounter digit = new DigitalCounter(); // digit reference variable has strong reference - line 3 PhantomReference<DigitalCounter> phantom = new PhantomReference<DigitalCounter>(digit); // phantom reference to object created at line 3 digit = null;

Read more: https://javarevisited.blogspot.com/2014/03/difference-between-weakreference-vs-softreference-phantom-strong-reference-java.html#ixzz7gS5OhcsB

10. How does ConcurrentHashMap achieve its Scalability? 

Sometimes this multithreading + collection interview question is also asked as, the difference between ConcurrentHashMap and Hashtable in Java. The problem with synchronized HashMap or Hashtable was that the whole Map is locked when a thread performs any operation with Map. 

The java.util.ConcurrentHashMap class solves this problem by using a lock stripping technique, where the whole map is locked at different segments and only a particular segment is locked during the write operation, not the whole map. 

The ConcurrentHashMap also achieves its scalability by allowing lock-free reads as read is a thread-safe operation.

11. What do you know about Factory Design Pattern in Java?

Factory design pattern is the most commonly used pattern in Java. They belong to Creational Patterns because it provides means of creating an object of different instances. Here, the logic of object creation is not exposed to the client. It is implemented by using a standard interface. It gives provision to select object types that we need for creation. This is why, they are also known as Virtual Constructors.

That’s all on this list of Java Interview Questions 5 Years Experience and senior developers. Read about constructor chaining in java here.

90% of Tech Recruiters Judge This In Seconds! 👩‍💻🔍

Don’t let your resume be the weak link. Discover how to make a strong first impression with our free technical resume review!

Related Articles

ascii value in java

Print ASCII Value in Java

In this article we will into the ASCII table in C, exploring its various character sets—from control characters to printable characters, including letters, digits, and special symbols—and demonstrates how to

data hiding in java

Data Hiding In Java

In this article we will discuss data hiding in Java which is an important concept in object-oriented programming. We will cover So let’s get started. What is Data Hiding in

Why Aren’t You Getting Interview Calls? 📞❌

It might just be your resume. Let us pinpoint the problem for free and supercharge your job search. 

Newsletter

Don’t miss out! Subscribe now

Log In