SCWCD-10

04/14/2008

6 Comments

 

What will be the output of the following JSP code?

<% int a = 10; %>
<%! int a = 20; %>
<%! int b = 30; %>
The value of b multiplied by a is <%= b * a %>

·  A) The code will not compile
·  B) The value of b multiplied by a is 30
·  C) The value of b multiplied by a is 300
·  D) The value of b multiplied by a is 600
·  E) The value of b multiplied by a is 0

Answer
C
Since the local variable "a" has precedence over the global variable "a", the expression (b * a) evaluates as (30 *10), which equals 300

Consider the following code snippet of servlet code:

public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
String dbValue = getValueFromDB ();
if (dbValue == null) response.sendError (HttpServletResponse.SC_NOT_FOUND, "DB Lookup Failed");
response.sendRedirect ("Form.jsp"); }
If the getValueFromDB () method returns null , which of the following statements are true?

·  A) The code will work without any errors or exceptions
·  B) An IllegalStateException will be thrown
·  C) An IOException will be thrown
·  D) A NullPointerException will be thrown

Answer
B

Let's assume you are developing a custom tag handler using the BodyTag interface. The BodyTag interface extends the IterationTag interface by defining additional methods that allow a tag handler to evaluate its body content in a temporary buffer.

Upon execution, your tag handler returns the EVAL_BODY_BUFFERED return value from the doStartTag() method. In addition, your tag handler returns the EVAL_BODY_AGAIN value two times from the doAfterBody() method.

Based on this information, how many times will the setBodyContent() method be called by the JSP container?


·  A) Zero
·  B) One
·  C) Two
·  D) Three

Answer
B
Let's assume you are deploying a legacy JSP page using a JSP container that complies with the JSP 2.0 specifications. In JSP 2.0, EL (Expression Language) constructs will be interpreted by default. However, you are concerned that this may cause problems for your legacy JSP application.Which of the following can be used to configure the JSP container to ignore EL expressions?

·  A) <%@ page isELIgnored="false" %>
·  B) <%@ page isELIgnored="true" %>
·  C) <%@ include isELIgnored="false" %>
·  D) <%@ import isELIgnored="false" %>

Answer
B

Let's assume you have a customerBean which has an address property object. This address object has a city property.

Which of the following is a valid way to set the city property of the customerBean.address object using the appropriate JSTL tags?


·  A) <c:set target="${customerBean.address}" property="city" value = "${city}" />
·  B) <c:set property="customerBean.address" value="${city}" />
·  C) <c:set target="${customerBean}" property="address.city" value = "${city}" />
·  D) <c:set var="${customerBean.address}" property="city" value = "${city}" />

Answer
A

Let's assume you are developing a web application. One of the requirements of this application is to design a flexible framework so that services can be easily added and removed without affecting existing components.

For instance, services such as logging, authentication, debugging, compressing and encoding output, etc.. should be "pluggable" without requiring changes to the core application code. Which of the following design patterns can be used in this situation?


·  A) Intercepting Filter
·  B) Transfer Object
·  C) Business Delegate
·  D) Data Access Object

Answer
A

Let's assume you are writing a JSP page using EL expressions. You have an variable declared as java.util.Map customerMap. You need to retrieve the customer info that is stored in this map.

Which of the following methods is invoked by the JSP engine upon execution of the following EL expression: ${customerMap[CustomerA]}?


·  A) customerMap.get (pageContext.findAttribute ("CustomerA"));

·  B) customerMap.get("CustomerA")
·  C) customerMap.remove ("CustomerA");
·  D) customerMap.getProperty ("CustomerA");
·  E) customerMap.getCustomerA ();

Answer
A

What will be the output of the following JSP page?
 <html><body>
   <% a = 100; %>
   <% int a = 200; %>
   <%! int a = 300; %>
   a = <%= a %>, <%= this.a %>
 </body></html>

A) a = 200, 100
B) a = 300, 100
C) a = 100, 200
D) a = 200, 300
E) The code will not compile as written

Answer
A

