Java Interview questions

Friday, 20 April 2007, 12:41 | Category : Interview questions, Java, Technology
Tags :

Q. How to create Immutable class?
Immutable objects are simply objects whose state (the object’s data) does not change after construction. Always construct an object completely, instead of using a no-argument constructor combined with subsequent calls to setXXX methods. Do not provide any methods which can change the state of the object in any way – not just setXXX methods, but any method which can change state. ensure no methods can be overridden – make the class final, or use static factories and keep constructors private, make fields final


Q. Why SingleThreadModel in servlets is depreceated?
A. SingleThreadModel does not solve all thread safety issues. For example, session attributes and static variables can still be accessed by multiple requests on multiple threads at the same time, even when SingleThreadModel servlets are used. It is recommended that a developer take other means to resolve those issues instead of implementing this interface, such as avoiding the usage of an instance variable or synchronizing the block of the code accessing those resources. This interface is deprecated in Servlet API version 2.4.

Q. How will u synchronise list?
A. use Collections.synchronizedCollection( Collection c) or Collections.synchronizedList( list l)

Hashmap allows more than one null insertion also, it overwrites the existing one. At max there will be one null key. On get(null) it will return the last inserted value.

HashMap :- This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time. TO maintain order of insertion:
In JDK1.4, LinkedHashMap can be used. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order). Note that insertion order is not affected if a key is re-inserted into the map.

  • vips

    Can a jsp extend another jsp, if yes/no why?

  • anjanBacchu

    hi there,

    weblogic comes with a base class for the JSP. You can extend this class to add your required methods. That is the same as extending an existing JSP(which compiles to a HttpServlet!).

    BR,
    ~A

  • Master of Nothing

    I’m not sure about the question “Why SingleThreadModel in servlets is depreceated?”. I don’t think you would find many engineers who have even heard of SingleThreadModel. This is the first time I came across it :)

  • Jay

    I have to disagree with making the immutable class final. That ensures that there can be no derivations of the given class. Immutable in this context simply means that the state of an instance of given class cannot modified.