Interview Questions


Java Timer

In this section lets discuss about how to use Timer Api in java.

In many scenarios there may be a need to run a task in repeated time interval. Java
provides a simple api to handle this case using Timer api. The code for executing a Timer task is very simple as follows

Timer t = new Timer();
t.schedule(TimerTask task,long delay,long period);

Java ThreadPool Executor

In this section , lets discuss about how to run multiple threads and manage the same using java threadpoolexecutor.

First of all lets see how to run a simple thread in java

Simple Thread


public class MyThread implements Runnable
    public void run()
        System.out.println("Thread execution is completed....");

public class MainThread
    public MainThread()

    public static void main(String[] args)
        Thread tD = new Thread(new MyThread());

Java Search Objects

Consider a scenario where you need to search through a list of objects for a particular value. For example, There is an ArrayList of Employees as follows.

    ArrayList al = new ArrayList();
    al.add(new Employee("100", "jack"));
    al.add(new Employee("102", "rose"));
    al.add(new Employee("103", "rock"));
    al.add(new Employee("104", "lily"));

Java - Simple Date Format

In this section, Lets discuss about handling Date format in Java application.

1. Lets first start with converting Current time in to a particular Date format (for example "12 Sep 2007 10:46:43:021")

Inorder to achive this functionlity, we are going to use SimpleDateFormat Api provided by Java.

* Create a SimpleDateFormat object using the specified format ( Eg., dd MMM yyyy HH:mm:ss:SSS ) as you wish.

Java - File Read/Write Operations

The below code sample describes the File Read and Write Operation.

1. File Read
Read all the contents from the File which exists in the same machine.
Create a File in "C:/Src.txt" with some content inside. Executing the below program will fetch all the contents and print in the console.

2. File Write
Write the contents in to the File.
Executing the below program will overwrite the File in "C:/Src.txt" with the content given in the sample program.


Java - Read/Write Properties file

Having a Properties file in any Java application in one of the mandatory things which needs to be done in order to achieve generic functionality. The reason behind this statement is that if any constant values are placed in a Final Java class, then if the value needs to be changed at a later time, then it involves changing the source code of the class which would result in re-deployment of the application.

In order to make it simpler, if the properties file have those constant values then changing the Properties file alone would achieve the functionality. Its so simple right!