1.
Which method in the HttpServlet class services the HTTP POST request?
(Select one)
Correct Answer
D. DoPost(HttpServletRequest, HttpServletResponse)
Explanation
Remember that HttpServlet extends GenericServlet and provides HTTPspecific functionality. Thus, all its methods take HttpServletRequest and HttpServletResponse objects as parameters. Also, the method names follow the standard Java naming convention—for example, the method for processing POST requests is doPost() and not doPOST().
2.
Which of the following lines would initialize the out variable for sending a
Microsoft Word file to the browser?
Correct Answer
D. OutputStream out = response.getOuputStream();
Explanation
For sending any data other than text, you need to get the OutputStream object.
ServletResponse.getOutputStream() returns an object of type Servlet-
OutputStream, where ServletOutputStream extends OutputStream.
3.
In the context of Java concurrency, what is the primary function of the volatile keyword when applied to a variable?
Correct Answer
B. It guarantees that changes to the variable are visible to all threads.
Explanation
The volatile keyword in Java is used to indicate that a variable's value will be modified by different threads. Declaring a variable as volatile ensures that any write to the variable by one thread is immediately visible to other threads. This is achieved by preventing the variable from being stored in the local thread cache, thereby directly writing to and reading from the main memory, which maintains memory visibility across threads.
4.
Which element is used to specify useful information about an initialization
parameter of a servlet in the deployment descriptor?
Correct Answer
B. Description
Explanation
Remember that the description element is used for all the elements that can
take a description (useful information about that element). This includes servlet,
init-param, and context-param, among others. For a complete list of the
elements that can take a description, please read the DTD for web.xml.
5.
What file is the deployment descriptor of a web application named BankApp
stored in?
Correct Answer
E. Web.xml
Explanation
The deployment descriptor of a web application is always kept in a file named
web.xml, no matter what the name of the web application is.
6.
Your web application, named simpletax, depends on a third-party JAR file
named taxpackage.jar. Where would you keep this file?
Correct Answer
C. Simpletax/WEB-INF/lib
Explanation
All the classes that are packaged in a JAR file must be kept in the WEB-INF/lib
directory. The servlet container automatically adds all the classes in all the JAR files
kept in this directory to the classpath of the web application.
7.
Consider the following class:
import javax.servlet.*;
public class MyListener implements ServletContextAttributeListener
{
public void attributeAdded(ServletContextAttributeEvent scab)
{
System.out.println("attribute added");
}
public void attributeRemoved(ServletContextAttributeEvent scab)
{
System.out.println("attribute removed");
}
}
Which of the following statements about the above class is correct?
Correct Answer
B. This class will compile only if the attributeReplaced() method is added to it.
Explanation
ServletContextAttributeListener also declares the public void attributeReplaced(
ServletContextAttributeEvent scab) method, which
is called when an existing attribute is replaced by another one.
8.
Consider the following doPost() method of a servlet:
public void doPost (HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
System.out.println("Inside doPost");
PrintWriter out = response.getWriter();
out.println("Hello, ");
String name = getNameFromDBSomeHow();
if(name == null)
{
response.sendError(HttpServletResponse.SC_NOT_FOUND,
"Unable to get name.");
}
out.println(name);
}
Assuming that getNameFromDBSomeHow() returns null, which of the following
statements regarding this code are correct?
Correct Answer
D. It will throw an IllegalStateException while serving a request.
Explanation
When the sendError() method is called, the response is assumed to be committed.
If you write anything to the response after it gets committed, an Illegal-
StateException is thrown. In this case, sendError() is called if the name is
null. This commits the response. However, after the if condition, we have
out.println(name), which will cause the IllegalStateException to
be thrown.
9.
Which of the following tags can you use to print the value of an expression to the
output stream?
Correct Answer(s)
C.
D.
Explanation
You can use a JSP expression to print the value of an expression to the output stream.
For example, if the expression is x+3, you can write . But you can also use a scriptlet to print the value of an expression to the output stream as
.
10.
Which of the following correctly declares that the current page is an error page
and also enables it to take part in a session?
Correct Answer
C.
Explanation
The isErrorPage attribute accepts a Boolean value (true or false) and indicates whether the current page is capable of handling errors. The session attribute accepts a Boolean value (true or false) and indicates whether the current page must take part in a session. Since the pageType attribute is not a valid attribute for a page directive, answer is not correct. The mandatory value is not a valid value for the session attribute, which means answer is not correct. The errorPage attribute is a valid attribute, but it is used for specifying another page as an error handler for the current page. Therefore, that answer is also incorrect.
11.
What will be the output of the following code? (Select one)
x = ,
Correct Answer
C. X = 5, 3
Explanation
The above code will translate to servlet code similar to the following:
public class ...
{
int x = 7;
public void _jspService(…)
{
...
out.print("");
x = 3;
int x = 5;
out.write("x = "); out.print(x);
out.write(","); out.print(this.x);
out.print("");
}
}
The declaration will create a member variable x and initialize it to 7. The first
scriptlet, x=3, will change its value to 3. Then, the second scriptlet will declare a
local variable x and initialize it to 5. The first expression refers to the local variable
x and will therefore print 5. The second expression uses the keyword this to
refer to the member or instance variable x, which was set to 3. Thus, the correct
answer is c, x = 5, 3.
12.
Which of the following implicit objects is not available to a JSP page by default?
Correct Answer
C. Exception
Explanation
The implicit variables application and config are always available to a JSP
page. The implicit variable session is available if the value of the page directive’s
session attribute is set to true. Since it is set to true by default, the implicit
variable session is also available by default. The implicit variable exception is
available only if the value of the page directive’s isErrorPage attribute is set to
true. It is set to false by default, so the implicit variable exception is not
available by default. We have to explicitly set it to true:
13.
Consider the following code:
state =
Which of the following are equivalent to the third line above? (Select three)
Correct Answer(s)
B.
D.
F. State =
Explanation
the standard convention
that the beans follow is to capitalize the first character of the property’s
name. Therefore, it should be getState() and not getstate().
the method call address.getState() is in a declaration
instead of an expression.
the scriptlet and does not print any output.
14.
Which of the options locate the bean equivalent to the following action? (Select three)
Correct Answer(s)
A. Request.getAttribute("address");
D. PageContext.getAttribute("address",PageContext.REQUEST_SCOPE);
E. PageContext.getRequest().getAttribute("address");
Explanation
beans cannot be get or set as request parameters.
They can be get and set as request attributes.
getServletContext() returns an object of type ServletContext and Servlet-
Context has nothing to do with the request scope.
the methods getRequestAttribute() and getRequestParameter()
do not exist in PageContext.
15.
Consider the following code, contained in a file called this.jsp:
<jsp:include page="that.jsp" />
Which of the following is true about the AddressBean instance declared in this
code?
Correct Answer
A. The bean instance will not be available in that.jsp
Explanation
By default, the scope is page, so the bean is not available in that.jsp.
16.
Which of the following is a valid taglib directive?
Correct Answer
B.
Explanation
A taglib directive requires uri and prefix attributes
17.
You are automating computer parts ordering business. For this purpose, your
web application requires a controller component that would receive the requests
and dispatch them to appropriate JSP pages. It would also coordinate the request
processing among the JSP pages, thereby managing the workflow. Finally, the
behavior of the controller component is to be loaded at runtime as needed.
Which design pattern would be appropriate in this situation?
Correct Answer
A. Front Controller
Explanation
This is a standard situation for the Front Controller pattern. The Front Controller
receives all the requests and dispatches them to the appropriate JSP pages. This is
not the MVC pattern, because the question only asks about controlling the workflow.
You would choose the MVC pattern if it asked about controlling and presenting
the data in multiple views.
18.
What are the benefits of using the Data Access Object pattern? (Select two)
Correct Answer(s)
A. The type of the actual data source can be specified at deployment time.
B. The data clients are independent of the data source vendor API.
Explanation
This pattern is used to decouple business logic from data access logic. It hides the
data access mechanism from the business objects so that the data source can be
changed easily and transparently to the business objects.
19.
Which design pattern allows you to decouple the business logic, data representation, and data presentation? (Select one)
Correct Answer
A. Model-View-Controller
Explanation
In the Model-View-Controller pattern, Model is the data representation, View is
the data presentation, and Controller is the implementation of business logic.
20.
Which of the following are the benefits of using the Value Object design pattern?
(Select two)
Correct Answer(s)
A. It improves the response time for data access.
C. It reduces the network traffic.
Explanation
The Value Object pattern allows you to retrieve all the data elements in one
remote call instead of making multiple remote calls; therefore, it reduces the network
traffic and improves the response time since the subsequent calls to the
object are local.
21.
Which of the following statements are correct? (Select two)
Correct Answer(s)
B. It is wise to make the Value Object immutable if the Value Object represents readonly
data.
C. Applying the Value Object pattern on EJBs helps to reduce the load on enterprise beans.
Explanation
Making the Value Object immutable reinforces the idea that the Value Object is not a remote object and any changes to its state will not be reflected on the server.
clients require a fewer number of remote calls to retrieve attributes.
22.
What are the benefits of using the Business Delegate pattern? (Select three)
Correct Answer(s)
B. It shields the clients from the details of the access mechanism, such as CORBA or RMI, of the business services.
C. It shields the clients from changes in the implementation of the business services.
D. It provides the clients with a uniform interface to the business services.
Explanation
The clients delegate the task of calling remote business service methods to the Business Delegate. Thus, they are shielded by the business delegates from the access mechanism of the business services. The Business Delegate is meant to shield the clients from the implementation of the business services. It provides the clients with a uniform interface to the business services, which is a goal of this pattern. x---in correct Business Delegate does not implement any business service itself. It calls remote methods on the business services on behalf of the presentation. Business Delegate does not reduce the number of remote calls. It calls the remote methods on behalf of the client components.