Which of the following files is the correct name and location of deployment descriptor of a web application. Assume that the web application is rooted at \doc-root. Select the one correct answer ?  
 
     A    \doc-root\dd.xml       
     B    doc-root\web.xml       
     C    \doc-root\WEB-INF\web.xml       
     D    \doc-root\WEB_INF\web.xml       
     E    \doc-root\WEB-INF\classes\web.xml

Answer
C

Which element of the deployment descriptor is used to specify the class of the Servlet. ?  
 
     A    <servlet-class>       
     B    <servlet-name>       
     C    <servlet_name>       
     D    <servlet_class>       
     E    <servlet>
Answer
A

 

SCWCD-9

04/14/2008

0 Comments

 

Which statement best expresses the purpose of a tag library descriptor (TLD) in
an EL function?

a It contains the Java code that will be compiled.
b It invokes the Java method as part of the JSP.
c It matches the tag library with a URI.
d It matches function names to tags that can be used in the JSP.

Answer:
d

Which design pattern allows you to decouple the business logic, data representation,
and data presentation?
(Select one)
a Model-View-Controller
b Transfer Object
c Bimodal Data Access
d Business Delegate
Answer: a

Which of the following is a valid way of getting a bean’s property? (Select one)
a <jsp:useBean action="get" id="address" property="city" />
b <jsp:getProperty id="address" property="city" />
c <jsp:getProperty name="address" property="city" />
d <jsp:getProperty bean="address" property="*" />

Answer:
c

Which of the following web.xml snippets correctly defines the use of a tag
library?
(Select one)
a <taglib>
<uri>http://www.abc.com/sample.tld</uri>
<location>/WEB-INF/sample.tld</location>
</taglib>
b <tag-lib>
<taglib-uri>http://www.abc.com/sample.tld</taglib-uri>
<taglib-location>/WEB-INF/sample.tld</taglib-location>
</tag-lib>
c <taglib>
<taglib-uri>http://www.abc.com/sample.tld</taglib-uri>
<taglib-location>/WEB-INF/sample.tld</taglib-location>
</taglib>
d <tag-lib>
<taglib>http://www.abc.com/sample.tld</taglib-uri>
<taglib>/WEB-INF/sample.tld</taglib-location>
</tag-lib>

Answer:
c

Which object does a SimpleTag tag handler use to access implicit variables?
a PageContext
b BodyContent
c JspContext
d SimpleTagSupport
Answer: c

What is wrong with the following code?
public void doFilter(ServletRequest req, ServletResponse, res,
FilterChain chain)
throws ServletException, IOException {
chain.doFilter(req, res);
HttpServletRequest request = (HttpServletRequest)req;
HttpSession session = request.getSession();
if (session.getAttribute("login") == null) {
session.setAttribute("login"”, new Login());
}
}
a The doFilter() method signature is incorrect; it should take HttpServlet-
Request and HttpServletResponse.
b The doFilter() method should also throw FilterException.
c The call to chain.doFilter(req, res) should be this.doFilter(req, res,
chain).
d Accessing the request after chain.doFilter() results in an IllegalState-
Exception.
e Nothing is wrong with this filter.

Answer:
e

 

SCWCD-8

04/14/2008

0 Comments

 

What is the name of the tag library descriptor element that declares that an
attribute can have a request-time expression as its value?

a [__________________]

Answer:
rtexprvalue

Consider the following HTML page code:
<html><body>
<a href="/servlet/HelloServlet">POST</a>
</body></html>
Which method of HelloServlet will be invoked when the hyperlink displayed
by the above page is clicked?
(Select one)
a doGet
b doPost
c doForm
d doHref
e serviceGet

Answer:
a

Which of the following deployment descriptor snippets correctly associates a servlet
implemented by a class named com.abc.SalesServlet with the name
SalesServlet?
(Select one)
a <servlet>
<servlet-name>com.abc.SalesServlet</servlet-name>
<servlet-class>SalesServlet</servlet-class>
</servlet>
b <servlet>
<servlet-name>SalesServlet</servlet-name>
<servlet-package>com.abc.SalesServlet</servlet-package>
</servlet>
c <servlet>
<servlet-name>SalesServlet</servlet-name>
<servlet-class>com.abc.SalesServlet</servlet-class>
</servlet>
d <servlet name="SalesServlet" class="com.abc.SalesServlet">
<servlet>
<servlet-class name="SalesServlet">
com.abc.SalesServlet
</servlet-class>
</servlet>
e <servlet>
<servlet-name class="com.abc.SalesServlet">
SalesServlet
</servlet-name>
</servlet>

