Descriptive Question Advanced Java

1. What do you mean by Generic?
J2SE 5.0 provides compile-time type safety with the Java Collections framework through generics .This allow us to specify, at compile-time, the types of objects we want to store in a Collection. So we don't need to cast anything.

2. What is a Thread?
A thread is the flow of execution of a single set of program statements. The Thread Class allows multitasking (ie running several tasks at the same time) by instantiating (ie creating) many threaded objects, each with their own run time characteristics.

3. What are the two ways of creating thread?
Ans) There are two ways to create a new thread.
a)Extend the Thread
b)Implements the Runnable interface e.g.

4. What is the difference between wait() and sleep()?
Ans) 1) wait() is a method of Object class. sleep() is a method of Thread class.
2) sleep() allows the thread to go to sleep state for x milliseconds. When a thread goes into sleep state it doesn’t release the lock. wait() allows thread to release the lock and goes to suspended state. The thread is only active when a notify() or notifAll() method is called for the same object.

5. What is the use of synchronized keyword?
Ans) synchronized keyword can be applied to static/non-static methods or a block of code. Only one thread at a time can access synchronized methods and if there are multiple threads trying to access the same method then other threads have to wait for the execution of method by one thread.

6. What is deadlock?
When two threads are waiting for each other and can’t proceed until the first thread obtains a lock on the other thread or vice versa, the program is said to be in a deadlock.

7. What is difference between ArrayList and vector?
Ans: )
1) Synchronization - ArrayList is not thread-safe whereas Vector is thread-safe. In Vector class each method like add(), get(int i) is surrounded with a synchronized block and thus making Vector class thread-safe.
2) Data growth - Internally, both the ArrayList and Vector hold onto their contents using an Array. When an element is inserted into an ArrayList or a Vector, the object will need to expand its internal array if it runs out of room. A Vector defaults to doubling the size of its array, while the ArrayList increases its array size by 50 percent.

8. Difference between Comparable and Comparator Interface?
Comparable Comparator
Comparable is a member of java.lang package. Comparator is a member of java.util.package.
Sorts the objects is natural order. Sorts the objects is custom order.
Its method is compareTo( object) Its method is compare(object1, object2)

9. What is stream?
A stream is a flow of data form a source to a sink.
Sources and sinks are also called input streams and output streams.
Java technology supports two type of data in stream raw bytes or Unicode character.

10. What is Serialization and deserialization?
Serialization is the process of transforming an in-memory object to a byte stream.
Deserialization is the inverse process of reconstructing an object from a byte stream to the same state in which the object was previously serialized.

11. Write the lifecycle method of a thread
void start()
– Creates a new thread and makes it runnable
– This method can be called only once
void run()
– The new thread begins its life inside this method
void stop()
– The thread is being terminated
The wait()
wait() causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object
notify( ) wakes up the first thread that called wait( ) on the same object.
notifyAll( ) wakes up all the threads that called wait( ) on the same object. The highest priority thread will run first

12. What are the daemon threads?
Ans) Daemon thread are service provider threads run in the background, these not used to run the application code generally. When all user threads(non-daemon threads) complete their execution the jvm exit the application whatever may be the state of the daemon threads. Jvm does not wait for the daemon threads to complete their execution if all user threads have completed their execution.
Example of the Daemon thread is the Garbage Collector run by jvm to reclaim the unused memory by the application.

13. What is the difference between yield() and sleep()?
Ans) yield() allows the current the thread to release its lock from the object and scheduler gives the lock of the object to the other thread with same priority.
sleep() allows the thread to go to sleep state for x milliseconds. When a thread goes into sleep state it doesn’t release the lock.

14. What is collection?
Write the type of collection?
A collection is a container that groups similar elements into an entity. In java Collection is an interface that represent different types of collections, such as sets, lists and maps. These interfaces form the basis of the framework.
The core Collection interface encapsulates different types of collections.

15. What is buffer Stream? Why do you use?
● An unbuffered I/O means each read or write request is handled directly by the underlying OS– This can make a program much less efficient, since each such request often triggers disk access, network activity, or some other operation that is relatively expensive. To reduce this kind of overhead, the Java platform implements buffered I/O streams
– Buffered input streams read data from a memory area known as a buffer; the native input API is called only when the buffer is empty
– Similarly, buffered output streams write data to a buffer, and the native output API is called only when the buffer is full.

16. Difference between wait and sleep?
Let's see the important differences between wait and sleep methods.
wait() sleep()
1. wait() method releases the lock 1. sleep() method doesn't release the lock.
2. is the method of Object class 2. is the method of Thread class
3. is the non-static method 3. is the static method
4. is the non-static method 4. is the static method
5. should be notified by notify() or notifyAll() methods 5. after the specified amount of time, sleep is completed.