Frequently asked interview questions in Java with answers
Earlier I have written a series of post on frequently asked Java interview questions. In this post we will see some more frequently asked interview questions in Java with answers.
1. Difference between method overloading and method overriding.
This questions is asked in most of the interviews, even for the freshers. Read this post for a detailed answer.
2. What do you mean by platform independence of Java or what do you mean by write once and run anywhere?
This is a basic question. Platform independence means that you can run the same Java program in any operating system without any change in the code. For example you can write a Java program in windows OS, compile it into .class file(byte code) and the generated byte code can be run in any OS(e.g. MAC, linux etc). That is why we call it as write once and run anywhere. You only need a JVM for the target OS to run the code. Here you must note that the Java program is platform independent whereas JVM is not.
3. Why Java doesn’t support multiple inheritance?
Multiple inheritance is nothing but a class extending two or more classes. Multiple inheritance is supported in languages like C++ but not in Java. Java doesn’t support multiple inheritance in classes because it can lead to diamond problem. So rather than providing some complex way to solve it, there are better ways through which we can achieve the same result as multiple inheritance.
Multiple inheritance is supported in interfaces. An interface can extend multiple interfaces because they just declare the methods and implementation will be present in the implementing class. So there is no issue of diamond problem with interfaces.
4. What is the importance of main method and why it is declared as public static void in Java?
The main() method is the entry point of any standalone java application. The syntax of main method is public static void main(String args). The main method is declared as public because it will be called from outside the class (by JVM). It is declared static so that the JVM can access it without creating an object of the class. The main method in Java is not supposed to return any value, hence it is made void which simply means main is not returning anything.
5. Can we overload main method?
Yes, we can overload main method by having multiple methods with name “main” in a single class. However when we run the class, java run time environment will look only for main method with syntax as public static void main(String args).
6. How to sort a collection of custom Objects in Java?
To sort a collection of custom objects, we need to implement Comparable / Comparator interface. Comparable interface has compareTo(T obj) method which can be overridden to provide default way to sort collection of custom objects.
However, if you want to sort based on multiple criteria, such as sorting an Employees collection based on salary or age etc, then we can implement Comparator interface and pass it as sorting methodology. For more details read Java Comparable and Comparator.
7. What is nested class in Java?
The Java programming language allows you to define a class within another class. Such a class is called a nested class. Nested classes are divided into two types – Non-static nested classes which are also called as inner class and Static nested classes.
Nested Classes is a way of logically grouping classes that are only used in one place. It increases encapsulation and can lead to more readable and maintainable code.
8. Difference between Iterator and Enumeration. Both are interface or class?
Both Iterator and Enumeration are interfaces in Java. Iterators differ from enumerations in two ways:
- Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics.
- Method names have been improved.
The bottom line is, both Enumeration and Iterator will give successive elements, but Iterator is improved in such a way so the method names are shorter, and has an additional remove method. Here is a side-by-side comparison:
As also mentioned in the Java API Specifications, for newer programs, Iterator should be preferred over Enumeration, as “Iterator takes the place of Enumeration in the Java collections framework.”
The iterators returned by some of the collection’s iterator and listIterator methods are fail-fast: if the collection (eg.vector) is structurally modified at any time after the iterator is created, in any way except through the iterator’s own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.
The Enumerations returned by the elements method are not fail-safe. Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification.
9. Can we call run() method of a Thread class?
Yes, we can call run() method of a Thread class but then it will behave like a normal method and will be executed in the current thread. To actually execute it in a new Thread, we need to start it using Thread.start() method.
10. Why thread communication methods wait(), notify() and notifyAll() are in Object class?
In Java, wait and notify methods acts as synchronization utility and are essential methods for inter thread communication. Hence these methods are defined in Object class so that every object will have access to it. Also every Object has a monitor and Locks are made available on per Object basis. This is another reason wait and notify is declared in Object class rather then Thread class.
11. What is Deadlock? How to analyze and avoid deadlock situation?
In concurrent programming, deadlock is a situation where two or more threads each waiting for others to release lock and are blocked forever. To analyze a deadlock, we need to look at the java heap dump of the application, and look out for the threads with state as BLOCKED and then the resources it is waiting to lock. Every resource has a unique ID using which we can find which thread is already holding the lock on the object.
Below are the some of the tips to avoid deadlock situation,
- Avoid Nested Locks, Lock Only What is Required and Avoid waiting indefinitely.
- Don’t hold several locks at once. If you do, always acquire the locks in the same order.
Hope you find these frequently asked interview questions in Java useful. Thanks for reading. If you have comments, post it in the comments section.
Latest posts by Uday (see all)
- How to run shell commands using SSH connection in Java - July 26, 2017
- Java program to print rectangle of stars - July 20, 2017
- Introduction to Java Enum data type with example programs - March 31, 2017