OCEJWCD 6 Mock Exam – 7

1 . Consider the following code:

public void doGet(HttpServletRequest req, HttpServlerResponse res)
{
    PrintWriter out = res.getWriter();
    out.println("Unable to find resource.");
    //1
    response.sendError(404);
}

Which of the following lines, when inserted at //1, will ensure that an IllegalStateException is NOT thrown?

Choose the one below:

  1. res.clear();
  2. res.empty();
  3. res.remove();
  4. if(!res.isCommitted())
  5. if( res.getStatus() != res.COMMITTED)

2 . Which of the following statements are correct JSP directives?

  1. <%@ page %>
  2. <%! taglib uri=”http://www.abc.com/tags/util” prefix=”util” %>
  3. <% include file=”/copyright.html”%>
  4. <%@ taglib uri=”http://www.abc.com/tags/util” prefix=”util” %>
  5. <%$ page language=”java” import=”com.abc.*”%>

3 . Consider the following contents for two JSP files:

In file companyhome.jsp:

&lt;html&gt;&lt;body&gt;

Welcome to ABC Corp!

&lt;jsp:include page=&quot;companynews.jsp&quot; /&gt;

&lt;/body&gt;&lt;/html&gt;

&lt;%@ page errorPage=&quot;simpleerrorhandler.jsp&quot; %&gt;

 

In file companynews.jsp:

&lt;%@ page errorPage=&quot;advancederrorhandler.jsp&quot; %&gt;

&lt;h3&gt;Todays News&lt;/h3&gt;

Choose the one below:

  1. When companyhome.jsp is requested, the output will contain “welcome…” as well as “Todays News”
  2. companyhome.jsp will not compile
  3. companynews.jsp will not compile
  4. Both the files will compile but will throw an exception at runtime
  5. None of these

4 . Which HTTP method is used in FORM based Authentication?

  1. POST
  2. GET
  3. FORM
  4. HEAD

5 . Consider the following code snippets. What will be displayed on the browser when a GET request is sent to FirstServlet assuming that the buffer is large enough to hold all the data before sending the data to the client?

In the doGet() of FirstServlet:

    PrintWriter out = response.getWriter();

    out.println(&quot;&lt;html&gt;&lt;body&gt;Page 1&quot;);

    RequestDispatcher rd = response.getRequestDispatcher(&quot;SecondServlet&quot;);

    rd.forward(request, response);

 

In the doGet() of SecondServlet:

    PrintWriter out = response.getWriter();

    out.println(&quot;&lt;br&gt;Page 2&lt;/body&gt;&lt;/html&gt;&quot;);

Choose the one below:

  1. Only Page1
  2. Only Page2
  3. Page1 and Page2
  4. IllegalStateException at Runtime

6 . Which of the given options are equivalent?

  1. <% Hashtable ht = new Hashtable(); %>
  2. <%= Hashtable ht = new Hashtable() %>
  3. <jsp:scriptlet>Hashtable ht = new Hashtable();</jsp:scriptlet>
  4. <jsp:code>Hashtable ht = new Hashtable();</jsp:code>
  5. <jsp:scriptlet>Hashtable ht = new Hashtable()</jsp:scriptlet>

7 . When a session becomes invalid, which method is invoked on a session attribute implementing an appropriate interface?

  1. sessionInvalidated() of HttpSessionListener
  2. sessionDestroyed() of HttpSessionListener
  3. valueUnbound() of HttpSessionAttributeListener
  4. valueUnbound() of HttpSessionBindingListener
  5. valueUnbound() of HttpSessionListener

8 . Your jsp page connects to the database and retrieves the data.It also formats the data and displays it to the client.Any of these operations can throw exceptions but you do not want to catch them all in this page and so you have written another jsp page that is meant to handle any kind of exceptions.
How would you associate that error page named “error.jsp” with this page?

  1. Add <%@errorPage=”error.jsp”%> in this page
  2. Add <%@page errorPage=”error.jsp”%> in this page
  3. Add <%@page isErrorPage=”true”%> in error.jsp
  4. Add <%@isErrorPage=”true”%> in error.jsp

9 . Consider the HTML code shown in the exhibit. Which of the following method calls can retrieve the “email” value sent from the browser?

&lt;html&gt;&lt;body&gt;

  &lt;form action=&quot;/myapp/servlet/EmailCatcherServlet&quot;&gt;

    Please enter your email: &lt;input type=&quot;text&quot; name=&quot;email&quot;&gt;

    &lt;input type=&quot;submit&quot;&gt;

  &lt;/form&gt;

