You are on page 1of 14

1) A servlet needs to acquire a data source through a JNDI naming lookup.

Which
of the following is the best place to do this?
A)constructor b)init c)service
2) If you dont want to make servlet multi-threaded implement
a)Runnable b)SingleThreadModel c)Servlet
3) How does one acquire the object implementing RequestDispatcher interface?
a) Using getRequestDispatcher() method of RequestDispatcher interface
b) Using getRequestDispatcher() method of ServletContext
interface
c) Using getRequestDispatcher() method of HttpSession
4) Once a Servlet is initialized, how do you get the initialization parameters ?
(a) Initialization parameters will not be stored
(b) They will be stored in instance variables
(c) using config.getInitParameters()
5) Which of the following is true ?
(a) Unlimited data transfer can be done using POST method
(b) Data is visible in Browser URL when using POST method
(c) When large amounts of data transfer is to be done, GET method is used.
6) Which of the following statements are true? [Check all correct answers]
A) Local variables are considered thread-safe.
B) Class variables are not considered thread-safe.
C) Class variables are considered thread-safe if the servlet
uses the single thread model.
D) Instance variables are considered thread-safe
if the servlet uses the single thread model.
7) Select all correct statements
a) forward is faster than sendRedirect
b) forwarding page can pass response to client
c) forwarded page can pass response to client
d) includer page can pass response to
client
e) included page can pass response to
client
8) The method gets executed as soon as servlet gets loaded in web server
a)destroy b)service c)init
9) A remote method must throw RemoteException
a)False
b) True
10) Return Type as well as Arguments of a remote method must be of Serialized Type
a)True b)False

11) In order to call stored procedure we use


a)CallableStatement b) Statement c) PreparedStatement
12) In order to load driver forName() method of following class is used
a)DriverManager b) Connection c) Class

13) The total no.of columns, their headings,column types etc. can be displayed with
the object of
a) ResultSet b)ResultSetMetaData c) DriverManager

14) This is a local proxy on client side in case of RMI


a)Stub b)Skeleton
15) RMI Server object can extend extend
a)RMISecurityManager
b)Remote
c) UnicastRemoteObject
16) Every business method should be declared as throws
a) SqlException
b) RemoteException
ClassCastException
17)

c)

_____________ class is used to connect to a Database.


A) Connection
B) DriverManager C)
PreparedStatement
18) ___________ is a thin Jdbc Driver.
A) Type 1 B) Type 2
C) Type 3
D) Type 4
19) More than one database commands can be sent to the database at a time using
_________.
A) execute() B) executeBatch() C) executeAll() D) executeUpdate().
20) By Default commit is off in java
A) False
B) True
21) Select one Correct statement.
A) PreparedStatement is a super interface of CallableStatement
B) CallableStatement is a super interface of PreparedStatement
C) PreparedStatement is a super interface for Statement
D) Statement is a super interface for CallableStatement.
22) Select one wrong statement
A) Cookies stores Text ,Session can store java Objects
B) we can send a response from the page where we include another web
resource
C) GenericServlet and HttpServlet ,both implement Serializable interface
D) we can send a response from the page,from where we forward request to
another web resource.

23) forward() is faster than sendRedirect()


A) True
B) False
24) What exception is thrown when Servlet initialization fails ?
(a) IOException
(b) ServletException
(c) RemoteException
25) Which is true about Servlets
(a) Only one instance of Servlet is created in memory
(b) Multi-Threading is used to service multiple requests
(c) Both (a) & (b)
26) Select all true :
A. HttpServletResponse provides a method to send a temporary redirect
response to the client.
B. HttpServletRequest provides a method to check if session state is
maintained using cookies.
C. HttpSession provides a method to unbind all the objects bounded to it, without
ending the session.
D. HttpServletContext provides a method to return a RequestDispatcher object for
a resource located in a particular path.
E. ServletConfig returns a reference to the corresponding user session.
27) Which interface of the Servlet API's defines methods to extract
form parameters from the HTTP request?
A) HttpServletRequest
B) ServletRequest
C) Servlet
D) HttpServletResponse
E) ServletResponse
28) If the Http Error 500 is generated by your servlet, you do not
want to show the Internal Server Error pages to the client,
Instead you want a custom error page to be displayed. What is
the best way to accomplish this ? (Select one correct answer)
A) Forward the user to the
error page using HttpServerResponse.sendRedirect method
B) Forward the user to the
error page using RequestDispatcher.forward method
C) Specify the mapping of the errorcode 500 and the error page in the deployment descriptor
D) It is not
possible to accomplish this

