Tuesday, October 8, 2013

Java Quiz - 6

1) How does deserialization works in java?
We read the object from the external device for eg. a file and then we create a new instance of the object and make the object referene point to the deserialized object's state.
    try{
FileInputStream door = new FileInputStream("name_of_file.sav");
ObjectInputStream reader = new ObjectInputStream(door);
MyObject x = new MyObject();
x = (MyObject) reader.nextObject();
}catch (IOException e){
e.printStackTrace();
}

2)Hierarchy of exceptions in java?
java.lang.object ==> Throwable >> Exception >> RunTime Exceptions(unchecked)
                                                              Error              IO Exceptions(Checked)
                                                                                      SqlException(Unchecked)

3)When do u use checked exceptions and when do u not use unchecked exceptions?
Checked exceptions are compile time exceptions . i.e,  In Java whenever you access an IO, or DB there may be chances of failure always,  Java wanted to handle it separately it is categorized as  checked Exceptions.

4)What is the runtime of an arraylist.sort()?
ArrayList sorts the smaller arrays size<7 using insertion sort and larger arrays using merge sort.
Although merge sort runs in O(n*logn) worst-case time and insertion sort runs in O(n*n) worst-case time, the constant factors in insertion sort can make it faster in practice for small problem sizes on many machines.

5)How a form data is passed into another jsp? or another servlet?
  * using hidden fields
*using sessions
*using request objects

6)What is the difference b/w  using req.getAttribute("name") and request.getParameterName("name") ?
We can refer to the form data by using request.getParameterName("Name") and we can refer to the attributes which we have set it in our servlet to the request scope by using request.getAttribute();
There will not be any clash if two same names occur because, one is set at client another one at server side. So both are different.

7)How do you write your own custom tag?
* Write a tag handler class which extends Tagsupport / Body TagSupport
* Write a .tld file where mention <taglib> <tag><attribute></attribute> </tag> </taglib>
* Write <@taglib prefix="mytag" uri="taghandler" />

8)How do you get access to ServletContext from your jsp page?
${pageContext.servletContext}
or 
application 
 

No comments:

Post a Comment