Frequently asked Java interview questions – Part III
1. What data structure would you recommend for ordered/sorted data?
TreeSet data structure can be used for ordered data.
2. What are the ways to create a Thread in Java?
Threadmust provide the code that will run in that thread. There are two different ways to specify which code to run in that Thread:
- Implement the java.lang.Runnable interface and pass an instance of the class implementing it to the Thread constructor.
- Extend the Thread class itself and override its run() method.
3. Difference between HashMap, LinkedHashMap and TreeMap.
Difference between any two structure will be asked. It is important that you understand all the three structures.
|Hash table based implementation of the Map interface||A Red-Black tree based NavigableMap implementation.||Hash table and linked list implementation of the Map interface, with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries.|
|Implements Map interface||Implements Map, NavigableMap,|
|Implements Map interface|
|This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.||The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used||The linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order).|
|Null key/values Allowed||null values are allowed||Null key/values Allowed|
4. What is a daemon thread?
Daemon threads are non-user threads. They are typically used to carry out low-priority tasks that should not take priority over the main task of the program. They can be used to do useful work when all other user threads are blocked. The garbage collector is one example of a daemon thread. In one line, main difference between daemon thread and user thread is that as soon as all user thread finish execution java program or JVM terminates itself, JVM doesn’t wait for daemon thread to finish their execution. As soon as last non daemon thread finished, JVM terminates no matter how many Daemon thread exists or running inside JVM.
5. Difference between String, StringBuffer and StringBuilder. Which performs better?
|String objects are immutable in Java||Mutuble object||Mutuable|
|Because String is final it can be safely shared between multiple threads without any extra synchronization.||StringBuffer is synchronized i.e. Thread safe||StringBuilder is not synchronized.|
|Operation is Fast||Slow||Fast|
|You can use “+” for concatenating two string because “+” operation is internally implemented using either StringBuffer or StringBuilder in Java.||You can convert a StringBuffer into String by its toString() method.||You can convert a StringBuilder into String by its toString() method.|
|Use String if you require immutability.||Use Stringbuffer in java if you need mutable + thread-safety||Use StringBuilder in java if you need mutable without thread-safety|
6. What is a marker interface?
Marker interface in Java is interface with no field or methods or in simple word empty interface in java is called marker interface. Example of market interface is Serializable, Cloneable and Remote interface. Looking carefully on marker interface in Java e.g. Serializable, Clonnable and Remote it looks they are used to indicate something to compiler or JVM. So if JVM sees a Class is Serializable it does some special operation on it, similar way if JVM sees one Class is implementing Cloneable it performs some operation to support cloning. Same is true for RMI and Remote interface. In summary marker interface in Java is used to indicate something to compiler, JVM or any other tool but Annotation is better way of doing same thing.
7. Write a program to check if the given number is prime or not (or) write a program to display Prime numbers from 1 to n.
Both the programs are simple. It is important to remember the logic at the time of interview.
Refer my previous post for answer.
8. Explain final, finally and finalize in Java.
|Final is a keyword in Java used to apply restrictions on variable, method and Class. If you make a variable final, you cannot change its value; it will act like a constant. If you make a method final in Java, you cannot override it in sub class. If you make a class final, it cannot be sub classed. Making a class final automatically makes all its method final.||finally is a block used in exception handling along with try and catch|
In Java, for exception handling you at least need either catch or finally block. finally block is guaranteed to be executed whether Exception is thrown or not, this makes it, an ideal place to close system resources e.g. InputStream or OutputStream etc. Closing streams, network connection, database connection in finally block is good coding practice in Java.
|finalize() is a method called by Garbage collection thread just before collecting eligible Objects. This is the last chance for object to perform any cleanup but since it is not guaranteed that whether finalize() will be called, its bad practice to keep resource till finalize call.|
9. How do you make an object Serializable? (or) What is a Serializable interface?
To make an object serializable, the class must implement the Serializable interface.
Serializable is a marker interface(no methods or fields) which defines that the object’s state can be serialized or deserialized. Classes that do not implement this interface will not have any of their state serialized or deserialized. All subtypes of a serializable class are themselves serializable.
10. Explain about Static keyword in Java.
Refer my another post for detailed explanation.
11. Can we declare a class as private?
A top level class cannot be declared as private. Only public, abstract & final modifiers are permitted for a top level class. However an inner class can be declared as private.
Hope you find these questions useful. We will discuss more questions in the upcoming posts. If you have any doubts 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