29) Which of the following can cause session to get destroyed ?


(select more than one correct ans)
A) Invoking an invalidate() method on the
HttpSession object
B) Invoking terminate() method on
the HttpSession object
C) Invoking setTimeout(0)
method on the HttpSession object
D) No request is
received from the client for the session timeout period
30) Which of the following statements are always true about the
SingleThreadModel interface ? (Select more than one correct
answers) A) It is a marker interface,which means it does not
have any methods
B) It provides a complete thread safety for
servlet
C) It does not provide thread safety for
static variables
D) implementing this interface ,causes all
the client requests to be service by the same servlet instance in
a synchronized manner
31) The method getWriter returns an object of type PrintWriter. This class has println
methods to generate output. Which of these classes define the getWriter method?
Select the one correct answer.
a. HttpServletRequest
b. HttpServletResponse
c. ServletConfig
d. ServletContext
32) Name the method defined in the HttpServletResponse class that
may be used to set the content type. Do not include parenthesis
after the method
setContentType

33) To send binary outptut in a response, the following method of


HttpServletResponse may be used to get the appropriate Writer/Stream object. Select
the one correct answer.
a) getStream b)getOutputStream
c)getBinaryStream
d)getWriter
(B. The getOutputStream method is used to get an output stream to
send binary data. The getWriter method is used to get a PrintWriter object that
can be used to send text data)
34) To send text outptut in a response, the following method of HttpServletResponse
may be used to get the appropriate Writer/Stream object. Select the one correct
answer.
a) getStream b)getOutputStream c)getBinaryStream
d)getWriter
35) Is the following statement true or false. URL rewriting may be used when a
cookie is disabled. In URL encoding the session id is included as part of the URL.
( true. The statement about URL encoding is correct)

36) Name the class that includes the getSession method that is used to get the
HttpSession object.
a)HttpServletRequest
b)HttpServletResponse
c)SessionContext
d)SessionConfig
37) Which of the following are correct statements. Select the two correct answers.
a) The getRequestDispatcher method of ServletContext class takes the
full path of the servlet, whereas the getRequestDispatcher method of
HttpServletRequest class takes the path of the servlet relative to the
ServletContext.
B)The include method defined in the RequestDispatcher class can
be used to access one servlet from another. But it can be invoked only if
no output has been sent to the server.
c)The getRequestDispatcher(String URL) is defined in both
ServletContext and HttpServletRequest method
d)The getNamedDispatcher(String) defined in HttpServletRequest
class takes the name of the servlet and returns an object of
RequestDispatcher class.
38) Which method would you override from the abstract class HttpServlet to handle
HTTP POST requests?
A) protected void doPost(HttpServletRequest req,
HttpServletResponse resp) b) public void doPost(HttpServletRequest req,
HttpServletResponse resp)
c) protected void doPost(ServletRequest req,
ServletResponse resp)
d) public void doPost(ServletRequest req,
ServletResponse resp)
e) None of the above
39) What will be the output when you compile and execute the following
servlet?
1.
public class PrinterServlet extends HttpServlet {
2.
protected void doGet(HttpServletRequest req,
HttpServletResponse resp)
3.
throws ServletException, IOException {
4.
PrintWriter pw = req.getWriter();
5.
pw.println ("New York");
6.
}
7.
}

a) Compilation succeeds, the output is "New York".


B) Compilation succeeds, no output is produced, the method flush() is not
used after line 5.
c) Compilation fails, the method getWriter() does not
exist in the HttpServletRequest interface.
d) Compilation fails, compile time error at line 5.

40) What statement do you need to insert at line 4 of the AnimalServlet to pick up all
selected animals in the form?
<form
action="/scwcd/servlet/com.baboon.scwcd.servletmodel.AnimalServlet"
method="POST">
<select name="animals" size="3" multiple>
<option value="TIGER">Tiger</option>
<option value="ELEPHANT">Elephant</option>
<option value="LION">Lion</option>
</select>
<input type="submit" value="Search">
</form>
1.
public class AnimalServlet extends HttpServlet {
2.
protected void doPost(HttpServletRequest req,
HttpServletResponse resp)
3.
throws ServletException, IOException {
4.
// insert code here
5.
}
6.
}

