Showing posts with label Java most asked interview questions. Show all posts
Showing posts with label Java most asked interview questions. Show all posts

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 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)

JSP interview questions

Q: What is JSP?
A: JavaServer Pages (JSP) is a technology for developing web pages that support dynamic content which helps developers insert java code in HTML pages by making use of special JSP tags, most of which start with <% and end with %>.
Q: What are advantages of using JSP?
A: JSP offer several advantages as listed below:
Performance is significantly better because JSP allows embedding Dynamic Elements in HTML Pages itself.
JSP are always compiled before it's processed by the server unlike CGI/Perl which requires the server to load an interpreter and the target script each time the page is requested.
JavaServer Pages are built on top of the Java Servlets API, so like Servlets, JSP also has access to all the powerful Enterprise Java APIs, including JDBC, JNDI, EJB, JAXP etc.
JSP pages can be used in combination with servlets that handle the business logic, the model supported by Java servlet template engines.
Q:What are the advantages of jsp over servlet?
A: The advantage of JSP is that they are document-centric. Servlets, on the other hand, look and act like programs. A Java Server Page can contain Java program fragments that instantiate and execute Java classes, but these occur inside an HTML template file and are primarily used to generate dynamic content. 
Some of the JSP functionality can be achieved on the client, using JavaScript. The power of JSP is that it is server-based and provides a framework for Web application development.
What are the different types of JSP tags?
The different types of JSP tags are as follows:
JSP tags




Q: What is the life cycle of jsp?
A: Life cyle of jsp:
Translation
Compilation
Loading the class
Instantiating the class
jspInit()
_jspService()
jspDestroy()

Q:What jsp life cycle method can I override?
A: You cannot override the _jspService() method within a JSP page. You can however, override the jspInit() and jspDestroy() methods within a JSP page. JspInit() can be useful for allocating resources like database connections, network connections, and so forth for the JSP page. It is good programming practice to free any allocated resources within jspDestroy().
Q: What are implicit objects in jsp?
A: Implicit objects in JSP are the Java objects that the JSP Container makes available to developers in each page. These objects need not be declared or instantiated by the JSP author. They are automatically instantiated by the container and are accessed using standard variables; hence, they are called implicit objects.
Q: Difference Between include Directive and include Action of JSP
This JSP interview question is a continuation of earlier question I just made it a separate one to write answer in clear tabular format.
Include Directive
Include Action
include directive is processed at the translation time
Include action is processed at the run time.
include directive can use relative or absolute path
Include action always use relative path
Include directive can only include contents of resource it will not process the dynamic resource
Include action process the dynamic resource and result will be added to calling JSP
We can not pass any other parameter
Here we can pass other parameter also using JSP:param
We cannot  pass any request or response object to calling jsp to included file or JSP or vice versa
In this case it’s possible.

Q: What are the JSP implicit objects ?
JSP provides 9 implicit objects bydefault.They are as follows:

Object
Type
1) out
JspWriter
2) request
HttpServletRequest
3) response
HttpServletResponse
4) config
ServletConfig
5) session
HttpSession
6) application
ServletContext
7) pageContext
PageContext
8) page
Object
9) exception
Throwable

Q:
 What is a Declaration?
A:
A declaration declares one or more variables or methods for use later in the JSP source file.A declaration must contain at least one complete declarative statement. You can declare any number of variables or methods within one declaration tag, as long as they are separated by semicolons. The declaration must be valid in the scripting language used in the JSP file.

<%! somedeclarations %>
<%! int i = 0; %>
<%! int a, b, c; %>

Q:
What is a Expression?
A:
An expression tag contains a scripting language expression that is evaluated, converted to a String, and inserted where the expression appears in the JSP file. Because the value of an expression is converted to a String, you can use an expression within text in a JSP file. Like
<%= someexpression %>
<%= (new java.util.Date()).toLocaleString() %>
You cannot use a semicolon to end an expression



Q:
 What is a Scriptlet?
A:
1.   A scriptlet can contain any number of language statements, variable or method declarations, or expressions that are valid in the page scripting language.Within scriptlet tags, you can1.Declare variables or methods to use later in the file (see also Declaration).
2.   Write expressions valid in the page scripting language (see also Expression).
3.   Use any of the JSP implicit objects or any object declared with a <jsp:useBean> tag.
You must write plain text, HTML-encoded text, or other JSP tags outside the scriptlet.
Scriptlets are executed at request time, when the JSP engine processes the client request. If the scriptlet produces output, the output is stored in the out object, from which you can display it.



Q:
Difference between forward and sendRedirect?
A:
When you invoke a forward request, the request is sent to another resource on the server, without the client being informed that a different resource is going to process the request. This process occurs completly with in the web container. When a sendRedirtect 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 completly new request any object that are stored as request attributes before the redirect occurs will be lost. This extra round trip a redirect is slower than forward.