&lt;/body&gt;&lt;/html&gt;

Choose the one below:

  1. getParameter(“email”) of ServletRequest
  2. getParameterValues(“email”) of ServletRequest
  3. getField(“email”) of HttpServletRequest
  4. getFormValue(“email”) of HttpServletRequest
  5. getParameters(“email”) of HttpServlet

10 . What will be the output?

&lt;% { %&gt;

    &lt;jsp:useBean id=&quot;sb&quot; class=&quot;java.lang.StringBuffer&quot; /&gt;

    sb.append(&quot;Hello&quot;);

&lt;% } %&gt;

&lt;%=sb%&gt;

Choose the one below:

  1. It will print null
  2. It will print “Hello”
  3. It will not compile because <jsp:useBean> cannot be used inside a block
  4. It will not compile because StringBuffer is not a bean
  5. None of these

11 . Which HTTP method would you use to test the validity, accesibility, or modification time of a hyperlink?

  1. GET
  2. POST
  3. HEAD
  4. OPTIONS
  5. PUT

12 . A function has been defined in a tag library descriptor as follows:

&lt;taglib&gt;

  ...

  &lt;function&gt;

    &lt;name&gt;transform&lt;/name&gt;

    &lt;function-class&gt;com.enthu.Functions&lt;/function-class&gt;

    &lt;function-signature&gt;java.lang.String transformString(String)&lt;/function-signature&gt;

  &lt;/function&gt;

  ...

&lt;/taglib&gt;

Which of the following statements are correct?

Choose the one below:

  1. The Function class must have a method with the signature: public String transform(String arg);
  2. The Function class must have a function with the signature: public String transformString(String arg);
  3. The Function class may have any method of the type: public static XXX(String s); but the method name (ie. XXX) must be mapped to “transform” in web.xml
  4. The Function class may have any method of the type: public static XXX(String s); but the method name (ie. XXX) must be mapped to “transformString” in web.xml
  5. None of these

13 . In your report.jsp page, you want to include the output of “customer.jsp” page. But this page requires an additional parameter “custid”, which is not present in the request?
Which of the following code snippents does this?

1.  &lt;jsp:include page=&quot;customer.jsp&quot; custid=&quot;1234&quot;/&gt;

2.  &lt;jsp:include page=&quot;customer.jsp&quot;&gt;

    &lt;jsp:param&gt;

    &lt;name&gt;custid&lt;/name&gt;

    &lt;value&gt;1234&lt;/value&gt;

    &lt;/jsp:param&gt;

    &lt;/jsp:include&gt;

3.  &lt;jsp:include page=&quot;customer.jsp&quot; param-name=&quot;custid&quot; param-value=&quot;1234&quot;/&gt;

4.  &lt;jsp:include page=&quot;customer.jsp&quot;&gt;

    &lt;jsp:param name=&quot;custid&quot; value=&quot;1234&quot;/&gt;

    &lt;/jsp:include&gt;

Choose the one below:

  1. 1
  2. 2
  3. 3
  4. 4

14 . Which of the following statements are correct regarding the import tag of JSTL?

  1. The String value of the imported content can be made available for use outside of the tag in ‘var’ variable
  2. If the url contains a relative path, then the resource must exist in the same webapp
  3. import tag is more efficient than jsp:include action in the case when large amount of data is imported only to be given to another tag
  4. import tag is useful to convert URL when cookies are not supported by the client
  5. The content imported by the import tag can be made available only through a String object

15 . What are the implications of using the HTTP GET method for a form submission?

  1. You cannot pass binary data to the server
  2. You cannot send unlimited (or a lot of) data to the server
  3. You cannot send multiple values for one parameter to the server
  4. You can only reply with the HEADER information in the response
  5. The parameters will be appended to the URL as a query string

16 . It important to note that response of a POST request are never cached?

1.  &lt;taglib&gt;

    &lt;taglib-uri&gt;/binomial&lt;/taglib-uri&gt;

    &lt;taglib-location&gt;/WEB-INF/MathLib.tld&lt;/taglib-location&gt;

    &lt;/taglib&gt;

2.  &lt;taglib&gt;

    &lt;taglib-uri&gt;/binomial&lt;/taglib-uri&gt;

    &lt;taglib-location&gt;/WEB-INF/MathLib.jar&lt;/taglib-location&gt;

    &lt;/taglib&gt;

3.  &lt;taglib id=&quot;ABC_MATH_LIB&quot;&gt;

    &lt;taglib-uri&gt;/binomial&lt;/taglib-uri&gt;

    &lt;taglib-location&gt;/WEB-INF/MathLib.tld&lt;/taglib-location&gt;

    &lt;/taglib&gt;