a) String animals= req.getParameter("animals");


b) String[] animals = req.getParameterValues("animals");
c) String[] animals = req.getParameterNames("animals");
d) none of the above

41) What would be the output in your browser when you invoke the servlet-code with the
following URL:
http://localhost/servlet/com.baboon.servletmodel.TestParamServlet?Param=10
1.
2.
3.
4.
5.
6.
7.
8.
9.

package com.baboon.servletmodel;
public class TestParamServlet extends HttpServlet {
protected void doPost(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException, IOException
{
resp.getWriter().println(req.getParameter("param"));
}
}

a)Code compiles, the output is a blank page with the value 10 printed out.
B) Code compiles, the container returns a status stating that doGet is not
supported.
C) Code compiles, the output is a blank page with null printed out.
D) Code failed compilation, exception IOException is not caught.
E) Code compiles, stacktrace is printed out in browser.

42) What is the output when you try to invoke the servlet defined below with the
following URL from the address line from your browser:
http://localhost/servlet/com.baboon.scwcd.servletmodel.TestParamServlet?Param=10
1.
package com.baboon.scwcd.servletmodel;
2.
public class TestParamServlet extends HttpServlet {
3.
protected void doGet(HttpServletRequest req,
HttpServletResponse resp)
4.
throws ServletException, IOException {
5.
resp.getWriter().println (req.getParameter("param"));
6.
}
7.
}

a) Code compiles, a blank page is displayed with the value 10 printed out.
B) Code compiles, a blank page with the value null is printed
out.
C) Code compiles, a blank page is displayed.
D) Code compiles, stack trace is printed out,
NullPointerException
e) None of the above

43) What would happen if the getSession(false) method of the interface


javax.servlet.http.HttpServletRequest is called and the request does not have a valid
session?
A) An InvalidStateException is thrown.
B) A NullPointerException is thrown.
C) A new session is created by the method and returned.
D) No new session is created by the method and null is returned.
44) Given the following extract from a web application's deployment descriptor, what is
the default duration that the servlet container will keep a session from being invalidated
between client accesses.
<session-config>
<session-timeout>0</session-timeout>
</session-config>

a) 0 minutes.
B) 0 seconds.
C) The session is never invalidated.
D) 30 minutes.
E) The default session timeout interval is container specific
45) Which type of event object is received by an object that implements
HttpSessionAttributeListener when an object is added to, removed from or replaced in a
session?
a) javax.servlet.http.HttpSessionEvent
b) javax.servlet.http.HttpSessionAttributeEvent
c) javax.servlet.http.HttpSessionBindingEvent
d)javax.servlet.http.HttpHttpSessionAttributeListener
e)javax.servlet.http.HttpSessionBindingListener

46) Which of the following methods in the interface


javax.servlet.http.HttpServletRequest can be used to obtain a session object? [Check all
correct answers]
a) getHttpSession()
b) getHttpSession(boolean create) c) getSession()
d) getSession(boolean create)
e) getRequestSession()
47) Which interface declares the setAttribute(String name, Object obj) method to allow
objects to be stored in a session?
a) javax.servlet.Session
b) javax.servlet.http.HttpSession
c)javax.servlet.http.ServletSession
d) javax.servlet.session.HttpSession
e)javax.servlet.session.ServletSession
48) Which method defined in the javax.servlet.http.HttpSession interface can be used to
expunge a session?
a) void destroy()
b) void invalidate()
c) void expunge() d) void kill()
e) void release()
49) Which of the following statements regarding the error-page element declared in the
deployment descriptor are true? [Check off correct answers]
a) Multiple error-page elements can be declared under the web-app element.
B) The error-exception element can be declared under the error-page
element.
C) Both error-code and exception-type can be declared together under the
same error-page element.
D)Either error-code or exception-type can be declared under the same errorpage element.
E) The value of the location element must have a leading '/'.
50) Given the following error pages configured in a web application's deployment
descriptor, which
of the following statements are true? [Check all correct answers]
<error-page>
<error-code>302</error-code>
<location>/wehavemoved.jsp</location>
</error-page>
<error-page>
<error-code>301</error-code>
<location>/wehavemoved.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<exception-type>java.lang.Throwable</exception-type>
<location>/errorhandler.jsp</location>
</error-page>

