1.What is difference between Iterator and ListIterator?
Methods in Iterator :No. | Iterator | ListIterator |
---|---|---|
1) | Iterator traverses the elements in forward direction only. | ListIterator traverses the elements in backward and forward directions both. |
2) | Iterator can be used in List, Set and Queue. | ListIterator can be used in List only. |
- hasNext()
- next()
- remove()
Methods in ListIterator
- hasNext()
- next()
- previous()
- hasPrevious()
- remove()
- nextIndex()
- previousIndex()
2.Can we Override main() method?
ANS: No, because main is a static method.
3.Can we overload main() method?
ANS: YES
EX:
public class ttt {
public static void main(String[] a, int b){
System.out.println("Main1");
}
public static void main(int a, int b){
System.out.println("Main2");
}
public static void main(String[] args) {
main(args,1);
main(1,2);
}
}
4.Can we overload methods that differ only by static keyword?
ANS: NO
EX:
public
class
Test {
public
static
void
foo() {
System.out.println(
"Test.foo() called "
);
}
public
static
void
foo(
int
a) {
System.out.println(
"Test.foo(int) called "
);
}
public
void
foo(
int
a) { //GIVES COMPILE ERROR
System.out.println(
"Test.foo(int) called "
);
}
public
static
void
main(String args[])
{
Test.foo();
Test.foo(
10
);
}
}
5.Can we overload methods static methods?
ANS: YES
6.Explain Overload?
If a class have multiple methods by same name but different parameters, it is known as Method Overloading.
Advantage :
public void add(int a, int b){};
public void add(String a, int b, int c){};
6.Explain Override?
If subclass (child class) has the same method as declared in the parent class, it is known as method overriding.
Advantage :
Method overloading increases the readability of the program.
Different ways to overload the method
Different ways to overload the method
There are two ways to overload the method in java |
- By changing number of arguments
- By changing the data type
public void add(int a, int b){};
public void add(String a, int b, int c){};
6.Explain Override?
If subclass (child class) has the same method as declared in the parent class, it is known as method overriding.
Advantage :
- Method Overriding is used to provide specific implementation of a method that is already provided by its super class.
- Method Overriding is used for Runtime Polymorphism
Rules for Method Overriding
- method must have same name as in the parent class
- method must have same parameter as in the parent class.
- must be IS-A relationship (inheritance).
Criteria criteria = session.createCriteria(Employee.class);
criteria.add(Restrictions.like("manager", "Anurag"));
criteria.add(Restrictions.gt("salary", 1000));
Some more EX:
Criteria cr = session.createCriteria(Employee.class);
cr.add(Restrictions.gt("salary", 2000));
cr.add(Restrictions.lt("salary", 2000));
cr.add(Restrictions.like("firstName", "zara%"));
// Case sensitive form of the above restriction.
cr.add(Restrictions.ilike("firstName", "zara%"));
cr.add(Restrictions.between("salary", 1000, 2000));
// To check if the given property is null
cr.add(Restrictions.isNull("salary"));
// To check if the given property is not null
cr.add(Restrictions.isNotNull("salary"));
// To check if the given property is empty
cr.add(Restrictions.isEmpty("salary"));
// To check if the given property is not empty
cr.add(Restrictions.isNotEmpty("salary"));
8.How to print collection data using JSTL?
<%
String[] columnHeaders = {"Banana", "Apple", "Carrot", "Orange", "Lychee", "Permisson"};
request.setAttribute("columnHeaders", columnHeaders);
%>
<c:forEach var="columnHeader" items="${columnHeaders}">
<td>
<c:out value="${columnHeader}" />
</td>
</c:forEach>
9.Difference between ServletConfig and ServletContext?
Servlet
Config
|
Servlet
Context
|
Servlet config object represent single servlet
|
It represent whole web application running on particular JVM and common
for all the servlet
|
Its like local parameter associated with particular servlet
|
Its like global parameter associated with whole application
|
It’s a name value pair defined inside the servlet section of web.xml
file so it has servlet wide scope
|
ServletContext has application wide scope so
define outside of servlet tag in web.xml file.
|
getServletConfig() method is used to get the
config object
|
getServletContext() method is used to get the context object.
|
for example shopping cart of a user is a specific to particular user so
here we can use servlet config
|
To get the MIME type of a file or application session related
information is stored using servlet context object.
|
The ServletContext object is created per application (i.e. only one)
whereas ServletConfig object is created per servlet (i.e. if there are 4
servlets, there will be 4 ServletConfig objects).
Non-technical differences are:
You may get the instance of ServletContext by calling the getServletContext() method of GenericServlet class whereas getServletConfig() method of Servlet interface returns the instance of ServletConfig.
ServletConfig
1.ServletConfig available in javax.servlet.*; package
2.ServletConfig object is one per servlet class
3.destroyed once the servlet execution is completed.
4.We should give request explicitly, in order to create ServletConfig object for the first time
5.Object of ServletConfig will be created during initialization process of the servlet
6.This Config object is public to a particular servlet only
ServletContext
1.ServletContext available in javax.servlet.*; package
2.ServletContext object is global to entire web application
3.Object of ServletContext will be created at the time of web application deployment
4.Scope: As long as web application is executing, ServletContext object will be available, and 5.it will be destroyed once the application is removed from the server.
6.ServletContext object will be available even before giving the first request
Non-technical differences are:
You may get the instance of ServletContext by calling the getServletContext() method of GenericServlet class whereas getServletConfig() method of Servlet interface returns the instance of ServletConfig.
ServletConfig
1.ServletConfig available in javax.servlet.*; package
2.ServletConfig object is one per servlet class
3.destroyed once the servlet execution is completed.
4.We should give request explicitly, in order to create ServletConfig object for the first time
5.Object of ServletConfig will be created during initialization process of the servlet
6.This Config object is public to a particular servlet only
ServletContext
1.ServletContext available in javax.servlet.*; package
2.ServletContext object is global to entire web application
3.Object of ServletContext will be created at the time of web application deployment
4.Scope: As long as web application is executing, ServletContext object will be available, and 5.it will be destroyed once the application is removed from the server.
6.ServletContext object will be available even before giving the first request
10.Can we call destroy() method in service()?
Yes, again, you can call
destroy()
from within the service()
as it is also a method like any other. This can make sense sometimes, as destroy()
will do whatever logic you have defined (cleanup, remove attributes, etc.).Just bear in mind that simply calling
destroy()
won't won't unload the Servlet instance. You do not manage the life cycle of Servlets in the program, the Servlet Container does.11.
No comments:
Post a Comment