1: What is annotation in Java?

Annotation in Java programming is a special kind of syntactic metadata, which can be included to java source code. Classes, variable parameters, packages and methods may be annotated. Not like Java doc tags, Java annotation is found to be reflective that are embedded in class files produced by the compiler and retained by the java VM to be retrievable during run-time. Basically, annotation deals with attaching metadata to class, method or package. Metadata is utilized by compiler to carry out some fundamental compile-time checking.

2: Explain Java thread life cycle?

The java thread life cycle is similar to that of the life cycle of processes that runs in an operating system. During the life cycle, the thread shifts from one state to other based on the operation carried out by it or carried out on it. Java thread can remain one of the states given below,

  1. New: Thread that is just instantiated is in the new state. When start () method is invoked, thread moves to ready state from which it is moved automatically to runnable state by thread scheduler.
  2. RUNNABLE (ready_running): a thread being executed in JVM is in the running state
  3. BLOCKED: a thread which is blocked and waiting for a monitor lock is under this state. This can occur if a thread performs an input/output operation and shifts to next runnable state
  4. Waiting: a thread which is waiting for another thread indefinitely to carry out a specific action is under this state
  5. TIMED_WAITING (sleeping): a thread which is waiting for other thread to carry out an action up to a particular waiting time is under this state
  6. TERMINATED (dead): a thread which has exited is under this state

3: Different ways for generating thread?

There are two different methods of generating thread

1) Implementing thread: Runnable interface describes a single method, run, intended to include the code that executed in the thread. The runnable object is passed to thread constructor, as that in the HelloRunnable example

public class HelloRunnable implements Runnable {

public void run() {

System.out.println(“Hello from a thread!”);


public static void main(String args[]) {

(new Thread(new HelloRunnable())).start();



2) Subclass thread: the thread class implements runnable interface, although it runs the method that does nothing. An application can subclass the thread and offers its own implementation of run, similar to that in the HelloThread example:

public class HelloThread extends Thread {

public void run() {

System.out.println(“Hello from a thread!”);


public static void main(String args[])


(new HelloThread()).start();



4: What is the difference between statement and PreparedStatement in java?

  1. A statement is parsed as well as executed every time its call transferred to database
  2. A prepared statement may parse once and repeatedly executed with various parameters

Four steps for executing query:

  1. Query is parsed
  2. Query is compiled
  3. Query is optimized
  4. Query is executed

Question 5: What is the purpose of join method?

The join method lets one thread to wait for another to complete. When‘t’ is a thread object, the thread of which is currently executing, then


Makes the current thread to pause the execution until the thread of terminates. The overloads of join enable the programmer to specify waiting period. But, as with sleep, join is based on the OS for timing, hence you must not assume that join can wait exactly until you specify.

Question 6: What is the purpose of volatile in java?

Threads are made to hold the values of variables in the local memory. For example, in a machine register. When a variable is indicated as volatile, each time when the variable is utilized, it should be read from main memory, likewise every time when the variable is written, value should be stored in main memory.

7: Explain the working of static variable in java?

  1. Static code is loaded prior to the class is instantiated and remains in memory until JVM exits as against instance variable that are loaded as well as unloaded, called dynamic code
  2. Every class contain single copy of each of its static members in the memory
  3. Every instance of the class get access to the single static memory location
  4. The single member remains the same for all instance
  5. Static member doesn’t get access to instance members

8: What is mean by hash code collision?

The main idea of hashing is to store any element into array in a position index that is computed below,

  1. Get element_hash_code of the element by processing the data of elements and producing an integer value
  2. Utilize a simple mod operation to map into range of array: index = hash(element) = abs(element_hash_code % array_capacity)

9: What is the difference between Executer.execute() and Executor.submit() method ?

There is a main difference between the exception handling. When the tasks throws the exception and it is submitted with the execute option then this exception will get the uncaught exception handler. If you submitted task with the submit option along thrown exception then the exception is part of task’s return status. When a task is submitted with the submit and terminates with the exception then exception the Future.get() will re-throw it as it is wrapped in the ExecutionException.

Question 10: What is the simplest way for finding the method for execution without using the profiling tool?

When time taken for the execution is too less then it will show that it will show the zero milliseconds for the execution. The considerable amount of processing is needed for the execution.

long start = System.currentTimeMillis ();

method ();

long end = System.currentTimeMillis ();

System.out.println (“Time taken for execution is ” + (end – start));

11: How the substring () inside String works?

Substring normally creates a new object from source string by taking the portion of original string. Substring holds the original character array reference as the 5 character long substring prevents the 1GB character array by holding the strong reference. The original character array is fixed in the Java 1.7 and the changes are also made with the creation of the substring bit in terms of time.

Question 12: What is difference between creating a String as new() and literal?

When we are creating a String as new() Operator, then it will create a heap so that it will not add into the string pool. String created the literal which creates the String pool.

String str = new String(“Test”)

We can call String.intern() method for putting the String pool explicitly do not put the object str. When we create the String objects as the String literal like String s = “Test” then Java automatically puts them into the String pool. This also creates another object such as “Test” for the string pool for String literal “Test”.

Question 13: Why the String class is immutable or final?

Strings are implemented as the final or immutable objects so that it will be safer for getting the String Immutability in Java. The thread-safe Immutable objects will be quick. Two threads work on immutable objects without any possibility of the conflict.

The system can pass the sensitive bits with the read-only information without altering it.

Sharing the duplicates with pointing them for a single instance

Creating the substrings without any copying

Immutability is top secret that makes fast Java substring implementation

Immutable objects are also good for making the Hashtable keys

String is immutable so inside each String there is a correct length char[]

14: What is the OutOfMemoryError in java and how to solve the java.lang.OutOfMemeryError error?

The OutOfMemoryError is shown when Java Virtual Machine does not allocate the object when there is no memory so the memory space will not be available for the garbage collector. Two types of OutOfMemoryError are

1) java.lang.OutOfMemoryError: Java heap space

This is a quick solution for adding the flags to JVM command line when the Java runtime gets started:

-Xms1024m -Xmx1024m

2) java.lang.OutOfMemoryError: PermGen space

This is another solution for adding the flags to JVM command line when Java runtime gets started:


15: What is the Marker interface and how can it be used in Java?

The marker interface is the design pattern that is used for languages to provide the run-time information about the objects. The marker interface provides the way for associating the metadata class. For using this pattern, the class implements marker interface with the code to interact with the instances of class test so that the existence of interface is enabled.

  1. java.io.Serializable – Serializability of the class gets enabled by class implementing java.io.Serializable interface. Java Classes do not implement Serializable interface cannot be able deserializ or serialize their state.
  2. java.rmi.Remote – Remote interface also serves for identifying the interfaces with the methods for the non-local virtual machine.
  3. java.lang.Cloneable – The class implements Cloneable interface for indicating the Object.clone() method which is legal method to make field-for-field copy instances.
  4. javax.servlet.SingleThreadModel – This model ensures the servlets handle only at the requested time.
  5. java.util.EvenListener – The tagging interface for all event listener interfaces extends