Frequently asked Java interview questions for experienced- Part IV

Frequently asked Java interview questions for experienced – 2 to 5 years

This is a continuation of my previous posts on frequently asked Java interview questions for experienced Java developers. Let see some of the important question and answers.

1. What is Generics in Java?

This is one of the most important Java interview questions. Especially those who have higher experience in Java should have thorough knowledge on Generics. Refer my previous post about Generics in Java.

2. Can we add an element while iterating over a list?

We should not modify collection’s structure once an iterator is defined. We can remove an element from the underlying collection with the iterator’s remove () method after a call to the next () method. This method can be called only once per call to next (). The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method. It will throw java.util.ConcurrentModificationException if we try to add element while the iteration is in progress.

Throws IllegalStateException – if the next method has not yet been called and the remove method is called or the remove method has already been called after the last call to the next method.

3. What is the difference between an application server and a web server?

A web server typically contains a servlet container(web container) to handle HTTP requests whereas an application server have servlet container as integral part of them and serves business logic through any protocol, including, but not limited, to HTTP.  That means an Application Server can do whatever Web Server is capable of. In addition an Application Server also has a EJB container, supports distributed transaction and provides application level services like  Connection pooling , Transaction management, load balancing, Messaging etc. while Web Server only supports Servlets and JSP.

E.g. Apache Tomcat is a web server and JBoss is an application server.

4. Which Collection can be used for holding key value pair in the insertion order?

LinkedHashMap can be used for holding key-value pair in the insertion order.

5. Why String is Immutable in Java?

Below are the key factors why String is made final or immutable.

  • Optimization and performance
  • Security
  • Multithreading Benefits

Read more about each factor  – 5 Reasons why string is immutable.

6. Difference between wait and sleep method in Java.

wait()sleep()
wait() is a method from Object classsleep() is static method of Thread class
wait() method releases the lock when thread is waiting.sleep() doesn’t release any lock while waiting.
wait method should be called from synchronized block or method else we will get IllegalMonitorStateExceptionThere is no such requirement and sleep can be called from anywhere
To wake a thread from wait(), you have to call notify() or notifyAll() methodWhile in sleep (), thread gets start after the specified duration

7. Difference between notify and notifyAll in Java?

notify method will wake up  or notify only one thread and notifyall will notify all threads. If you are sure that more than one thread is waiting on monitor and you want all of them to give equal chance to compete for CPU, use notifyAll method. If multiple threads are waiting on a lock and if you call notify then it is not guaranteed that which thread will be informed.

8. Can we override private method in Java?

No, we cannot override private methods in Java. When we declare a method as private, it will be visible only for that class and not even to sub class. Hence overriding is not applicable in sub class.

9.What is the difference between using == operator and equals() method?

In case of primitive values, the == operator compares the values whereas in case of an object == compares the address of the objects and returns true if both references are pointing to the same object in the heap. equals() method on the other hand is used for logical comparison and its expected from an object to override this method to provide logical equality. For example, String class overrides the equals() method to return true if two Strings contains same letters.

10. The difference between Serializable and Externalizable interface in Java?

This is one of the frequently asked questions from Java Serialization.

Serializable is a marker interface(interface with no methods) is used to make Java classes serializable so that they can be transferred over network or their state can be saved on disk, but it uses default serialization built-in JVM. Externalizable interface contains two methods writeExternal() and readExternal() and allows you to fully control the Serialization process, specify a custom binary format and add more security measure and in terms of performance its good because everything is under control.

11. What do you mean by synchronization and what is the level of synchronization that can be applied?

Synchronization is nothing but control of access to a shared resource among multiple threads. Without synchronization when multiple thread try to access a shared resource there may arise unforeseen result due to concurrency issue. For example if multiple threads try to write within a same file then the file may be corrupted because one of the threads can overwrite data of other or while one thread is opening the file, at the same time another thread might be closing the file. So there is a need to control access to a shared resource such that only one thread can operate at a given time. Synchronization in java is achieved using synchronized keyword.

Synchronization can be applied to methods and blocks in a class.

Synchronized Method Example:

If you remove the synchronized keyword from the print method and run the program, the output will be different in different execution. Below is the output that is obtained in one of the executions.

It is evident from the above output that, without synchronization, both the threads access the print() method simultaneously.

Synchronized Block Example:

The level of synchronization in the above example can also be achieved by using synchronized block. A synchronized block is nothing but a group of statements within curly braces with a synchronized keyword.

When you run the above program you will get the same output as that of synchronized method example. Here synchronization is achieved by obtaining a lock on the display object d. So at a particular point of time only one thread can access methods of the object d. So when the program is run, Thread-1 gets a lock on object d and Thread-2 has to wait until Thread-1 finishes.

Hope you find my series of post on “Frequently asked Java interview questions for experienced developers” useful. If you have any comments, post it in the comments section.

Best of Luck!!!

The following two tabs change content below.
Working as a Java developer since 2010. Passionate about programming in Java. I am a part time blogger.

Add Comment

Required fields are marked *. Your email address will not be published.