Saturday, November 25, 2006

Core Java Interview Questions 3

Question: What are synchronized methods and synchronized statements?
Answer:
Synchronized methods are methods that are used to control access to an object. A thread only executes a synchronized method after it has acquired the lock for the method's object or class. Synchronized statements are similar to synchronized methods. A synchronized statement can only be xecuted after a thread has acquired the lock for the object or class referenced in the synchronized statement.

Question: What are three ways in which a thread can enter the waiting state?
Answer:
A thread can enter the waiting state by invoking its sleep() method,
by blocking on I/O, by unsuccessfully attempting to acquire an object's lock, or
by invoking an object's wait() method. It can also enter the waiting state by
invoking its (deprecated) suspend() method.

Question: Can a lock be acquired on a class?
Answer:
Yes, a lock can be acquired on a class. This lock is acquired on the class's Class object.

Question: What's new with the stop(), suspend() and resume() methods in JDK 1.2?
Answer:
The stop(), suspend() and resume() methods have been deprecated in JDK 1.2.

Question: What is the preferred size of a component?
Answer:
The preferred size of a component is the minimum component size that will allow the component to display normally.

Question: What method is used to specify a container's layout?
Answer:
The setLayout() method is used to specify a container's layout.

Question: Which containers use a FlowLayout as their default layout?
Answer:
The Panel and Applet classes use the FlowLayout as their default layout.

Question: What is thread?
Answer:
A thread is an independent path of execution in a system.

Question: What is multithreading?
Answer:
Multithreading means various threads that run in a system.

Question: How does multithreading take place on a computer with a single CPU?
Answer:
The operating system's task scheduler allocates execution time to multiple tasks. By quickly switching between executing tasks, it creates the impression that tasks execute sequentially.

Question: How to create multithread in a program?
Answer:
You have two ways to do so. First, making your class "extends" Thread class. Second, making your class "implements" Runnable interface. Put jobs in a run() method and call start() method to start the thread.

Question: Can Java object be locked down for exclusive use by a given thread?
Answer:
Yes. You can lock an object by putting it in a "synchronized" block. The locked object is inaccessible to any thread other than the one that explicitly claimed it

Question: Can each Java object keep track of all the threads that want to exclusively access to it?
Answer:
Yes

Question: What state does a thread enter when it terminates its processing?
Answer:
When a thread terminates its processing, it enters the dead state.

Question: What invokes a thread's run() method?
Answer:
After a thread is started, via its start() method of the Thread class, the JVM invokes the thread's run() method when the thread is initially executed.

Question: What is the purpose of the wait(), notify(), and notifyAll() methods?
Answer:
The wait(),notify(), and notifyAll() methods are used to provide an efficient way for threads to communicate each other.

Question: What are the high-level thread states?
Answer:
The high-level thread states are ready, running, waiting, and dead.

Question: What is the Collections API?
Answer:
The Collections API is a set of classes and interfaces that support operations on collections of objects.

Question: What is the List interface?
Answer:
The List interface provides support for ordered collections of objects.

Question: How does Java handle integer overflows and underflows?
Answer:
It uses those low order bytes of the result that can fit into the size of the type allowed by the operation.