Java Interview Questions – II

This is in continuation of my first post HERE about the java interview questions I have come across.

1. What are Threads in JAVA and in what ways we can create them?

A. A Thread is defined as an independent path of execution in a program. A program in java can have many threads running concurrently, either synchronously or asynchronously. The threads are defined by java.lang.Thread class. There are two ways of creating a

a. Extend Thread class : In this method, the user defined class extends ‘Thread’ class and override ‘run()’ method in it.The ‘start()’ method is inherited from the Thread class and is invoked on the object of the user defined class to make the thread start executing.

b. Implement Runnable Interface: In this method, the user defined class implements the ‘Runnable’ interface and implement ‘run()’ method in it.  An object of the ‘Thread’ class is created by passing a Runnable object as an argument to the Thread constructor. The Thread object will then have a Runnable object that implements ‘run()’ method. The ‘start()’ method return immediately after the thread has been initiated and started and the thread ends its lifetime with the end of ‘run()’ method.

When creating threads, there are two reasons why the method (b) defined above may be preferable to method (a).

– Extending the ‘Thread’ class means that the subclass cannot extend any other class, whereas a class implementing the Runnable interface has that option available to it.

– It will be an overhead to inherit all the ‘Thread’ class functionalities in a class that only wants to be ‘Runnable’.


2. What is the difference between a Thread and a Process ?

A. A thread of execution is a lightweight process and is an individual process that has its own call stack. In JAVA, there is one thread per call stack.

  • Threads share the address space of the process that created them whereas processes have their own address space.
  • Threads have direct access to the data segment of its process; Processes have their own copy of the data segment of their parent process.
  • Threads can directly communicate with the other threads of same process; Processes must user Inter Process Communication(IPC) to communicate with sibling processes.
  • Changes in main thread may affect the behaviour of other threads of same process; Changes to parent process does not affect child processes.
  • Threads can exercise considerable control over threads of same process; Processes can only exercise control over child processes.
  • New threads are easily created whereas new process requires duplication of parent process resources.
  • Threads have no overhead, processes have considerable overhead.

3. How can objects of a user-defined class be sorted ? or Use of ‘Comparator’ and ‘Comparable’?

A.  Comparator and Comparable are interfaces that are very useful for implementing sorting for user-defined defined data types in Java. These are often used to sort a Collection of objects stored in any Collection class like an ArrayList. Two methods that are used are ‘compare()’ and ‘compareTo()’ which are defined in java.util.Comparator and java.lang.Comparable classes respectively.

Suppose we have an Employee class as defined below.

In this class, if we wish to have a natural ordering of employee objects according to their ID’s, we implement a Comparable interface and its corresponding method ‘compareTo(Employee emp). The ‘compareTo(Obj o)’ can return one of three values.

a. positive value : which means that, in this case, the current object (this) is greater than emp.

b. negative value: which means that, in this case, the current object(this) is smaller than emp.

c. zero : which means that, in this case, both the current object(this) and emp are equal.

The implementation is shown below

Now when Collections.sort(List) is called on a list of Employee objects, the ‘compareTo()’ will automatically do the ordering of all the Employee instances in the order of Employee ID’s. Notice that a class implementing  ‘Comparable’ interface can compare only its own instance with another object whereas a class implementing ‘Comparator’ interface is used to compare not its own instance but instances of other class. We will see that below.

Say now we have a need to sort the employees according to their names or age or any other field, then all we have to do is change the ‘compareTo()’ method everytime !! sounds absurd? yes it is. Not only will that involve changing the Employee class everytime but also will result in we losing the natural ordering of the class. That is when Comparator comes in.

All one has to do if one wants the ordering of a collection of objects to be different from the natural ordering defined for that object is to implement the Comparator interface and define the comparison criteria as done in previous example. The implementation is shown below.

The class EmployeeByName sorts the Employee instances according to their names in lexicographic manner. The compare() method in this class implements the sorting mechanism. The comparison is done in following manner.

So as has been shown here, the ordering can be done based on any variable of the object without changing the object at any point of time and without disturbing the natural ordering of the object instances.


4. How can you copy objects in Java? or what is a clone() method? or How should copy constructors be used in JAVA?

A. One line answer : Always implement a custom copy constructor which is declared protected and override ‘clone()’ to use that.

I think the link HERE gives the best explanation about the answer and hence I wont be repeating it again here.


5. Upcasting and Downcasting in Java?

A. (PS :This is one of the questions that tests the basics of a candidate and after seeing the inability of a surprisingly large number of candidates to answer this, I make sure I ask it to every candidate I interview. )

Again, I dont think I can explain this better than the post HERE and hence I will not repeat it. But please make sure that you read it till the end to familiarize yourself with the subtleties of upcasting and downcasting Java objects through the inheritance chain.

The other standard questions that I have asked or I have been asked include the likes of following

– Comparison between HashMap and HashSet and HashTable

– Comparison between String and StringBuffer and questions on String references

– Final/Finalize/Finally keywords and their usage

– Overloading and Overriding

– Abstract Classes and Interfaces

– Iterators


Share Comments
comments powered by Disqus