- What are the states of a thread?
Answer: There are four states of thread:
New
- Runnable
- Not runnable
- Dead
- What is a socket?
Answer: A socket is an endpoint for communication between two machines.
- How will you establish the connection between the servlet and an applet?
Answer: Using the URL, I will create the connection URL.Then by openConnection method of the URL, I will establish the connection, through which I can be able to exchange data.
- What are the threads will start, when you start the java program?
Answer: Finalizer, Main, Reference Handler, Signal Dispatcher.
- Is it necessary that each try block must be followed by a catch block?
Answer: It is not necessary that each try block must be followed by a catch block.It should be followed by either a catch block OR a finally block.And whatever exceptions are likely to be thrown should be declared in the throws clause of the method.
- If I write return at the end of the try block, will the finally block still execute?
Answer: Yes even if you write return as the last statement in the try block and no exception occurs, the finally block will execute.The finally block will execute and then the control return.
- If I write System.exit (0); at the end of the try block, will the finally block still execute?
Answer: No in this case the finally block will not execute because when you say System.exit (0); the control immediately goes out of the program, and thus finally never executes.
- How are Observer and Observable used?
Answer: Objects that subclass the Observable class maintain a list of observers.When an Observable object is updated it invokes the update() method of each of its observers to notify the observers that it has changed state.The Observer interface is implemented by objects that observe Observable objects.
- What is synchronization and why is it important?
Answer: With respect to multithreading, synchronization is the capability to controlthe access of multiple threads to shared resources.Without synchronization, it is possible for one thread to modify a shared object while another thread is in the process of using or updating that object's value.This often leads tosignificant errors.
- Why do we need wrapper classes?
Answer: It is sometimes easier to deal with primitives as objects.Moreover most of the collection classes store objects and not primitive data types.And also the wrapper classes provide many utility methods also.Because of these resons we need wrapper classes.And since we create instances of these classes we can store them in any of the collection classes and pass them around as a collection.Also we can pass them around as method parameters where a method expects an object.