What is the difference between Iterator and Enumeration?

Iterator
Enumeration
It is useful to retrieve object
From a collection
It’s also useful to retrieve object
From a collection
Iterator has methods whose
Names are easy to follow
Enumeration has methods whose
Names are difficult to remember
Iterator has an option to remove
Objects from a collection
Which is not available
So Iterator is preferred than Enumeration

What is the difference between Iterator and ListIterator?

  • Both are useful to retrieve elements from a collection.
  • Iterator can retrieve the elements only in forward direction.
  • But ListIterator can retrieve the elements in forward and backward direction also.
  • So ListIterator is preferred than Iterator

What is map in collection framework?

  • Maps stored elements in the form of key and value pairs.
  • If key is provided then its corresponding value can be obtained.
  • Of course key should have unique values.

What is a queue in collections framework?

  • A queue represents arrangements of elements in FIFO(Firs In First Out) order.
  • This means that element that is stored as a first element into the queue will be removed first from the queue.

What is lists in collection framework?

  • Lists are like Sets.
  • They store a group of elements,
  • But Lists allow duplicate values to be stored.

What is a set?

  • A set represents a group of elements arranged just like an array.
  • The set will grow dynamically when the elements are stored into it.
  • A set will not allow duplicate elements.
  • If we try to pass same element that is already available in the set, then it is not stored into it.
  • Set will not maintain the same order of elements as in which they were entered.

What is the differences between Java5 and Java6?





JAVA-5
JAVA-6
Also known as  Tiger
Also known as  Mustang
  •      Generics—Provides compile-time type safety for collections and eliminates the need for casting every time you get an object out of Collections.
  •  Enhanced For loop—Eliminates error-proneness of iterators.
  • Autoboxing/unboxing—Eliminates need of manual conversion between primitive types (such as double) and wrapper types (such as Double).
  • Typesafe enumsProvides all benefits of the Typesafe enum pattern.
  • Static import—Eliminates the need for using class names prior to using the static member variables of other classes. This will make the code a bit neater.
  • Metadata—Allows  programmers to avoid writing boiler plate code and gives the opportunity for declarative programming.
  • Annotation Processing API (JSR 269)
  • Common Annotations (JSR 250)
  • Java API for XML Based Web Services - 2.0 (JSR 224)
     JAXB 2.0 (JSR 222)
  • Web Services Metadata (JSR 181)
  • Streaming API for XML (JSR 173)
  • XML Digital Signature (JSR 105)
  • Java Class File Specification Update (JSR 202)
  • Java Compiler API (JSR 199)
  • JDBC 4.0 (JSR 221)
  • Scripting in the Java Platform (JSR 223)

Servlet interview questions

What is the Servlet?
A servlet is a Java programming language class that is used to extend the capabilities of servers that host applications accessed by means of a request- response programming model.

What are the uses of Servlet?
Typical uses for HTTP Servlets include:
Processing and/or storing data submitted by an HTML form.
Providing dynamic content, e.g. returning the results of a database query to the client.
A Servlet can handle multiple request concurrently and be used to develop high performance system
Managing state information on top of the stateless HTTP, e.g. for an online shopping cart system which manages shopping carts for many concurrent customers and maps every request to the right customer.
How the servlet is loaded?
A servlet can be loaded when:
First request is made.
Server starts up (auto-load).
There is only a single instance which answers all requests concurrently. This saves memory and allows a Servlet to easily manage persistent data.
Administrator manually loads.

What is Servlet interface?

The central abstraction in the Servlet API is the Servlet interface. All servlets implement this interface, either directly or , more commonly by extending a class that implements it.
Servlet

Note: Most Servlets, however, extend one of the standard implementations of that interface, namely javax.servlet.GenericServlet andjavax.servlet.http.HttpServlet.

What is difference between forward() method of RequestDispatcher and sendRedirect() method ?

forward() method
sendRedirect() method
1) forward() sends the same request to another resource.
1) sendRedirect() method sends new request always because it uses the URL bar of the browser.
2) forward() method works at server side.
2) sendRedirect() method works at client side.
3) forward() method works within the server only.
3) sendRedirect() method works within and outside the server.
 
What is the difference between doGet() and doPost()?
doGet()
doPost()
In doGet() the parameters are appended to the URL and sent along with header information
In doPost(), on the other hand will (typically) send the information through a socket back to the webserver and it won't show up in the URL bar.
The amount of information you can send back using a GET is restricted as URLs can only be 1024 characters.
You can send much more information to the server this way - and it's not restricted to textual data either. It is possible to send files and even binary data such as serialized Java objects!
doGet() is a request for information; it does not (or should not) change anything on the server. (doGet() should be idempotent)
doPost() provides information (such as placing an order for merchandise) that the server is expected to remember
Parameters are not encrypted
Parameters are encrypted
doGet() is faster if we set the response content length since the same connection is used. Thus increasing the performance
doPost() is generally used to update or post some information to the server.doPost is slower compared to doGet since doPost does not write the content length
doGet() should be idempotent. i.e. doget should be able to be repeated safely many times
This method does not need to be idempotent. Operations requested through POST can have side effects for which the user can be held accountable.
doGet() should be safe without any side effects for which user is held responsible
This method does not need to be either safe
It allows bookmarks.
It disallows bookmarks.

What is the difference between PrintWriter and ServletOuputStream?
PrintWriter is a character-stream class where as ServletOutputStream is a byte-stream class. The PrintWriter class can be used to write only character-based information where as ServletOutputStream class can be used to write primitive values as well as character-based information.



What is the difference between GenericServlet and HttpServlet?
GenericServlet
HttpServlet
The GenericServlet is an abstract class that is extended by HttpServlet to provide HTTP protocol-specific methods.
An abstract class that simplifies writing HTTP servlets. It extends the GenericServlet base class and provides an framework for handling the HTTP protocol.
The GenericServlet does not include protocol-specific methods for handling request parameters, cookies, sessions and setting response headers.
The HttpServlet subclass passes generic service method requests to the relevant doGet() or doPost() method.
GenericServlet is not specific to any protocol.
HttpServlet only supports HTTP and HTTPS protocalls.