4.  &lt;taglib name=&quot;ABC_MATH_LIB&quot;&gt;

    &lt;taglib-uri&gt;/binomial&lt;/taglib-uri&gt;

    &lt;taglib-location&gt;/WEB-INF/MathLib.jar&lt;/taglib-location&gt;

    &lt;/taglib&gt;

5.  &lt;taglib author=&quot;ABCINC&quot;&gt;

    &lt;taglib-uri&gt;/binomial&lt;/taglib-uri&gt;

    &lt;taglib-location&gt;/WEB-INF/MathLib.tld&lt;/taglib-location&gt;

    &lt;/taglib&gt;

Choose the one below:

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5

17 . Consider the following jsp code:

&lt;html&gt;

  &lt;head&gt;

    &lt;% int k = 0; %&gt;

  &lt;/head&gt;

&lt;body&gt;

&lt;/body&gt;

&lt;/html&gt;

In which method of the generated servlet will the declaration for ‘k’ be placed?

Choose the one below:

  1. This will not compile as you cannot put jsp code in the <head> element
  2. init()
  3. doGet()
  4. _jspService()
  5. constructor of the servlet

18 . Which event is received by a registered listener when an attribute is added to HttpSession?

  1. HttpSessionChangeEvent
  2. HttpSessionEvent
  3. HttpAttributeChangeEvent
  4. HttpSessionBindingEvent
  5. HttpAttributeEvent

19 . You are using a tag library with prefix “generator”, which supports a tag named “random”.This tag generates a random number and sets it to a variable named “value”?
Which of the following will output this value in the page?

Choose the one below:

  1. <generator:random>value</generator:random>
  2. <generator:random><%=value%></generator:random>
  3. <generator:random><% int value;%> <%=value%></generator:random>
  4. <generator:random><%getParameter(“value”)%></generator:random>
  5. None of the above

20 . Identify the techniques that can be used to implement ‘sessions’ if the client browser does not support cookies?

  1. Using Http headers
  2. Using https protocol
  3. Hidden form fields
  4. URL rewriting
  5. It cannot be done without cookie support

21 . Which of the following are valid JSP scriptlets?

  1. <% String uid = LoginHelper.login(request) %>
  2. <% String uid = LoginHelper.login(request); %>
  3. <%! String uid = LoginHelper.login(request) %>
  4. <%@ String uid = LoginHelper.login(request) %>
  5. <% for(int i=0; i< 10; i++) { out.println(i); } %>

22 . Consider the following web.xml code snippet:


&lt;servlet&gt;

        &lt;servlet-name&gt;BankServlet&lt;/servlet-name&gt;

        &lt;servlet-class&gt;com.abc.bankapp.BankServlet&lt;/servlet-class&gt;

        &lt;security-role-ref&gt;

            &lt;role-name&gt;manager&lt;/role-name&gt;

            &lt;role-link&gt;supervisor&lt;/role-link&gt;

        &lt;/security-role-ref&gt;

    &lt;/servlet&gt;

Which of the following statements are correct?

Choose the one below:

  1. The servlet code should use “manager” as a parameter in request.isUserInRole() method
  2. The servlet code can use “manager” or “supervisor” as a parameter in request.isUserInRole() method
  3. The servlet code should use”supervisor” as a parameter in request.isUserInRole() method
  4. The role of “manager” must be defined in the servlet container
  5. None of these

23 . You are designing a complex webapp that uses multi tier architecture. The application must provide interfaces for HTML as well as XML and should be maintainable?
Which design pattern would you use?

Choose the one below:

  1. Data Access Object
  2. Business Deligate
  3. MVC
  4. Remote Method Invocation
  5. Transfer Object

24 . Which of the following directives are applicable ONLY for tag files?

  1. attribute
  2. variable
  3. page
  4. include
  5. import
  6. tag

25 . Which of the following are correct about FORM based authentication mechanism?

  1. HTML FORM is used to capture the username and password of the user
  2. Password is transmitted as plain text
  3. Password is transmitted in an encrypted form
  4. Password is transmitted either in encrypted text or in plain text depending on the browser
  5. This mechanism can be used over HTTPS

26 . Which pattern allows you to replace the presentation logic without much impact on the data representation?

  1. Model View Controller
  2. Business Delegate
  3. Transfer Object
  4. Data Access Object
  5. Bimodal DataAccess

27 . Identify the elements that help describe the attribute characteristics of a JSP custom tag in a TLD file?

  1. value
  2. name
  3. description
  4. rtexprvalue
  5. class