Answer:
c

Consider the contents of the following two JSP files:
File 1: test1.jsp
<html><body>
<% String message = "Hello"; %>
//1 Insert LOC here.
The message is <%= message %>
</body></html>
File 2: test2.jsp
<% message = message + " world!"; %>
Which of the following lines can be inserted at //1 in test1.jsp so that it
prints "The message is Hello world!" when requested?
(Select one)
a <%@ include page="test2.jsp" %>
b <%@ include file="test2.jsp" />
c <jsp:include page="test2.jsp" />
d <jsp:include file="test2.jsp" />

Answer:
b

Which of the following code snippets, when inserted in the doGet() method,
will correctly count the number of GET requests made by a user?
(Select one)
a HttpSession session = request.getSession();
int count = session.getAttribute("count");
session.setAttribute("count", count++);
b HttpSession session = request.getSession();
int count = (int) session.getAttribute("count");
session.setAttribute("count", count++);
c HttpSession session = request.getSession();
int count = ((Integer) session.getAttribute("count")).intValue();
session.setAttribute("count", count++);
d HttpSession session = request.getSession();
int count = ((Integer) session.getAttribute("count")).intValue();
session.setAttribute("count", new Integer(count++));

Answer:
d

What is the term for determining whether a user has access to a particular
resource?
(Select one)
a Authorization
b Authentication
c Confidentiality
d Secrecy

Answer:
a

Which of the following tags can you use to print the value of an expression to the
output stream?
(Select two)
a <%@ %>
b <%! %>
c <% %>
d <%= %>
e <%-- --%>

Answers:
c and d

Which of the following methods will be invoked when a ServletContext is
created?
(Select one)
a contextInstantiated() of javax.servlet.ServletContextListener
b contextInitialized() of javax.servlet.ServletContextListener
c contextInited() of javax.servlet.ServletContextListener
d contextCreated() of javax.servlet.ServletContextListener

Answer:
b

What will be the output of the following code? (Select one)
<html><body>
The value is <%=""%>
</body></html>
a Compilation error
b Runtime error
c The value is
d The value is null

Answer:
c

 

SCWCD-7

04/14/2008

4 Comments

 

Which of the following JSP tags can be used to include the output of another JSP
page into the output of the current page at request time?
(Select one)
a <jsp:insert>
b <jsp:include>
c <jsp:directive.include>
d <jsp:directive:include>
e <%@ include %>

Answer:
b

Which listener will get notified when a context is created?
A ServletContextListener
B ServletConfigListener
C HttpServletContextListener
D ContextListener

Answer:
A
When a context is created, the ServletContextListener will be notified and the method contextInitialized(ServletContextEvent) will be invoked.

Which method correctly set an attribute to context ? Select two
A getServletContext().setAttribute(name, value);
B getServletConfig().setAttribute(name, value);
C getServletConfig().getServletContext().setAttribute(name, value);
D response.setAttribute(name, value);

Answer:


A and C
Answer C is correct because always ServletConfig holds a reference to ServletContext


Which method is used to know when a request is created ?
A RequestListener
B ServletRequestListener
C ServletRequestAttributeListener
D servletListener

Answer:

B
When a request is created the ServletRequestListener will be notified

Consider the following code:
<html><body>
${(5 + 3 + a > 0) ? 1 : 2}
</body></html>
Select the correct statement from the options below:
a It will print 1 because the statement is valid.
b It will print 2 because the statement is valid.
c It will throw an exception because a is undefined.
d It will throw an exception because the expression’s syntax is invalid.

Answer:
a

Which of the following is a valid use of the <jsp:useBean> action? (Select one)
a <jsp:useBean id="address" class="AddressBean" />
b <jsp:useBean name="address" class="AddressBean"/>
c <jsp:useBean bean="address" class="AddressBean" />
d <jsp:useBean beanName="address" class="AddressBean" />

Answer:
a

Which of the following elements are required for a valid <taglib> element in
web.xml? (Select two)
a uri
b taglib-uri
c tagliburi
d tag-uri
e location
f taglib-location
g tag-location
h tagliblocation

