0% found this document useful (0 votes)
103 views

C) You Are User No 3 To Visit This Site

The document contains 30 multiple choice questions about servlets. Some key topics covered include the servlet lifecycle methods, accessing servlet context and request parameters, HTTP request methods like GET and POST, and using sessions and cookies.

Uploaded by

anand_raman10
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views

C) You Are User No 3 To Visit This Site

The document contains 30 multiple choice questions about servlets. Some key topics covered include the servlet lifecycle methods, accessing servlet context and request parameters, HTTP request methods like GET and POST, and using sessions and cookies.

Uploaded by

anand_raman10
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 12

Servlets Questions

1) What is the output of the following code if the user types the url as
http://localhost:8080/emp_details/servlet/EmployeeDetails and browser is refreshed
three times?
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class EmployeeDetails extends HttpServlet


{
static int i;
PrintWriter out;
public void init()
{
i = 0;
out=null;
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
i++;
out=response.getWriter();

out.println("<B>You are user no. " + i +" to visit this


site.</B><BR><BR>");
}

}
a) ("<B>You are user no. " + i +" to visit this site.</B><BR><BR>”);
b) You are user no 1 to visit this site
c) You are user no 3 to visit this site
d) Exception is shown

2) what is the proper syntax for retrieving the context of servlet


a) public void init(ServletContext cfig)
{
ServletContext ctx;
ctx = cfig.getServletContext();
}

b) public void init(ServletConfig cfig)


{
ServletContext ctx;
ctx = getServletContext();
}
c) public void init(ServletConfig cfig)
{
ServletContext ctx;
ctx = cfig.getServletContext();
}
d) public void init(ServletConfig cfig)
{
ServletContext ctx;
cfig = ctx.getServletContext();
}

3) which of these is not a life cycle method of servlet


a) ServletInitialized()
b) Service()
c) Destroy()
d) Init()

4) ------ is the approach to keep only a few connections that are shared among data
access logic elements
a) Connection pooling
b) DAO pattern
c) Struts framework
d) Filtering of servlets

5) Which of the following is the correct syntax for locating a datasource using JNDI
a) Context c= InitialContext();
If(c==null)
{ throw new RuntimeException(“JNDI context could not be found”); }
ds=(DataSource) c.lookup(“java:comp/env/jdbc/myDB”);
If(ds==null)
{ throw new RuntimeException(“DataSource could not be found”);}
b) Context c=new InitialContext();
If(c==null)
{ throw new RuntimeException(“JNDI context could not be found”); }
ds=(DataSource)lookup(“java:comp/env/jdbc/myDB”);
If(ds==null)
{ throw new RuntimeException(“DataSource could not be found”);}
c) Context c=new InitialContext();
If(c==null)
{ throw new RuntimeException(“JNDI context could not be found”); }
ds= c (“java:comp/env/jdbc/myDB”);
If(ds==null)
{ throw new RuntimeException(“DataSource could not be found”);}
d) Context c=new InitialContext();
If(c==null)
{ throw new RuntimeException(“JNDI context could not be found”); }
ds=(DataSource) c.lookup(“java:comp/env/jdbc/myDB”);
If(ds==null)
{ throw new RuntimeException(“DataSource could not be found”);}

6) which of these is not an advantage of DAO pattern


a) business logic and data access logic are separate
b) developers writing other servlets can reuse the same data access code
c) using DAO pattern you can change web tier compoenets easily without
impatcting the existing data access logic.
d) The DAO pattern emphasizes code reuse by encapsulating the data access
logic in one location the DAO interface

7) which is the correct syntax for configuring a datasource and JNDI in web.xml
file?
a) <resource-ref>
<description>
This defines a JNDI resource reference for
java:comp/env/jdbc/leagueDB DataSource which
is formally declared in the domain.xml.
</description>
<res-ref-name>jdbc/leagueDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
b) <resource-ref>
<description>
This defines a JNDI resource reference for
java:comp/env/jdbc/leagueDB DataSource which
is formally declared in the domain.xml.
<res-ref-name>jdbc/leagueDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</description>
c) <resource-ref>
<description>
This defines a JNDI resource reference for
java:comp/env/jdbc/leagueDB DataSource which
is formally declared in the domain.xml.
</description>
<res-ref-name>jdbc/leagueDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
</resource-ref>
d) <resource-ref>
<description>
This defines a JNDI resource reference for
java:comp/env/jdbc/leagueDB DataSource which
is formally declared in the domain.xml.
</description>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

8) which of these is not an operation performed by filters


a) blocking access to a resource based on user identity or role membership
b) non-transformation of the response
c) auditing incoming requests
d) logging servlet performance

9) when filter is called which of the following method is called by container in turn?
a) init()
b) doFilter()
c) destroy()
d) none of the above

