Java Interview Questions and Answers Set 4

31.Explain the life cycle of a Servlet

On every client’s request, the Servlet Engine loads the servlets and invokes its init methods, in order for the servlet to be initialized. Then, the Servlet object handles all subsequent requests coming from that client, by invoking the service method for each request separately. Finally, the servlet is removed by calling the server’s destroy method.

32.What is the purpose of using RMISecurityManager in RMI ?

RMISecurityManager provides a security manager that can be used by RMI applications, which use downloaded code. The class loader of RMI will not download any classes from remote locations, if the security manager has not been set.

33.What is Java String Pool?

A String Pool in Java is a distinct place that has a pool of strings stored via the Java Heap Memory. Here, String represents a special class in Java, and string objects can be created using either a new operator or using values in double-quotes. The String is immutable in Java, thus, making feasibility of String pool and then the further implementation via String interning concepts.

34.What advantage do Java’s layout managers provide over traditional windowing systems ?

Java uses layout managers to lay out components in a consistent manner, across all windowing platforms. Since layout managers aren’t tied to absolute sizing and positioning, they are able to accomodate platform-specific differences among windowing systems.

35. What is synchronization?

Synchronization in Java defines the capability to manage and control the access of multiple threads to a particular resource. So that, one thread can access a specific resource at the present time. Here, Java allows the development of threads and then synchronizing tasks via the synchronizing blocks.
These Synchronized blocks allow only one thread execution for a particular time and block the other threads until the current thread exits in the block. Here monitors concept is crucial in implementing the synchronization in Java. Once the thread goes in a lock phase, it is termed to enter the monitor. Thus, locking all the other threads unless the first thread has exited the monitor.
Synchronization gives the capability to control multiple threads at any resource that is shared. Synchronization becomes usefu for reliable communication between threads as the multiple threads might try to access the shared resources at the same time thus producing results that are inconsistent.

Java TRAINING
Weekend / Weekday Batch

36.What are untrusted applets ?

Untrusted applets are those Java applets that cannot access or execute local system files. By default, all downloaded applets are considered as untrusted.

37.What is the purpose of garbage collection in Java, and when is it used ?

The purpose of garbage collection is to identify and discard those objects that are no longer needed by the application, in order for the resources to be reclaimed and reused.

38.What is the tradeoff between using an unordered array versus an ordered array ?

The major advantage of an ordered array is that the search times have time complexity of O(log n), compared to that of an unordered array, which is O (n). The disadvantage of an ordered array is that the insertion operation has a time complexity of O(n), because the elements with higher values must be moved to make room for the new element. Instead, the insertion operation for an unordered array takes constant time of O(1).

39.What is the importance of hashCode() and equals() methods ?

In Java, a HashMap uses the hashCode and equals methods to determine the index of the key-value pair and to detect duplicates. More specifically, the hashCode method 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.

40.What is an Iterator ?

The Iterator interface provides a number of methods that are able to iterate over any Collection. Each Java Collection contains the iterator method that returns an Iterator instance. Iterators are capable of removing elements from the underlying collection during the iteration.