28 . Select the correct return types for ServletContext.getResource() and ServletContext.getResourceAsStream() methods?

  1. java.io.Resource and java.io.InputStream
  2. java.io.Resource and java.io.BufferedInputStream
  3. java.net.URL and java.io.InputStream
  4. java.io.File and java.io.InputStream
  5. java.net.URL and java.io.FileInputStream

29 . Which of the following are valid values for the <transport-guarantee> element?

  1. CONFIDENTIAL
  2. INTEGRAL
  3. SECURE
  4. ENCRYPTED
  5. NONE

30 . Which method of ServletResponse would you use to set its content type?

  1. setParameter
  2. setHeader
  3. setAttribute
  4. setContentType
  5. None of the above

31 . Which of the following lines can be used to retrieve a servlet initialization parameter “dbname” from the init() method of a servlet?

public void init()

{

 String dbname = //1 : Insert line here

}

Choose the one below:

  1. getServletConfig().getParameter(“dbname”);
  2. getServletConfig().getInitParameter(“dbname”);
  3. getServletContext().getInitParameter(“dbname”);
  4. getInitParameter(“dbname”); E.getInitParameterValue(“dbname”);

Answers

1 : 4 is correct.

2 : 1 & 4 is correct.

3 : 1 is correct.

4 : 1 is correct.

5 : 2 is correct.

6 : 1 & 3 is correct.

7 : 4 is correct.

8 : 2 & 3 is correct.

9 : 1 & 2 is correct.

10 : 5 is correct.

11 : 3 is correct.

12 : 5 is correct.
Explanation: The Function class must have a method with signature: public static String transformString(String arg);
1 is incorrect. The method name must be the same as given in <function-signature> element.
2 is incorrect. Method name is correct but it should also be static.

13 : 4 is correct.

14 : 1 & 3 is correct.

15 : 1,2 & 5 is correct.

16 : 1,2 & 3 is correct.

17 : 4 is correct.
Explanation: This is a scriptlet and they always go inside the service method. ie. they are local to the request. 1 is incorrect. You can put JSP code anywhere

18 : 4 is correct.
Explanation:Following is the mapping of Listeners and their corresponding events.
HttpSessionListener : HttpSessionEvent
HttpSessionAttributeListener : HttpSessionBindingEvent
HttpSessionBindingListener : HttpSessionBindingEvent
Observe that both – HttpSessionAttributeListener and HttpSessionBindingListener, use HttpSessionBindingEvent.

19 : 2 is correct.
Explanation: 3 is incorrect It’ll give an exception saying value is not initialized!

20 : 2,3 & 4 is correct.
Explanation: 2 is correct Unlike HTTP, HTTPS uses SSL which is a stateful protocol.
3 is correct Remember that this is a non-standard and obsolete way.
This can only be done in an application specific way and requires that the page has a form.

21 : 2 & 5 is correct.
Explanation: 1 is incorrect. It does not have an ending semicolon
3 is incorrect. ! is used for declarations
4 is incorrect. @ is used for directives

22 : 1 is correct.
Explanation:4 is incorrect “supervisor” must be defined in the container. For example, in conf/tomcat-users.xml for Tomcat.
<security-role-ref> is used to map the role names hard coded in the servlet code to the actual role names defined in the servlet container.

23 : 3 is correct.
Explanation: The statement “…should provide XML and HTML interfaces…” means the same data is represented in different ways, therefore this is MVC.

24 : 1,2 & 6 is correct.
Explanation: 2 Valid only for tag files.
4 is incorret include directive is valid for regular JSP file also
5 is incorrect No such directive

25 : 1,2 & 5 is correct.
Explanation: 3 is incorrect This is done in HTTP Digest authentication mechanism

26 : 1 is correct.
Explanation: 1 is correct. A view (ie. the presentation of data) knows how to present the data and so can be replaced with another view without any impact on the data representation.
2 is incorrect. This allows plug and play between back end logic and the front end.
4 is incorrect. This allows plug and play between the data container (the DB) and data requestor.

27 : 2,3 & 4 is correct.
Explanation: An attribute element describes the attribute for a tag. Following is its definition.
<!ELEMENT attribute (name, required? , rtexprvalue?, type?, description?) >

28 : 3 is correct.

29 : 1,2 & 5 is correct.

30 : 4 is correct.

31 : 2 & 4 is correct.
Explanation: Calling getServletConfig() from the init() method returns the ServletConfig object for this servlet and calling getInitParameter(…) on the ServletConfig object returns the value of that parameter.

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*