a) More than one error page declaration can be configured in the web application's
deployment descriptor.
B) Error page declarations with different error codes can be associated to the

same location.
C) wehavemoved.jsp is called by the servlet container if a servlet invokes
setStatus(301) on a response object.
d) errorhandler.jsp is called by the servlet container if either an internal error is
encountered by the servlet container or a java.lang.Throwable exception is propagrated to
the container.
E) The value of the location must have a leading '/'.

51) Given the following fragment of code and


com.pleasanthotel.RoomUnavailableException extends javax.servlet.ServletException,
which error-page mappings can be defined in the web.xml to handle a
RoomUnavailableException?
import
import
import
import

java.io.IOException;
javax.servlet.*;
javax.servlet.http.*;
com.pleasanthotel.RoomUnavailableException;

public class ReservationServlet extends HttpServlet {


private void reserveRoom() throws RoomUnavailableException {
// code to reserve a room here
}
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
// make reservation here
reserveRoom(roomNumber);
}
}

a) <error-page>
<error-code>500</error-code>
<location>/intervalservererrorhandler.jsp</location>
</error-page>
b) <error-page>
<exception-type>javax.servlet.ServletException</exception-type>
<location>/servletexceptionhandler.jsp</location>
</error-page>
c) <error-page>
<exception-type>javax.servlet.ServletException</exception-type>
<location>servletexceptionhandler.jsp</location>
</error-page>
d) <error-page>
<exception-type>com.pleasanthotel.RoomUnavailableException</exception-type>
<location>/roomunavailableexceptionhandler.jsp</location>
</error-page>

e) <error-page>
<exception-type>com.pleasanthotel.RoomUnavailableException</exception-type>
<location>roomunavailableexceptionhandler.jsp</location>
</error-page>
52) Select the correct statements about the following code. [Check all
correct answers]
1.
import javax.servlet.http.*;
2.
public class RainbowServlet extends HttpServlet {
3.
private int numColours;
4.
public void doGet(HttpServletRequest req, HttpServletResponse
res) {
5.
String outerColour = "Red";
6.
}
7.
}

a) The servlet uses the single threaded model.


B) The variable numColours is thread-safe.
C) The variable outerColour is thread-safe.
D) The servlet uses the multi-threaded model.
E) Multiple threads can execute the doGet
method concurrently
53) Which interface is used to declare that a servlet must use the single thread model?
a) javax.servlet.isThreadSafe
c)javax.servlet.SingleThreadModel
e)javax.servlet.SynchronizedServlet

b) javax.servlet.SingleThreaded
d) javax.servlet.ThreadSafe

54) How is a servlet declared to use the single thread model?


a)The servlet declares the service method as synchronized.
B) The servlet implements the interface SingleThreadModel
c) In the deployment descriptor.
D) Nothing is required, by default a servlet uses the single thread model.
E) The declaration is vendor specific
55) Which of the following attribute scopes are thread-safe? [Check all correct answers]
a) Request attribute.
B) Response attribute.
C) Session attribute.
D) Context attribute.
E) None of the above.
56) Which method returns all the names of the context's initialisation parameters? The
method returns an Enumeration of String objects.
a) ServletContext.getInitParameterNames()
b)ServletContext.getAttribute(java.lang.String name)
c)ServletContext.getAttributeNames()
d)ServletContext.getInitParameter(java.lang.String name)