Answers:
b and f

Which of the following is not a valid subelement of the <attribute> element in
a TLD? (Select one)
a <name>
b <class>
c <required>
d <type>
Answer:
b

 

SCWCD-6

04/14/2008

10 Comments

 

Which method in the HttpServlet class services the HTTP POST request?
(Select one)
a doPost(ServletRequest, ServletResponse)
b doPOST(ServletRequest, ServletResponse)
c servicePost(HttpServletRequest, HttpServletResponse)
d doPost(HttpServletRequest, HttpServletResponse)
Answer:
d

Which element is used to specify useful information about an initialization
parameter of a servlet in the deployment descriptor?
(Select one)
a param-description
b description
c info
d param-info
e init-param-info

Answer:
b

Which of the following methods will be invoked when a ServletContext is
destroyed?
(Select one)
a contextDestroyed() of javax.servlet.ServletContextListener
b contextDestroyed() of javax.servlet.HttpServletContextListener
c contextDestroyed() of javax.servlet.http.ServletContextListener
d contextDestroyed() of javax.servlet.http.HttpServletContextListener

Answer:
a
Explanation
Remember that the concept of servlet context applies to all the servlets and not
just HttpServlets. Therefore, interfaces related to servlet context belong to the
javax.servlet package.


Which elements are allowed in the element of the deployment
descriptor?
(Select three)
a
b
c
d
e

Answers:
a, c, and d

Explanation
Answer a is correct because you can map filters to named servlets, as well as URL
patterns.

When servlet init parameter will be read ?
A When client makes a request.
B When starting server
C When container initializes the servlet
D When container shuts down.

Answer:
C
When the container initializes the servlet, it reads the DD and creates a parameter list for ServletConfig. These parameters are read only once at the time of initialization only


Which of the following interfaces or classes is used to retrieve the session associated
with a user?
(Select one)
a GenericServlet
b ServletConfig
c ServletContext
d HttpServlet
e HttpServletRequest
f HttpServletResponse

Answer:
e
Explanation
The session associated with a user can only be retrieved using the HttpServlet-
Request.getSession() method.

Which of the following correctly defines data integrity? (Select one)
a It guarantees that information is accessible only to certain users.
b It guarantees that the information is kept in encrypted form on the server.
c It guarantees that unintended parties cannot read the information during transmission
between the client and the server.
d It guarantees that the information is not altered during transmission between the client
and the server.

Answer:
d
Explanation
Answers a and c describe authorization and confidentiality. Encrypting data kept
on the server may be part of some security plans, but is not covered by the servlet
specification.

Which method is used to read the context initialization parameter ? Select two
A getServletConfig().getInitParameter();
B getServletContext().getInitParameter();
C getServletConfig().getContextParameter();
D getServletContext().getServletParameter();
E getServletConfig().getServletContext().getInitParameter();

Answer:
B and E
Option A is incorrect because it reads servlet initialization parameter only, not context initialization parameter. Option c and d are incorrect because , they are invalid methods. Option E is correct because always a ServletConfig contains a reference to the ServletContext.

What getInitParameterNames() of ServletContext does ?
A Returns the init parameters for a servlet
B Returns the init parameter names of a servlet
C Returns the init parameter names of the context
D All of the above.

Answer:
C
getInitParameterNames() returns the names of the context's initialization parameters as an Enumeration

Which is the correct way to set a context initialization parameter ?
A getServletContext().setInitParameter();
B getServletConfig().setInitParameter();
C using DD
D None of the above.

Answer:
C
The only way to set a context initialization parameter is by using tag in the DD only. We cannot set the context param programmatically

Consider the following code and select the correct statement about it from the
options below.
(Select one)
<html><body>
<%! int aNum=5 %>
The value of aNum is <%= aNum %>
</body></html>
a It will print "The value of aNum is 5" to the output.
b It will flag a compile-time error because of an incorrect declaration.
c It will throw a runtime exception while executing the expression.
d It will not flag any compile time or runtime errors and will not print anything to the
output.

Answer:
b

