Thursday, January 24, 2013

MultiThreading Java Interview Question




What are the two types of multitasking?


a. Process-based.b. Thread-based.

What is a Thread?

A thread is a single sequential flow of control within a program.

What are the two ways to create a new thread?

a.Extend the Thread class and override the run() method.b.Implement the Runnable interface and implement the run() method.

If you have ABC class that must subclass XYZ class, which option will you use to create a thread?

I will make ABC implement the Runnable interface to create a new thread, because ABC class will not be able to extend both XYZ class and Thread class.

Which package contains Thread class and Runnable Interface?

java.lang package

What is the signature of the run() mehod in the Thread class?

public void run()

Which methods calls the run() method?

start() method.

Which interface does the Thread class implement?

Runnable interface

What are the states of a Thread ?

Ready,Running,Waiting and Dead.

Where does the support for threading lie?

The thread support lies in java.lang.Thread, java.lang.Object and JVM.

In which class would you find the methods sleep() and yield()?

Thread class

In which class would you find the methods notify(),notifyAll() and wait()?

Object class

What will notify() method do?

notify() method moves a thread out of the waiting pool to ready state, but there is no guaranty which thread will be moved out of the pool.

Can you notify a particular thread?

No.

What is the difference between sleep() and yield()?

When a Thread calls the sleep() method, it will return to its waiting state. When a Thread calls the yield() method, it returns to the ready state.

What is a Daemon Thread?

Daemon is a low priority thread which runs in the backgrouund.

How to make a normal thread as daemon thread?

We should call setDaemon(true) method on the thread object to make a thread as daemon thread.

What is the difference between normal thread and daemon thread?

Normal threads do mainstream activity, whereas daemon threads are used low priority work. Hence daemon threads are also stopped when there are no normal threads.

Give one good example of a daemon thread?

Garbage Collector is a low priority daemon thread.

What does the start() method of Thread do?

The thread's start() method puts the thread in ready state and makes the thread eligible to run. start() method automatically calls the run () method.

What are the two ways that a code can be synchronised?

a. Method can be declared as synchronised.b. A block of code be sychronised.

Can you declare a static method as synchronized?

Yes, we can declare static method as synchronized. But the calling thread should acquire lock on the class that owns the method.

Can a thread execute another objects run() method?

A thread can execute it's own run() method or another objects run() method.

What is the default priority of a Thread?

NORM_PRIORITY

What is a deadlock?

A condition that occurs when two processes are waiting for each other to complete before proceeding. The result is that both processes wait endlessly.

What are all the methods used for Inter Thread communication and what is the class in which these methods are defined?

a. wait(),notify() & notifyall()b. Object class

What is the mechanisam defind in java for a code segment be used by only one Thread at a time?

Synchronisation

What is the procedure to own the moniter by many threads?

Its not possible. A monitor can be held by only one thread at a time.

What is the unit for 500 in the statement, obj.sleep(500);?

500 is the no of milliseconds and the data type is long.

What are the values of the following thread priority constants?
MAX_PRIORITY,MIN_PRIORITY and NORMAL_PRIORITY

10,1,5

What is the default thread at the time of starting a java application?

main thread

The word synchronized can be used with only a method. True/ False?

False. A block of code can also be synchronised.

What is a Monitor?

A monitor is an object which contains some synchronized code in it.

What are all the methods defined in the Runnable Interface?

only run() method is defined the Runnable interface.

How can i start a dead thread?

A dead Thread cannot be started again.

When does a Thread die?

A Thread dies after completion of run() method.

What does the yield() method do?

The yield() method puts currently running thread in to ready state.

What exception does the wait() method throw?

The java.lang.Object class wait() method throws "InterruptedException".

What does notifyAll() method do?

notifyAll() method moves all waiting threads from the waiting pool to ready state.

What does wait() method do?

wait() method releases CPU, releases objects lock, the thread enters into pool of waiting threads.

Interview Question of Exception In Java


Which package contains exception handling related classes?


java.lang

What are the two types of Exceptions?

Checked Exceptions and Unchecked Exceptions.

What is the base class of all exceptions?

java.lang.Throwable

What is the difference between Exception and Error in java?

Exception and Error are the subclasses of the Throwable class. Exception class is used for exceptional conditions that user program should catch. Error defines exceptions that are not excepted to be caught by the user program. Example is Stack Overflow.

What is the difference between throw and throws?

throw is used to explicitly raise a exception within the program, the statement would be throw new Exception(); throws clause is used to indicate the exceptions that are not handled by the method. It must specify this behavior so the callers of the method can guard against the exceptions. throws is specified in the method signature. If multiple exceptions are not handled, then they are separated by a comma. the statement would be as follows: public void doSomething() throws IOException,MyException{}

Differentiate between Checked Exceptions and Unchecked Exceptions?

Checked Exceptions are those exceptions which should be explicitly handled by the calling method. Unhandled checked exceptions results in compilation error.Unchecked Exceptions are those which occur at runtime and need not be explicitly handled. RuntimeException and it's subclasses, Error and it's subclasses fall under unchecked exceptions.

What are User defined Exceptions?

Apart from the exceptions already defined in Java package libraries, user can define his own exception classes by extending Exception class.

What is the importance of finally block in exception handling?

Finally block will be executed whether or not an exception is thrown. If an exception is thrown, the finally block will execute even if no catch statement match the exception. Any time a method is about to return to the caller from inside try/catch block, via an uncaught exception or an explicit return statement, the finally block will be executed. Finally is used to free up resources like database connections, IO handles, etc.

Can a catch block exist without a try block?

No. A catch block should always go with a try block.

Can a finally block exist with a try block but without a catch?

Yes. The following are the combinations try/catch or try/catch/finally or try/finally.

What will happen to the Exception object after exception handling?

Exception object will be garbage collected.

The subclass exception should precede the base class exception when used within the catch clause. True/False?

True.

Exceptions can be caught or rethrown to a calling method. True/False?

True.

The statements following the throw keyword in a program are not executed. True/False?

True.

How does finally block differ from finalize() method?

Finally block will be executed whether or not an exception is thrown. So it is used to free resoources. finalize() is a protected method in the Object class which is called by the JVM just before an object is garbage collected.

What are the constraints imposed by overriding on exception handling?

An overriding method in a subclass may only throw exceptions declared in the parent class or children of the exceptions declared in the parent class.