Ajp MCQ Chapter 6
Ajp MCQ Chapter 6
Ajp MCQ Chapter 6
5)The server calls __________ method to relinquish any resources, such as files handles that are allocated for
servlet.
a)service() b)init() c)destroy() d) stop()
9)Which package contains the classes and interfaces required to build servlet?
a)import javax.servlet.*; b) import javax.servlet.http.*; c)both a&b d) None of these
10)The _______ class provides functionality that makes it easy to handle requests and responses.
a)Generic Servlet b)ServletInputStream c)ServletOutputStream d) None of these
11) The call to setContentType() establishes the MIME type of the HTTP response.
a) True b) False
12) servletrunner utility tool listens on ______ port for incoming client requests.
a) 80 b)21 c) 53 d)8080
13)Which of the following interface declares the lifecycle method for servlet.
a)Servlet b)Servlet Config c) ServletContext d) ServletResponse
15) ServletContext interface enables servlets to log events and access information about their environment.
a) True b) False
19) Which of the following class provides an input stream for reading requests from a client
a) ServletException b)GenericServlet c)ServletOutputStream d) ServletInputStream
20) Which of the following class provides an output stream for writing response to the client
a) ServletException b)GenericServlet c)ServletOutputStream d) ServletInputStream
22) Which of the following interface allows a servlet to obtain configuration data when it is loaded
a) ServletConfig b) ServletContext c) ServletRequest d)None of these
24) Which of the following method returns the value of the initialization parameter named param.
a) ServletContextgetServletContext() b) String getInitParameter(String param)
c) Enumeration getInitParameterNames() d)String getServerInfo()
25) Which of the following interface enables the servlet to obtain information about their environment?
a) ServletContext b) ServletConfig c)ServletRequest d) ServletResponse
28) ________ method returns the string equivalent of the client hostname.
a) String getRemoteHost() b)String getRemoteAddr() c) String getProtocol() d) None of these
29) Which method is used to specify before any lines that uses the PrintWriter?
a)SetPageType() b) setContentType() c)setContextType() d)setResponseType()
30) In a web application, running in a web server, servlet is responsible for creating request and response
object.
a) True b) False
31) Which of the following method Write s and stack the trace for e to the server log?
a) void log(Exception e, String s) b) void log( String s) c) void log() d) None of these
32) Which of the following method returns an enumeration with the name of servlets in the same namespace in
the server?
a)String getInitParameter(String param) b) Enumeration getInitParameterNames()
( c) Enumeration getServletNames() d) None of these
33) _____________ returns the real path that corresponds to the virtual path vpath.
a) String getServerInfo() b) String getMimeType(String File)
c) String getRealPath(String vpath) d) int getContentLength()
34) The ________ interface is used to indicate that only a single thread should execute the service() method
of a servlet.
a) SingleThreadModel b) UnithreadModel c) ThreadModel d) None of These
35) If a servlet implements _____________ interface , the server creates several instances of it.
a) ServletResponse Interface b) ServlerRequest Interface c) SingleThreadModel Interface d)None of
these
36) Which of the following class provides implementations of the basic life cycle methods for a servlet.
a)Servlet InputStream Class b) GenericServlet Class
c) ServletException Class d) Servlet OutputStream Class
40) We cannot set attributes in ________ interface that other servlets can use in their implementations.
A)ServletConfig b) ServletContext c)ServletRequest d) both a & b
41) We can set attributes in __________ that other servlets can use in their implementation.
ServletConfig b) ServletContext c)ServletRequest d) none of these
42) Which of the following interface is used to forward the request to another resource that can be
HTML, JSP or another servlet in same application?
a) Request Dispatcher Interface b)SinglethreadModel Interface
c)ServletResponse Interface d) None of These
43)Which of the following method forwards the request from a servlet to another resource (servlet,
JSP file, or HTML file) on the server?
a) void include(ServletRequest request, ServletResponse response)
b) void forward(ServletRequest request, ServletResponse response)
c) void include(ServletRequest request)
d) void forward(ServletRequest request)
46) Which of the following interface guarantees that no two threads will execute concurrently in the
servlet’s service method ?
a)ServletResponse b) ServletRequest c) SingleThreadModel d)ServletConfig
56) Which of the following informs an object that it is bound to or unbound from a session?
58) ------------------ class provides methods to handle HTTP requests and responses
60) Which of the following code can be used to send an error response to the client using the
specified status code and error message?
a) request.sendError(statusCode,message) b) response.sendError(statusCode,message)
62) Which methods are used to bind the objects on HttpSession instance and get the objects?
a) setAttribute b)getAttribute c. Both a & b d. None of the above
a.True b.False
65) ----------- returns part of the URL that identifies the servlet
creates and returns a session for this request. Otherwise, returns the existing session for this request.
a. True b.False
a.True b.False
69) Determines if the session ID must be encoded in the URL identified as url. If so, returns the
modified version of url. Otherwise, returns url.
69) boolean containsHeader(String field) Returns true if the HTTP response header contains a field
named field.
a)False b)True
70) ------------- Returns true if the server created the session and it has not yet been accessed by the
client.
a)True b)False
a)True b)False
73) Some of the information that is saved for each cookie includes the following:
a) True b)False
77) --------------- method Returns true if the cookie must be sent using only a secure protocol.
Otherwise, returns false.
a)False b)True
80) Which method is Called by the server when an HTTP request arrives for this servlet.
a) True b)False
82) --------- obtains the session to which the listener is being bound or unbound
a)getName() b)getSession() c)getSessionName() d)None Of the above
84) ------------ method returns true if the cookie contains session id.Otherwise returns false.
a)Boolean isRequestedSessionIdFromCookie()
b) Boolean isRequestedSessionId()
c) Boolean isSessionIdFromCookie()
a)True b)False
91) Which method adds field to the header with date value equal to msec?
92) Which method sets status code for this response to code
93) Which method returns the time when the client last made a request for this session
96) Which method invalidates this session and removes it from the context?
a) True b)False
a) True b)False
108) ---------------- method returns any path information that is located after the servlet path and
before a query string of the URL
110) Which method returns int equivalent of the header field named field?
111) ----------- method returns name of the user who issues this request.
112) A servlet can write a cookie to a user’s machine via the _________ method of the
HttpServletResponse interface.
113) A servlet can write a cookie to a user’s machine via the addCookie( ) method of the
_____________ interface
115) Information that is saved for each cookie includes the following:
a)The name of the cookie, the value of the cookie b)The expiration date of the cookie
117) Following method is used for Sets the maximum age of the cookie in seconds.
118) Following is the method of HttpServletRequest interface is used to return all the cookies from
the browser.
119) Which one is following method indicates if secure protocol to be used while sending this
cookie?
121) Following method is used to Specifies a path for the cookie to which the client should return
the cookie.
123) Following method is used to Sets the domain in which this cookie is visible.
126) Following method returns the session in which the event occurred.
131) The ________ method is overridden to process any HTTP POST requests that are sent to
servlet.
132) ________________ interface enables a servlet to obtain information about a client request.
a)
b)
c)
d)
138) Following method returns the time (in milliseconds since midnight, January 1, 1970, GMT)
when this session was created.
139) Methods of HttpSession interface throw an _______________ if the session has already been
invalidated.
a) IllegalState b)IllegalException c)LegalStateException d)IllegalStateException
140) Following method returns true if the requested session ID is valid in the current session context.
141) The Java __________ specification defines an application programming interface for
communication between the Web server and the application program.
a) Servlet
b) Server
c) Program
d) Randomize
142) The doGet() method in the example extracts values of the parameter’s type and number by
using __________
a) request.getParameter()
b) request.setParameter()
c) responce.getParameter()
d) responce.getAttribute()
143) Dynamic interception of request and responses to transform the information is done by
import java.io.*;
import javax.servlet.*;
pw.close();
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ColorPostServlet extends HttpServlet
{
public void doPost(HttpServletRequest request,HttpServletResponse response) throws
ServletException, IOException
{
String color = request.getParameter("color");
PrintWriter pw = response.getWriter();
pw.close();
Answer:
145) Which is the missing statement in following code to get proper output:
import java.io.*;
import java.util.*;
public class HelloWorld extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
HttpSession hp=request.getSession(true);
Date create=new Date(hp.getCreationTime());
Date access=new Date(hp.getLastAccessedTime());
out.println("<h1><br>Session is created at "+create);
out.println("<h1><br>Last access time is "+access);
out.println("<h1><br>Session id is "+hp.getId());
}
}
a)Missing ;
b)Missing()
c)Missing Package Statement
d)Missing {
146) What is the missing statement in the following program to get the proper output
import java.io.*;
import java.util.*;
public class HelloWorld extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
HttpSession hp=request.getSession(true);
Date create=new Date(hp.getCreationTime());
Date access=new Date(hp.getLastAccessedTime());
out.println("<h1><br>Session is created at "+create);
out.println("<h1><br>Last access time is "+access);
out.println("<h1><br>Session id is "+hp.getId());
}
}
/*OUTPUT*/
a)Missing semicolon
b)missing {}
c)missing Package statement
d)none of the above
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class example extends ___________
{
public void doGet(HttpServletRequest request, HttpServletResponse response)throws
IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
// print out cookies
String name = request.getParameter("t1");
String value = request.getParameter("t2");
Cookie c = new Cookie(name, value);
response.addCookie(c);
String n1 = c.getName();
String v1 = c.getValue();
out.println("<h3> The Name Of Cookie is:</h3>"+n1);
out.println("<h3> The Value Of Cookie is:</h3>"+v1);
}
}
a) HttpServlet
b)HttpResponse
c)HttpRequest
d)All of the above
148) What we should write at the place of____________ to get the proper output.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class password extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)throws
IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = ________________
out.println("<html>");
out.println("<head>");
out.println("<title>Hello World!</title>");
out.println("</head>");
out.println("<body>");
String a= request.getParameter("t1");
String b= request.getParameter("t2");
int a1 =a.length();
int b1= b.length();
if(b.length()<=6)
{
out.println("<h3> Welcome To Home Page<h3>");
}
else
{
out.println("<h3>Password Should not more than 6 Character<h3>");
}
out.println("</body>");
out.println("</html>");
}
}
a)request.getWriter();
b)response.getWriter();
c)response.printWriter();
d)none of the above
149) What should we write at the place of _____________to get the proper output.
import java.io.*;
import javax.servlet.*;
public class GenericServletDemo extends GenericServlet
{
public void service(ServletRequest request ,____________ response) throws
ServletException,IOException
{
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("<HTML>");
out.println("<HEAD><TITLE>hello page</TITLE></HEAD>");
out.println("<BODY>");
String name=(String) request.getParameter("user1");
String password=(String) request.getParameter("password1");
String phone=(String) request.getParameter("phone1");
out.println("Name="+name+"<br>");
out.println("Passsword="+password+"<br>");
out.println("Phone="+phone+"<br>");
out.println("</BODY></HTML>");
}
}
a)HttpReuest
b) ServletResponse
c)HttpResponse
d)none of the above
150) Choose correct package for following code from given options.
import java.io.*;
import javax.servlet.*;
public class HelloServlet extends ******* {
public void service(ServletRequest request,
ServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.println("<B>Hello!");
pw.close();
}
}
a) GenericServlet
b) genericServlet
c) HttpServlet
d) httpServle
151) In following Java program fill statement showing ******.Select any one option from given
options
import java.io.*;
import java.util.*;
import javax.servlet.*;
1. SOFTWARE DEVELOPMENT
2. USING JAV// Get enumeration of parameter names
Enumeration e = request.getParameterNames();
;
// Display parameter names and values.
while(e.hasMoreElements()) {
String pname = (String)e.nextElement();
pw.print(pname + " = ");
String pvalue = *******;
pw.println(pvalue);
}
pw.close();
}
}
a) request.getParameterName(pname);
b) request.Getparametername(pname);
c) Request.Getparametername(pname);
d) request.getParametername(pname);
152) Consider the following program and identify the missing method attribute ***** if you want
to implement HTTP POST.
<html>
<body>
<center>
<form name="Form1"
method=*****
action="http://localhost:8080/examples/servlet/ColorPostServlet">
<B>Color:</B>
<select name="color" size="1">
<option value="Red">Red</option>
<option value="Green">Green</option>FARE DEVELOPMENT
USING JAVA
<option value="Blue">Blue</option>
</select>
<br><br>
<input type=submit value="Submit">
</form>
</body>
</html>
a)HTTP POST
b)HTTP GET
c)post
d)get
153) Consider the following program. Identify the missing word ****.
<html>
<body>
<center>
<form name="Form1"
action="http://****/examples/servlet/ColorGetServlet">
<B>Color:</B>
<select name="color" size="1">
<option value="Red">Red</option>
<option value="Green">Green</option>
<option value="Blue">Blue</option>
</select>
<br><br>
<input type=submit value="Submit">
</form>
</body>
</html>
a)localhost:8080
b) localhost:0808
c) locallhost:8080
d) localhost:80800
154) Consider the following program and identify the missing method name ****** if you want
to implement HTTP POST.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ColorGetServlet extends HttpServlet {
public void ******(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String color = request.getParameter("color");
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.println("<B>The selected color is: ");
pw.println(color);
pw.close();
}
}
a)doGet()
b)doPost()
c)HTTP GET()
d)HTTP POST()