What will be the output of the following code? (Select one)
<html><body>
<% x=3; %>
<% int x=5; %>
<%! int x=7; %>
x = <%=x%>, <%=this.x%>
</body></html>
a x = 3, 5
b x = 3, 7
c x = 5, 3
d x = 5, 7
e Compilation error

Answer:
c

 

SCWCD-5

04/14/2008

0 Comments

 

What will be the output , when accessing the following servlet

import javax.servlet.http.*;

public class TestServlet extends HttpServlet {

}

A Blank Screen
B Runtime Error
C Compiler Error
D HTTP 405, error message.
D HTTP 404, error message.

Answer

D
HttpServlet is providing a default implementation for Http Methods, it will not give any compile time or run time error. But the default implementation will give an HTTP 405 error message (At least in BEA Web logic server).


What is the return type of request.getParameterValues()?

A String
B Vector
C String[]
D ArrayList

Answer

C
getParameterValues() returns an array o f String objects containing all of the values the given request parameter has, or null if the parameter does not exist

Which method is used to get the HTTP method of the request?

A request.getMethod();
B request.getForm()
C request.getHttpMethod()
D request.getFormMethod()

Answer

A
Return the name of the HTTP method with which the servlet request was made , such as GET, POST etc..


In order to redirect a servlet , what is the method used ?

A response.sendRedirect(java.net.URL)
B response.sendRedirect(String)
C response.sendURL(java.net.URL)
D response.sendURL(String)

Answer

B
response.sendRedirect(String) is used for redirecting response to another URL.


Which method is used for finding out what the server is receiving ?

A GET
B POST
C PUT
D TRACE

Answer

D
TRACE gives information about what the server is receiving . Used mainly for debugging only.

Which methods are used to set a header? Choose all that apply.

A response.setHeader()
B response.addHeader()
C response.addStringHeader()
D response.setDateHeader()
E response.addDateHeader()

Answer

A, B, D and E
setHeader and addHeader adds a header value. But setHeader overwrites existing value if one exist where as addHeader adds one more value for this header. The setDateHeader and addDateHeader are used fro setting date headers.

What will be the output of the following expression ?
<%= System.out.println("Hello.."); %>

A Hello..
B System.out.println("Hello..")
C Compiler Error
D java.io.PrintStream@1ccf82Hello

Answer

C
We cannot use 'void' in JSP expression. All JSP expression should return a valid type except void.


Which method is used to log messages from a servlet ?

A getServletConfig().log();
B getServletContext().log();
C getServletConfig().log(String)
D getServletContext().log(String)

Answer

B and D
The log() method is defined in ServletContext interface.


What are the mandatory sub elements of <login-config> element ?

A <auth-method>
B <form-login-config>
C <login>
D No mandatory sub element.

Answer

D
The <login-config> element does not define any mandatory sub element.

What methods are used to get the header information from the client ? Select all that apply.

A request.getHeader(String)
B request.getFormHeader(String)
C request.getIntHeader(String)
D request.getDateHeader(String)

Answer

A, C and D
(String headername) is used to get the header from the client. The getDateHeader is used for getting date headers. It will throw an IllegalArgumentException , if the header cannot be converted to a date. getIntHeader will return the value of specified header as an int. if the specified header cannot be converted to an int , it till throw a NumberFormatException.

 

SCWCD-4

04/14/2008

1 Comment

 

Which of the following HTTP methods are not idempotent?
A POST
B GET
C HEAD
D PUT

Answer
A
HTTP 1.1 declares POST as non idempotent

If the web application is distributed across multiple JVMs, how many instances of the servlet will be created ?
A 1
B we cannot distribute a web-application in multiple JVMs.
C One instance per JVM
D None

Answer
C
There will be one instance per JVM in distributed web-apps. But there will be only one instance in a single JVM

What is the argument for init() method ? Select all that apply.
A ServletContext
B ServletConfig
C HttpServletConfig
D HttpServlet
E No arguments

Answer
B and E
The init() method is having two overloaded versions in GenericServlet class. The init() that takes a ServletConfig object and init without any parameter. The no-argument init method is simply calling the init with ServletConfig object.