e) ServletConfig.getInitParameterNames()
57) Which method can you use to obtain parameters mentioned in the deployment
descriptor of your web-application?
a) String getInitParameter(String name);
b) String getParameter(String name);
c) String getInitAttribute(String name);
d) String getAttribute(String name);
e) String getResource(String name);
58) Type 4 database driver, also known as thin driver
1. True
2. False
59) Which one of the following is the correct sequence of opening a result set.
1. Create Statement, create connection, open result set.
2. Create Connection, create statement, open result set
3. Load driver, open connection, create statement, open result set.
4. All of the above.
60) Which interface defines the following method in the servlet API?
public void service (ServletRequest request, ServletResponse response)
1. javax.servlet.Servlet
2. javax.servlet.GenericServlet
3. javax.servlet.HttpServlet
4. javax.servlet.http.HttpSession
61) Read the following code segment
<form action="/scwcd/servlet/com.baboon.scwcd.servletmodel.AnimalServlet" method="POST">
<select name="animals" size="3" multiple>
<option value="TIGER">Tiger</option>
<option value="ELEPHANT"> Elephant
</option>
<option value="LION">Lion</option>
</select>
<input type="submit" value="Search">
</form>
1. public class AnimalServlet extends HttpServlet {
2. protected void doPost(HttpServletRequest req, HttpServletResponse resp)
3. throws ServletException, IOException {
4. // insert code here
5. }
6. }
What statement do you need to insert at line 4 of the AnimalServlet to pick up all selected animals in
the form?
1. String animals= req.getParameter("animals");
2. String[] animals = req.getParameterValues("animals");
3. String[] animals = req.getParameterNames("animals");
4. None of the above

62) Which of the following actions will cause a session to be expunged( wiped out) in a web
application?
1. Navigating to a different web site or web application
2. Closing the web browser.
3. The client does not send a request to the web application for a length of time that exceeds
the session timeout value of the associated session object in the application.
4. All of the above.
63) You have created a Listener called AccountApplicationListener that receives events when the
context of the web-application "account" is created or destroyed. How would you define this in
your deployment descriptor?
1. <listener>
com.cdac.acts.AccountApplicationListener
</listener>
2. <listener>
<listener-class>com.cdac.acts.AccountApplicationListener </listener-class>
</listener>
3. <listener-class>com.cdac.acts.AccountApplicationListener</listener-class>
4. <web-app name="account">
<listener-class>com.cdac.acts.AccountApplicationListener</listener-class>
</web-app>

64) Which interface of the Servlet API's defines methods to extract form parameters from the HTTP
request?
1. HttpServletRequest
2. ServletRequest
3. Servlet
4. HttpServletResponse
65) Which statement can be inserted in a Servlet to forward the request to http://www.cdac.in, outside
the current domain?
1. resp.encodeRedirectURL("http://www.cdac.in");
2. resp.sendRedirect(resp.encodeRedirectURL("http://www.cdac.in "));
3. resp.sendRedirect(HttpServletResponse.SC_MOVED_PERMANENTLY, " http://www.cdac.in ");
4. getServletContext().getRequestDispatcher("http://www.cdac.in).forward(req,resp);
66) A _______ object is used to submit a query to database.
1. Connection
2. ResultSet
3. Statement
4. Any of the above
67) ________ allows Java Objects running on separate computer to communicate with one another
via remote method calls.
1. RMI
2. Threads
3. Tag libraries
4. None of the above

68) You are calling the method HttpServletResponse.sendRedirect(String path) and you want to make
sure that the user continues in the same session. How do you do that?
1. By calling the method with an absolute path as the argument
2. By enconding the path with HttpServletResponse.endcodeURL method
3. By enconding the path with HttpServletResponse.endcodeRedirectURL method

4. If you use sendRedirect the session is lost.


69) Session information can be stored on client, when the cookies on the client are
1. Enabled
2. Disabled
3. All of above
4. None of the above

70) rmiregistry by default listens on port :


1. 1051
2. 80
3. 1099
4. 1024

71) Exception thrown by the remote method is


1. ClassNotFoundException
2. StubCannotBeCreatedException
3. RemoteException
4. RemoteServerException
72) Which code segment could execute the stored procedure "countRecs()" located in a database
server?
1. PreparedStatement pstmt = connection.prepareStatement("countRecs()");
pstmt.execute();
2. Statement stmt = connection.createStatement();
stmt.executeStoredProcedure("countRecs()");
3. Statement stmt =
connection.createStatement();
stmt.execute("COUNTRECS()");
4. CallableStatement cs = con.prepareCall("{call COUNTRECS}");
cs.executeQuery();

73) What is Java serialization?

1. Distributed Application.
2. The ability to examine the encapsulated data of a class.
3. Remote method invocation.
4. The ability transform the state of an object into bits and resurrect a copy of the object from
those bits.
74) By using Java System Property, one can load the driver into the memory.
1. True
2. False

You might also like