10) Which method is invoked only once in the life cycle of a servlet?
a. init()
b. doPost()
c. service()
d. doGet()

11) Identify the correct feature of servlets.


a. Servlets are client-side components that generates dynamic
contents.
b. Servlets by default can service only one client request at a time.
c. Servlets are platform dependent.
d. Servlets can service multiple client requests.

12) Identify the method called by the Web container after creating a servlet
instance.
a. init()
b. service()
c. start()
d. destroy()

13) Which method is automatically invoked by the Web container when a


request arrives?
a. init()
b. service()
c. doPost()
d. doGet()

14) Identify the method that sends user information as query string
appended to the URL.
a. GET
b. POST
c. HEAD
d. DELETE

15) Which of the following method does the container calls to allow a servlet to release
all resources it is holding?
a. service()
b. destroy()
c. init()
d. doGet()

16) . Which of the following statement is true?


a. The init() method calls the doGet() method
b. The doPost() method calls the doGet() method
c. The container directly calls the doGet() method
d. The service() method calls doGet() method

17) Which of the following element of the deployment descriptor specifies the
name of the servlet class file?
a. <servlet>
b. <servlet-class>
c. <servlet-name>
d. <display-name>

18) Which of the following is a feature of the GET method?


a. Handles most request types including file uploading and form submissions
b. Data is passed in the request body
c. Data is passed as query string appended to the URL
d. There is no limit to the amount of data that can be passed

19) What is the use of setContentType() method of ServletResponse interface?


a. To set the MIME type associated with the request
b. To get the MIME type associated with the request
c. To set the MIME type associated with the response
d. To get the MIME type associated with the response

20) A servlet, TestServlet is deployed in the J2EE application server installed in your
computer with the context root as testctx. Which of the following URL will
you specify to access the servlet?
a. http://localhost:8080/testctx/TestServlet
b. http://localhost:8080/testctx/servlet/TestServlet
c. http://localhost:8080/testctx/servlet/TestServlet.class
d. http://localhost:8080/servlet/testctx/TestServlet

21) You need to create a listener class to receive notification from the container when the
servlet context is initialized. Which of the following interface will you
implement in your listener class to receive the notification?
a. ServletContextListener.
b. ServletContextAtributeListener.
c. ServletContext.
d. Servlet.

22) Which of the following object does the container pass to the init() method of
the servlet?
a. ServletContext.
b. ServletRequest
c. ServletConfig
d. ServletResponse

23) Identify the sequence of servlet methods that are invoked when a servlet is accessed
for the first time?
a. init(), doPost(), service(), destroy()
b. service(), init(), destroy()
c. init(), doGet(), service(), destroy()
d. init(), service(), doGet()/doPost(), destroy()

24) Which of the following statement regarding the HTTP POST method is true?
a. Allows sending unlimited length of data as a part of HTTP request body.
b. Allows creation of bookmarks.
c. Uses simple query strings.
d. POST is the default method unless explicitly specified.

25) The getRemoteAddr() method of the ServletRequest interface returns a String that
represents:
a. The name of the computer from which the request is sent.
b. The IP address of the computer from which the request is sent.
c. The name of the computer, where the servlet is running.
d. The IP address of the computer where the servlet is running.

26) Which interface is used to log messages to the application server log file?
a. HttpServletRequest
b. ServletResponse
c. HttpSession
d. ServletContext

27) The getAttribute(String name) method of the HttpSession interface returns:


a. The name of the object bound to the session as an Object.
b. The Object bound with the name in the session.
c. The name of the object bound to the session as a String.
d. The String representation of the Object bound with the name in the session.

28) Which event is generated when there is any change in the attribute of the session
object?
a. javax.servlet.http.HttpSessionEvent
b. javax.servlet.http.HttpSessionActivationEvent
c. javax.servlet.http.HttpSessionAttributeEvent
d. javax.servlet.http.HttpSessionBindingEvent

29. Which method is defined in the HttpSessionListener interface?


a. sessionCreated()
b. sessionDidActivate()
c. sessionWillPassivate()
d. attributeAdded()

30. Identify the correct statement.


a) Cookies are created on the server and stored by the browser on the client
machine.
b) Cookies are created and stored on the server.
c) Cookies are created by the browser and stored by the server on the client machine.
d) Cookies are created and stored by the browser on the client machine.

31. The setMaxAge(int expiry) method defined in the Cookie class sets:
a) The maximum age of cookies in milliseconds
b) The maximum age of cookies in seconds
c) The maximum age of cookies in minutes
d) The maximum age of cookies in hours

32. The setMaxInactiveInterval(int interval) method of the