public class MyServlet extends HttpServlet {
private int x = 0;
public MyServlet (int x) {
this.x = x;
}
public void doGet (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
x++;
PrintWriter out = response.getWriter();
out.println(x);
}
}
What will be the output (assume all imports are done correctly), when invoking MyServlet ?


A
Compiler error

B
Runtime Exception

C
Prints 1 in browser window.

D
Prints the value of x in browser window , depending up on the value used to create the servlet.
Which method is used to retrieve a form value in a JSP or Servlet?
A request.getAttribute(String)
B response.getAttribute(String)
C request.getParameter(String)
D response.getParameter(String)

Answer
C
Option B and D are incorrect because , no such method exist. Option A is incorrect because we cannot use that method for receiving request parameters.

Which is the default HTTP form method?
A POST
B HEAD
C PUT
D GET

Answer
D
The default HTTP form method is GET

HTTP HEAD method is used for ?
A To get the header part of the URL
B To delete a resource from server
C To place a resource on the server
D To connect to the server

Answer
A
HEAD method is used to get only the header part of the requested URL. It will return only header, no body

If a single client makes two requests, how many threads will be created by the container ?
A 0
B 1
C 2
D 3

Answer
C
For each and every request , the container will create a separate thread.

All Servlets implement which interface ?
A HttpServlet
B Servlet
C ServletRequest
D GenericServlet

Answer
B
All Servlets must implement javax.servlet.Servlet interface either directly or indirectly

True or False ? A new servlet is created each time a client request a servlet ?
A True
B false

Answer
B
There will be only one servlet in a JVM (if your servlet is not SingleThreadModel). Each time a client makes the request, the container will create a new thread for handling that request

 

SCWCD-3

04/14/2008

0 Comments

 

What will be the output of the following code? Assume that all the variables are declared properly

String str= request.getDateHeader("Accept-Language");

out.println(str);
A Compiler error
B An IO Exception is thrown
C Null
D An IllegalArgumentException thrown

Answer

D
Answer D is the correct choice. If the header value cannot converted to date , then an IllegalArgumentException will be thrown. Since the header value is a String, it will throw the IllegalArgumentException

Which method is used to store file in the server ?
A GET
B PUT
C POST
D HEAD

Answer

B
A PUT method is used to store a file or data in the server. The request URI identifies the location in the server to store the file

Which character is used to separate the URI and query string ?
A ?
B &
C =
D ;

Answer

A
Choice A is correct. ? is used to separate the URI and query string. & is used to separate the name value pairs from each other


Which method is called when a context is initialized ?
A contextInitialize(ServletContextEvent)
B contextInitialized(HttpServletContextEvent)
C contextInitialized(ServletContextEvent)
D contextInitialized(SevletContext)

Answer

C
The contextInitialized(ServletContextEvent) method is called when a context is initialized

When a user clicks on a link in a page, which method will get invoked ?
A POST
B PUT
C GET
D HEAD

Answer

C
When the user types the request URL into the browser's location field or clicks on a hyperlink, the GET method is triggered. When the user submits the form with the method attribute as "GET" or without any method attribute, then also GET will invoked


Which interface is having method getSession() ?
A ServletSession.
B ServletRequest
C HttpServletRequest
D ServletResponse
E HttpServletResponse

Answer

C
The HttpServletRequest interface contains the method getSession().

Which exception will be thrown, when a servlet is unavailable temporarily ?
A ServletException
B UnavailableException
C IOException
D UnAccessibleException

Answer

B
The UnavailableException will be thrown, when a servlet is unavailable temporarily

What method will get executed on clicking the following code.?

<form method="GET" action="/TestServlet">

<input type="text" name="name">

<input type="submit" value="Submit">

</form>
A GET
B POST
C HEAD
D PUT


Answer

A

Which of the following listeners will be called when a context is destroyed?
A HttpServletContextListner
B HttpSessionListener
C ServletContextDestroyedListener
D ServletContextListener

Answer

D
The contextDestroyed(ServletContexEvent) method of ServletContextListener is called, when a context is destroyed

 

SCWCD-2

04/14/2008

1 Comment

 

Which of the following can be used to configure the JSP container to ignore EL expressions?
A <%@ page isELIgnored="true" %>
B <%@ page isELIgnored="false" %>
C <%@ import isELIgnored="false" %>
D None of the above.

