IGNOU BCA MCA Students - VIVA Question Answer for JAVA or JSP
JAVA or JSP Selected Question Answer - PART IV
15. How does thread synchronization occurs inside a monitor ?
What levels of synchronization can you apply ? The JVM uses
locks in conjunction with monitors. A monitor is basically a guardian that
watches over a sequence of synchronized code and ensuring that only one thread
at a time executes a synchronized piece of code. Each monitor is associated
with an object reference. The thread is not allowed to execute the code until
it obtains the lock.
16. What’s 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.
17. How do you ensure that N threads can access N resources
without deadlock ? A very simple way to avoid deadlock while using N threads
is to impose an ordering on the locks and force each thread to follow that
ordering. Thus, if all threads lock and unlock the mutexes in the same order,
no deadlocks can arise.
Java Collections
18. What are the basic interfaces of Java Collections Framework
? Java Collections Framework provides a well designed set of
interfaces and classes that support operations on a collections of objects. The
most basic interfaces that reside in the Java Collections Framework are:
19. Why Collection doesn’t extend Cloneable and Serializable
interfaces ? The Collection interface specifies groups of objects known as elements.
Each concrete implementation of a Collection can choose its own way of how to maintain and order its
elements. Some collections allow duplicate keys, while some other collections
don’t. The semantics and the implications of either cloning or serialization
come into play when dealing with actual implementations. Thus, the concrete
implementations of collections should decide how they can be cloned or
serialized.
20. What is an Iterator ? The Iterator interface provides a number of methods that are able to
iterate over anyCollection. Each Java Collection contains the iterator method that returns an Iterator instance. Iterators arecapable of removing
elements from the underlying collection during the iteration. 21. What
differences exist between Iterator and ListIterator ? The differences
of these elements are listed below:
·
An Iterator can be used to traverse the Set and List collections, while the ListIterator can be used to iterate only over Lists.
·
The Iterator can traverse a collection only in forward direction, while
the ListIterator can traverse a List in both directions.
·
The ListIterator implements the Iterator interface and contains extra functionality, such as adding
an element, replacing an element, getting the index position for previous and
next elements, etc.
22. What is difference between fail-fast and fail-safe ? The Iterator's fail-safe property works with the clone of the underlying
collection and thus, it is not affected by any modification in the collection.
All the collection classes in java.util package are fail-fast, while the
collection classes in java.util.concurrent are fail-safe. Fail-fast iterators
throw aConcurrentModificationException, while fail-safe
iterator never throws such an exception.
23. How HashMap works in Java ? A HashMap in Java stores
key-value pairs. The HashMap requires a hash function and uses hashCode and equals methods, in order to put and retrieve elements
to and from the collection respectively. When the put method is invoked, the HashMap calculates the hash value of the key and stores the pair
in the appropriate index inside the collection. If the key exists, its value is
updated with the new value. Some important characteristics of aHashMap are its capacity, its load factor and the threshold
resizing.
24. What is the importance of hashCode() and equals() methods ? In Java, a HashMap uses the hashCode andequals methods to determine the index of the key-value pair and
to detect duplicates. More specifically, the hashCodemethod is used in order to determine where the specified key
will be stored. Since different keys may produce the same hash value, the equals method is used, in order to determine whether the
specified key actually exists in the collection or not. Therefore, the
implementation of both methods is crucial to the accuracy and efficiency of the HashMap.
No comments:
Post a Comment