Академический Документы
Профессиональный Документы
Культура Документы
Checked exceptions are also known as compile time exceptions as they are identified
during compile time of the program. Exception and all the subclasses of Exception
class are checked exceptions
b) There are 2 types of exception in Java (1) check like an network failure or any
database problem showes this types of excepton.(2 ) Uncheked exception that is at
run time exceptions.
a) forward will just forward the request to next page where as sendRedirect will first
come back to the page where its been generated and the redirect to next page
b) forward will just forward the request to next page where as sendRedirect will first
come back to the page where its been generated and the redirect to next page
d) When u Goes in to the forward() , here request and response may not be available
to u. i,e u cannot say req.getParameter().
1
Interview Questions: JSP
<%@ page
[ language="java" ]
[ extends="package.class" ]
[ import="{package.class | package.*}, ..." ]
[ session="true | false" ]
[ buffer="none | 8kb | sizekb" ]
[ autoFlush="true | false" ]
[ isThreadSafe="true | false" ]
[ info="text" ]
[ errorPage="relativeURL" ]
[ contentType="mimeType [ ;charset=characterSet ]" | "text/html ;
charset=ISO-8859-1" ]
[ isErrorPage="true | false" ]
%>
c) In "Page" directive you have "extends" attribute which allows you to extend you
class by jsp.
They why i can not implement my interface......??????
First solution:: i can do it by implementing my interface by my class and then i
can use "Page" directive which have "extends" attribute which allows me to extend
my class by jsp.
uri provides only the name of the resource, like in web.xml, in url-pattern you would
be specifying the uri for identification of the servelt when the request is made in the
url.
url gives the path of the resource where it was located.url identifies the path where
the uri is located.
2
Interview Questions: JSP
a) Even though we call a jsp page from servlet through request dispatcher,we can
not call a perticular method of a jsp from servlet.there is no such mechanism in
servlet API.but iam not sure.
reqDis.forward(request, response);
You can get RequestDispatcher from ServletConetext object, then you should give
the path to resouce with reference to context path, as
RequestDispatcher reqDis =
getServletContext().getRequestDispatcher("/Jspfile.jsp");
Thread Independent:
Garbage Independent:
ex : String s="abc";// here two objects are created and one reference s
c) If your value is not going to change and if u don't want to multiple threads to
access...then strings come into picture
3
Interview Questions: JSP
d) Immutable means not changeable. when a string is declared and initiated with
some values.
Afterwards whenever we will concat that string or change that string. if we r going to
add more characters in that string object. it will not allow dynamic size change. to
cater such problem when any change will occur in string object the object will be
recreated and previous object will be destroyed and new will be created with new
size to accomodate change. thats y strings are immutable in java.
String s1 = "abc";
String s1 = "abc"; //here both s1 , s2 point to the same object abc,rather than 2
different objects with same value.
Very easy, just define a class that implement the Printable interface and then import
that class and use the print() method.
public int print(Graphics gr, PageFormat pf, int pageIndex) throws Exception {
Using Abstract class we can take n number of instances. Like using the Blueprint of a
Car
we can take n number of similar cars. But we cannot use the Bluprint or model to
drive the
car.That is the meaning of abstract. So, Car is the Blueprint and Ford Car is the
4
Interview Questions: JSP
So we find that frog uses both methods hopping and swimming. So it is better to
declare
in some interface so that any frog which implements the interface will define its own
way of transportation.
So let me create an Interface called Amphibian and give two method Swimming()
and hopping().
It looks like
The above class Frog when implements the interface Amphibian it has to define the
methods
declared in the interface Amphibian. It can be an empty method. But it should be
defined.
The use of an interface is any class which wants to use some remote functionality
can
implement that particular interface and can perform the task like man using
Parachute to
fly in air.
5
Interview Questions: JSP
What is ClassLoader?
Class loaders are one of the cornerstones of the Java virtual machine (JVM)
architecture. They enable the JVM to load classes without knowing anything about
the underlying file system semantics, and they allow applications to dynamically load
Java classes as extension modules.A class loader is an object that is responsible for
loading classes. The class ClassLoader is an abstract class. Given the name of a
class, a class loader should attempt to locate or generate data that constitutes a
definition for the class. A typical strategy is to transform the name into a file name
and then read a "class file" of that name from a file system.
a) the wait() method is defined in the java.lang.OBject() class and not in the
java.lang.Thread()class. it's signature is public static void wait(). we do have some
overloaded versions of the wait() method. when an wait method is called by an
thread on a object it release the lock on the object and goes to an WAIT/BLOCKED
state inorder to let the other threads to enter the RUNNING state. However it regains
the Lock on the object after sometime when it moves from the WAIT/BLOCKED state
to RUNNABLE state . that can happen due to the call of notify(),notifyAll() methods
by other threads holding of the respective objects Lock.
Question: Is there other way to prevent next thread to access the resource
with out using "synchronized" blocks?
a)singleton class is used to create a single object for a class and is most secured
class, but it do not support siglethreaded model
you can block the next thread without using sinchronize method by implementing
single threaded model interface.
a) We can call the destroy() method. But it works like normal method, for example, if
u have one method in ur servlet class like show(), it acts like show() method.
Whenever we click on the stop link in manager page of that particular application,
then only application will be stopped and it calls the destory method. At that time
only the application objects permanently destroyed. Sample code: Try it out..
6
Interview Questions: JSP
System.out.println("hello in init"); }
PrintWriter out=response.getWriter();
b) Yes, we can call destroy() method from service() method but it will be treated as
any other normal method rather lifecycle method. Also destroy() method called from
service() method can do any deinitialization task etc but the servlet container will not
unload the servlet, that can only be done by the lifecycle destroy() method.
c) Destroy method is a method where we can provide the actions (to free up the
resources)Â to be performed while the servlet is getting unloaded. In general when a
servlet is getting destroyed the servlet container calls this method and also does
many other things. So if you want to unload a servlet you have to do all the things
the servlet container does including calling the servlet method.
When you directly call the destroy method from service method the method will be
successfully called as you are calling the method from same object but you are not
doing all the other things the servlet does to unload the servlet. So the servlet will
not be unloaded but the destroy method will be called as any other non life cycle
method (like display()).
Question: When we increase the buffer size in our project using page
directive attribute ‘buffer’ what changes we observe?
a) The effect will be only that the caching of pages in the memory will increase and if
it is increase upto much extends, it will consume the more memory of the system. It
may increase or decrease the performance of an application.
b) the default buffer size is 8k and when we increase the buffer size of the page
using the buffer attribute of the page directive then the storage capacity will be
increased .if the buffer size is too large then the performance will degrade.
instead of increasing the buffer size use autoFlush=true ,bydefault the autoFlush is
true only.so even the content i.e to be send to the client crosses
a) It is a java class only but it does not support Constructor becuase Servlet is
loaded by servlet container not through normally we initilized the java class
7
Interview Questions: JSP
b) If there is no constructore in the Servlet , then it does not mean that we can not
write the constructore in the servlet. Offcourse we can write. As we know that the
constructore only instantiate the normal java class and servlet is not a notmal java
class. To make a object as servlet, a lot of properties has to be defined. and this
property comes by init() method. so, writing the constructore in servlet is worthless.
c) First of all Servlet is not a Java class. Its an interface, interface does not have
constructors. And java servlet container implements servlet creating respective
servlet classes which are mapped in deployment discriptor.
init method performs the task on normal constructor in servlets as its called by the
actual class taht implements servlet in the container run time by calling the init
method.
d) Servlet is an interface!!!!
a) when we call forName() method on Class class what happen is1)initially the class
is loaded into the memory2)then it calls the static method forName()3)the static
forName() method contains a static block.That static block regiser the loaded driver
class with the DriverManager class
b) it is used to load the class at runtime and in jdbc it is used to load the driver and
according to specification every driver class must contain a static block in which it
contains the code to instanciate itself and the register itself with the driver manager.
Ans):if a composite object becomes unreachable, then its constituent objects also
become unreachable, barring any reachable references to the constituent objects.
ex:objects o1, o2, and o3 form a circular list(object o1 have reference to o2,object
o2 have reference to o3 and object o3 have reference to o1) , and they do not have
any reachable references.These objects are called "object iceland". Thus, these
objects are all eligible.
b) Instance ation will not allocate memory, just a instance will be created.
8
Interview Questions: JSP
The reference variable is that one where we r not using the new keyword e.g.
Connection con; But Object is that where we r using new operatore.g Classname
cs1=new classname
Question: why the StringBuffer and the wrapper classes defined as final ?
A class can be declared final if its definition is complete and no subclasses are
desired or required.they are handled in different way by jvm at runtime. so both the
classes are marked as final
b) By defining a class as final we are restricting the one of the main feature of OOP
ie., INHERITANCE. But JavaSoft(SUN) has provided this restricting feature because
Java Developers thought that some methods cannot be modified by the users. Eg.
for those are the methods in String , StringBuffrer ,StringBuilder , all Wrapper
classes. As String is a standard class those methods should be as they are as
developed by Sun. The user can not be permitted to change the functionality of those
methods. If your application needs this type of situation u can decalre your class as
final.
The user can't implement the method toUppercase( ) in String class as that it can
convert only some of the characters into Uppercase.
a) when we are using thows when exception coms thatperticular exceptions handle
by JVM.
like:
try {
//..
}catch(xxException e) {
//..
throw new xxxException("xxx");
}catch(Excepiton ee) {
//..
}finally {
try {
//..
}catch(xxException eee) {
//..
}
}
9
Interview Questions: JSP
You use the Transient keyword to indicate to the Java virtual machine that the
indicated variable is not part of the persistent state of the object.
a) Servlet context is used for the persistency of the data for the servlets within the
same web application or within the same servlet engine.
So a particular data is common for all servlets sharing the same web application.
Whereas the servlet config is used to use a particular data for each servlet.
Other servlet in the web application cannot access the data obtained by servlet
config of other user. Servlet config is used to access the data for itself. Other servlet
cant access others servlet config data.
Servlet context is used to communicated within web container. There is one servlet
context per Application per JVM. Servlet Context is part of ServletConfig.
ServletContext = ServletConfig.getServletContext()
rRequest dispatcher whis communciate dirctecly communicate with data which page
destination page .
10
Interview Questions: JSP
example:
ServletContext sc=getServletContext();
RequestDipatcher rc =sc.getRequestDispatcher("1.jsp");
rc.forward(req,res);
here u'r
senRedirect() this method which time delay control goes to browser and it again go
to destination page.that is the difference b/e sendRedirect and Requestdispatcher
a. int a =5;
b. new Object();
c. new Integer(“5”);
d. new Character(‘C’);
Question: what will happen when u attempt to compile and run the below
code.
a.0
11
Interview Questions: JSP
b.1
c. compilation fails
d. 0 to Ineteger.MAX_VALUE
a. boolean b =0;
b. boolean b = Boolean.TRUE;
c. boolean b = true;
d. boolean b = (3>=0);
class A
Void get()
Sysout(“A”);
Class B extends A
Void get()
Sysout(“B”);
12
Interview Questions: JSP
A a = new A();
B b = (B) a;
b.get();
a. compilation fails
c. A
d. B
6. Given:
package test1;
13
Interview Questions: JSP
package test2;
System.out.println(“x = “ + x);
A. x = 0
B. x = 42
7.
class TestSuper {
TestSuper(int i) { }
class TestAll {
14
Interview Questions: JSP
new TestSub();
Which is true?
A. Compilation fails.
8. O/P?
String s =”abcde”;
If(s.equals(obj))
{
Sysout(“Equal 1”);
}
If(obj.equals(s))
Sysout(“Equal 2”);
a. compilation fails
b. runtime exception
c. Equal 1 Equal 2
9. When is the Float object, created in line 11, eligible for garbage
collection?
15
Interview Questions: JSP
oa[0] = o;
o = null;
oa[0] = null;
print 'return
int x = 0;
System.out.println(“finished”);
A. finished
B. Compilation fails.
C. An AssertionError is thrown.
16
Interview Questions: JSP
a) 1. 0
3. b
4. compliation fails (a)
5. only A
6. b
7. b.
8.c
10. B
17