Answer
D
Configuring the <el-ignored> tag inside the DD is the only way to ignore EL expressions. There was a isELIgnored attribute for page directive in the 2.0 draft version. But it is removed from the final version.

Which method is used to retrieve objects from session?
A getAttribute method of javax.servlet.ServletSession.
B getAtrribute method of javax.servlet.HttpSession
C getAttribute method of javax.servlet.http.Session
D getAttribute method of javax.servlet.http.HttpSession
E getAttribute method of javax.servlet.HttpSession

Answer
D
All other answers are invalid because no such classes present.

Which among the following will compile ?
A <% int x=10 %>
B <%= "hello how are you" %>
C <%= "hello" ;% >
D <%! int x=10 %>

Answer
B
Option B is the correct choice. A is in correct because there is no semi colon present. The same rule applies to option D also. Option C is in correct because of semi colon

Which of the following is the proper way to include java.util package in your jsp page?
A <%@page:import package="java.util">
B <%@ page import = "java.util.*" %>
C <%= import java.util.*; %>
D <%@ page import="java.util.*" %>

Answer
D
Answer A is incorrect because, there is no such thing called <%@page import package. Answer B is incorrect because , we cannot have space between page and = sign. Answer c is correct, because it is a JSP expression.

Consider the following code snippet of servlet code:
public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
String value = getValue ();
if (value == null)
response.sendError (HttpServletResponse.SC_NOT_FOUND, "Failed");
response.sendRedirect ("test.jsp");
}
If the getValue () method returns null , which of the following statements are true?
A The code will work without any errors or exceptions
B An IllegalStateException will be thrown
C An IOException will be thrown
D A NullPointerException will be thrown

Answer
B
Since the response is already committed by calling the sendError() , we cannot redirect it once again

To send binary output to response, which method of HttpServletResponse is used to get the Writer/ Stream object ?
A getStream
B getWriter
C getBinaryOutputStream
D getOutputStream
E getBinaryStream

Answer

D
The getOutputStream is used for sending binary data. The getWriter is used for sending character data only

<%@ page language ="java" session="false" isErrorPage="false" %> which of the following JSP implicit object will not be available to the JSP page ? Select two
A session
B request
C application
D exception

Answer
A and D
Since we are disabling the session by using session="false", session will not be available. And since this page is not an error page , so exception also will not be available.

What will be the output of the following JSP code?
<html>
<body>
<%! int a = 20; %> <% int a = 10; %> <%! int b = 30; %> Now b = <%= b * a %>
</body>
</html>

A Now b = 300
B Now b = 600
C The code will not compile
D Now b = 30
E Now b = 0

Answer
A
In the first declaration <%! int a = 20; %>, the variable "a" will be declared as an instance variable. In the second declaration <% int a = 10; %>, the variable "a" will be declared as a local variable inside the service method. And at the time of multiplication it will use the local variable.

What is the default scope for <jsp:useBean>
A application
B session
C page
D request

Answer
C
The default scope attribute for useBean is page

In a JSP custom tag , which method would you use to access JSP implicit variable that references application scope ?
A PageContext.getOut()
B jspFactory.getPageContext()
C TagSupport.getValue(String)
D pageContext.getServletContext()

Answer
D

 

SCWCD-1

04/14/2008

3 Comments

 

Which of the following files is the correct name and location of deployment descriptor of web application. Assume that the web application is rooted at \doc-root. Select the one correct answer ?
A \doc-root\dd.xml
B doc-root\web.xml
C \doc-root\WEB-INF\web.xml
D \doc-root\WEB_INF\web.xml
E \doc-root\WEB-INF\classes\web.xml

Answer
C
The deployment descriptor must be called web.xml and be placed in the directory named WEB-INF.

Which element of the deployment descriptor is used to specify the class of the Servlet. ?
A <servlet-class>
B <servlet-name>
C <servlet_name>
D <servlet_class>
E <servlet>

Answer
A
The element servlet-class specifies the class of servlet.