HttpSession interface sets:
a) The maximum age of the session in seconds.
b) The maximum age of the session in milliseconds.
c) The maximum time for which a client can remain inactive before the session is
invalidated in seconds.
d) The maximum time for which a client can remain inactive before the session is
invalidated in milliseconds.

33. Which one of the following is not a technique to maintain the state of an end user
visiting multiple pages of a Web site?
a. Cookies
b. Hidden Form Fields
c. Servlet Session APIs
d. Request Dispatcher
34Which session management technique does not transfer session information between a
client and server?
a. URL rewriting
b. Cookies
c. Servlet Session API
d. Hidden Fields

35. select the correct option that specifies the two exception classes that handle servlet-
related exceptions:
a. IOException, ServletException
b. ServletException, UnavailableException
c. UnavailableException, IOException
d. IOException, EOFException

36. Name the class that includes the getSession() method that is used to get the
HttpSession object.
a. HttpServletRequest
b. HttpServletResponse
c. ServletContext
d. ServletConfig

37. Which of the following interfaces can you implement in your servlet to ensure that
your servlet instance handles only a single request at a time:
a. HttpServlet
b. GenericServlet
c. SingleThreadModel
d. Runnable

38. Select the correct code snippet that demonstrates retrieving a value, user from a
Cookie object.
a. Cookie ck[] = req.getCookies();
If (ck!=null)
{
for (int i=0; i<ck.length; i++)
{
if (ck[i].getName().equals("user"))
username = ck[i].getValue();
}}
b. Cookie ck = req.getCookies();
if (ck!=null)
{
for (int i=0; i<ck.length; i++)
{
if (ck[i].getName().equals("user"))
username = ck[i].getValue();
}
}
c. Cookie ck[] = req.getCookie();
if (ck!=null)
{
for (int i=0; i<ck.length; i++)
{
if (ck[i].getName().equals("user"))
username = ck[i].getValue();
}
}
d. Cookie ck = req.getCookies();
if (ck!=null)
{
for (int i=0; i<ck.length; i++)
{
if (ck[i].getCookieName().equals("user"))
username = ck[i].getCookieValue();
}
}

39. Which of the following deployment descriptor elements specifies the session
timeout value to 30 minutes:
a. <session-timeout>30</session-timeout>
b. <session-timeout>30*60</session-timeout>
c. <session-timeout>30*60*60</session-timeout>
d. <session-timeout>30*1000<session-timeout>

40. Consider the following code snippet:


RequestDispatcher
dispatch=getServletContext().getRequestDispatcher("/servlet/CabInformation");
dispatch.forward(request);
Select the correct option that describes the preceding code snippet:
a. Code snippet will not compile as the getRequestDispatcher() method does not take
parameters
b. Code snippet will not compile as the forward() method takes both request and
response objects as parameters
c. Code snippet will not compile as
getServletContext().getRequestDispatcher
("/servlet/CabInformation”) returns a ServletContext and not a RequestDispatcher object
d. Code will compile without any error

41. Identify the correct statement.


a) The Request Dispatcher object can only be used to include the output of another
document.
b) The Request Dispatcher object can only be used to forward the request to another
document.
c) The Request Dispatcher object can be used to forward the request to another
document or include the output of another document.
d) The Request Dispatcher object can neither be used to forward the request to another
document nor can be used to include the output of another document.

42. Identify the servlet attribute that is inherently thread safe.


a) Local variables
b) Class variables
c) Context attributes
d) Session attributes

43. Which method is not included in the life cycle method of a Servlet filter?
a) init()
b) start()
c) doFilter()
d) destroy()

44. Identify the false statement.


a) A servlet filter is an object that intercepts the requests and responses that flow between
a client and a servlet.
b) A servlet filter can modify the headers and contents of a request coming from a Web
client and forward it to the target servlet.
c) A servlet filter can also intercept and manipulate the headers and contents of the
response that the servlet sends back.
d) A servlet filter can process multiple client requests using a single servlet filter
instance.

45. Consider the following statements:


Statement A: The include() method of RequestDespatcher class is used to include the
output of another document.
Statement B: The forward() method of RequestDespatcher class is used to forward the
output to another document.
Identify the correct option.
a) Both, statements A and B are true
b) Statement A is true and B is false
c) Statement A is false and B is true
d) Both, statements A and B are false

46. Which is the correct syntax for doFilter method


a) public void doFilter(ServletRequest sr,ServletResponse res,FilterChain ch)
b) private Filter doFilter(ServletRequest sr,ServletResponse res,FilterChain ch)
c) private void doFilter(ServletRequest sr,ServletResponse res,FilterChain ch)
d) public Filter doFilter(ServletRequest sr,ServletResponse res,FilterChain ch)
47) which is the return type of execute method of Action class
a) Action
b) ActionForward
c) void
d) ActionForm

48)

You might also like