Java Questions 51 - 100  «Prev Next»

Polymorphism, Servlets, MVC

  1. What is polymorphism?

    Answer:
    Polymorphism is also known as dynamic binding. The class determines what method to call at runtime. Let us assume we have a superclass called shape and subclasses circle and triangle. Both subclasses circle and triangle have a draw() method. Based on which object is called, the compiler knows which method to execute . (Based on which object is instantiated will determine which method of the subclass is going to be executed.)

  2. In which component of module-view-controller application would user authentication typically be performed?

    Answer:
    The username and password are entered in the “View”, but the authentication occurs in the controller. If authentication is successful, you forward the user to the welcome page, else the invalid password page will be displayed.

  3. Why is a servlet initialized with its init() method instead of a constructor?

    Answer:
    First the servlet is constructed, then initialized with the init() method. Any request from the client is handled initially by the service() method before delegating to the doXxx() methods in the case of HttpServlet.
    The servlet is removed from service, destroyed with the destroy() method, then the garbage collector is called and finalized.

  4. Can you tell me the difference between a a) hash table, b) hash map and c) tree map?

    Answer:
    A TreeMap implements SortedMap, so when you iterate over the contents, you will get the contents sorted by their natural order, or through a Comparator you defined when creating the TreeMap. With the regular HashMap, iterating over the content can result in any order.
    Key difference between Hashmap and HashTable: The key difference between the two is that access to the Hashtable is synchronized on the table while access to the HashMap is not. You can add synchronization to the HashMap, but it is not there by default. Another difference is that the iterator in the HashMap is fail-safe while the enumerator for the Hashtable is not. (enumerator for the Hashtable is not fail safe) If you change the map while iterating, you will know. A third difference is that a HashMap permits null values in it, while a Hashtable does not. For new code, I would tend to always use HashMap.

  5. During the course of designing an application, there exist trade-offs between maintainability and scalability. What are the trade-offs between maintainability and scalability?

    Answer:
    1) Maintainability is the ability to correct flaws in the existing functionality of the system without impacting other components of the system. 2) Scalability is the ability to support the required quality of service as the system load increases without changing the system.

  6. How would you implement a Singleton pattern in a simple Java Class?

    Answer:
    public class Singleton {
      static Singleton theInstance =  new Singleton();
      protected Singleton() {
      }
      public static Singleton getInstance() {
        return theInstance;
      }
    } 
    

  7. What is the difference between an interface and an abstract class?

    Answer:
    An abstract class has at least one abstract method. It is often desirable to specify a group of abstract methods defining some behavior for an object without tying it to any implementation at all. In Java, this is called an interface. All of the methods of an interface have to be used by the class that implements the interface.

  8. You are building a web server application using J2EE servlets, beans, JSP, and Oracle 9i database. During various stages of the application, the information needs to be collected from the user and used to personalize the content of the site. How would I go about building a web server application to manage session?

    Answer:
    Do not use cookies for storing Session ID, but use request parameters instead. This way, each opened tab will request their own session. With cookies you have only one cookie for all tabs in browser.

  9. What is the difference between composition and aggregation?

    Answer:
    Airports have Aircraft. Aircraft are not part of Airports, but the Airport still has airplanes. This type of relation is called aggregation. A car has tires and a car uses a gas station. The uses relationship is depicted by a dashed line with an arrow and is called a dependency relationship. This type of relationship is called composition. Composition means the contained object is a part of the containing object.
    Aggregation is a special type of association like a whole-part relationship. One or more classes are part of a larger whole. Aggregation represents a looser grouping, and if the links are broken the original objects still exist. In the case of composition, if the links that bind the objects are broken, then all objects are destroyed.

  10. What is the difference between a “forward” and “Send-Redirect”? (See SCWCD)

    Answer:
    When the <jsp:forward> standard action is executed at request time, the current request is forwarded to another JSP page. The processing of the current JSP is terminated.
    When a sendRedirect method is invoked it causes the web container to return to the browser indicating that a new URL should be requested. Because the browser issues a completely new request, any objects that are stored as request attributes before the redirect occurs will be lost. Because of this extra round trip, a redirect is slower than forward.

  11. In Java, can you tell me the difference between a checked exception and an unchecked exception?

    Answer:
    Exceptions that are not subclasses of RuntimeException are checked for by the compiler. They are called “checked exceptions”. RuntimeExceptions are not checked by the compiler. They are known as “unchecked exceptions.” You can throw, catch, and declare RuntimeExceptions, but you do not have to, and the compiler will not check.

  12. What does the keyword static mean in Java?

    Answer:
    Static allows you to make a method call without instantiating an object. Static variable is also know as a Class variable.