Which of the following statements is true regarding MyServlet?
import javax.servlet.*;
;import javax.servlet.http.*;
import java.io.*;
public class MyServlet extends HttpServlet implements SingleThreadModel{
String myName;
public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
response.setContentType("text/plain");
PrintWriter out = res.getWriter();
myName = req.getParameter("name");
sayHello(out);
out.close();
}
public void sayHello(PrintWriter out) {
out.println("Hello " + myName);
}
}
A MyServlet is thread safe
B MyServlet is not thread safe because myName is an instance variable
C MyServlet is not thread safe because MyServlet implements SingleThreadModel.
D None of the above

Answer
A
Choice A is correct. A application is thread safe if it always behaves predictably regardless of the number of concurrent threads running in its process space. The simplest way to ensure that a servlet is thread safe is to implement the SingleThreadModel interface. By implementing this interface, the server guarantees that no more than one thread can execute the service(), doGet(), or doPost() method at a time for a particular servlet instance. This makes the servlet thread safe. Thus even if class MyServlet has instance variables, it is thread safe. Thus A is the correct choice and the other choices are incorrect.

Which of the following combinations regarding Design Patterns are correct?
A Business Delegate - Reduces the coupling between presentation-tier clients and business services.
B Data Access Object - Allows for Multiple Views using the same model
C MVC - Enables easier migration to different persistence storage implementations.
D Value Object - Reduces Network Traffic

Answer
A and D
Choices A and D are correct. In a normal scenario, presentation-tier components (e.g. a JSP) interact directly with business services. As a result, the presentation-tier components are vulnerable to changes in the implementation of the business services: when the implementation of the business services change, the code in the presentation tier must change. The goal of the Business Delegate object design pattern is to minimize the coupling between presentation-tier clients and the business service API, thus hiding the underlying implementation details of the service. Thus choice A is correct. Choice B is incorrect as it's the MVC design pattern rather than the DAO (Data Access Object), which provides Multiple Views using the same model. Choice C is incorrect as it's the DAO (Data Access Object) pattern, which enables easier migration to different persistence storage implementations. The Value Object is used to encapsulate the business data. A single method call is used to send and retrieve the Value Object. When the client requests business data from an enterprise bean, the enterprise bean can construct the Value Object, populate it with its attribute values, and pass it by value to the client. Thus choice D is also correct.

Which of these is true about deployment descriptors. Select one correct answer.
A The order of elements in deployment descriptor is important. The elements must follow a specific order.
B The elements of deployment descriptor are not case insensitive
C The servlet-mapping element, if defined, must be included within the servlet element.
D The web-app element must include the servlet element

Answer
A
The servlet specifications specifies a specific order of deployment descriptor. b is incorrect because elements are case-sensitive. The servlet-mapping element should be included within the element. So c is incorrect. All the elements within the web-app element are optional. So d is incorrect.

Which element of the deployment descriptor includes the exception-type as a sub-element ?
A <exception>
B <error-page>
C <error>
D <exception_type>
E <error_page>

Answer
B
The element error-page includes the element web-app.

Which of these is a correct fragment within the web-app element of deployment descriptor. Select the two correct answer.
A 404 /error.jsp
B mypackage.MyException 404 /error.jsp
C mypackage.MyException 404
D mypackage.MyException /error.jsp

Answer
A and D
error-page element must include either exception-type or error-code element but not both. It must also include the location element.

Which element of the deployment descriptor of a web application includes the welcome-file-list element as a sub element.
A <welcome>
B <welcome-files>
C <list>
D <web-app>
E <context>

Answer
D
The contains the element.

Which of these is a correct example of specifying a listener element resented by MyClass class. Assume myServlet element is defined correctly. Select one correct answer.
A MyClass
B MyClass
C aListener MyClass
D <> myServlet MyClass

Answer
B
The element listener-class must be included within the listener element.

Which of the following is legal JSP syntax to print the value of i. Select the one correct answer
A <%int i = 1;%><%= i; %>
B <%int i = 1;i; %>
C <%int i = 1%><%= i %>
D <%int i = 1;%><%= i %>
E <%int i = 1%><%= i; %>

Answer
D
When using scriptlets (that is code included within ), the included code must have legal Java syntax. So the first statement must end with a semi-colon. The second statement on the other hand is a JSP expression. So it must not end with a semi colon.

 
    SCWCD Sets


    Archives

    May 2008
    April 2008