Advanced Java Quiz

Reviewed by Samy Boulos
Samy Boulos, MSC, Computer Science |
Computer Expert
Review Board Member
With over 25 years of expertise, Samy is a seasoned Senior Technology Consultant. His extensive background spans diverse areas such as software development, data migration, Apple and Office 365 integration, computer helpdesk support, data engineering, and cloud computing. A dedicated professional, Samy combines technical proficiency with a strategic mindset, ensuring optimal solutions for complex technological challenges. His wealth of experience positions him as a reliable and knowledgeable consultant in navigating the ever-evolving landscape of IT and technology.
, MSC, Computer Science
Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By JQuizMaster
J
JQuizMaster
Community Contributor
Quizzes Created: 1 | Total Attempts: 9,679
Questions: 22 | Attempts: 9,812

SettingsSettingsSettings
Advanced Java Quiz - Quiz

Have you practiced Java enough that you are ready to learn advanced Java? Take this advanced Java quiz, and see how much you have learned. This will examine your understanding of JSP, Servlet, and Design patterns. You will get an idea about your understanding while taking the quiz and after checking the result. Give it a try and see what more you need to learn, or you know everything. All the best! Do share the quiz with other programming fans.


Questions and Answers
  • 1. 

    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)

    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().

    Rate this question:

  • 2. 

    Which of the following lines would initialize the out variable for sending a Microsoft Word file to the browser?

    • A.

      PrintWriter out = response.getServletOutput();

    • B.

      PrintWriter out = response.getPrintWriter();

    • C.

      PrintWriter out = response.getOuputStream();

    • D.

      OutputStream out = response.getOuputStream();

    • E.

      ServletOutputStream out = response.getServletOutputStream();

    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.

    Rate this question:

  • 3. 

    Which of the following methods would you use to retrieve header values from a request? (Select two)

    • A.

      GetHeader() of ServletRequest

    • B.

      GetHeaders() of HttpServletRequest

    • C.

      GetHeaderValue() of ServletRequest

    • D.

      GetHeader() of HttpServletRequest

    • E.

      GetHeaders() of ServletRequest

    • F.

      GetHeaders() of HttpServletRequest

    Correct Answer(s)
    D. GetHeader() of HttpServletRequest
    F. GetHeaders() of HttpServletRequest
    Explanation
    Headers are a feature of the HTTP protocol. Thus, all the header-specific methods belong to HttpServletRequest. getHeader() returns a String (or null), while getHeaders() returns an Enumeration of all the values for that header (or an empty Enumeration).

    Rate this question:

  • 4. 

    Which element is used to specify useful information about an initialization parameter of a servlet in the deployment descriptor?

    • A.

      Param-description

    • B.

      Description

    • C.

      Info

    • D.

      Param-info

    • E.

      Init-param-info

    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.

    Rate this question:

  • 5. 

    What file is the deployment descriptor of a web application named BankApp stored in?

    • A.

      BankApp.xml

    • B.

      Bankapp.xml

    • C.

      Server.xml

    • D.

      WebApp.xml

    • E.

      Web.xml

    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.

    Rate this question:

  • 6. 

    Your web application, named simpletax, depends on a third-party JAR file named taxpackage.jar. Where would you keep this file?

    • A.

      Simpletax/WEB-INF/thirdparty

    • B.

      Simpletax/WEB-INF/jars

    • C.

      Simpletax/WEB-INF/lib

    • D.

      Simpletax/WEB-INF/classes

    • E.

      Simpletax/WEB-INF

    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.

    Rate this question:

  • 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?

    • A.

      This class will compile as is.

    • B.

      This class will compile only if the attributeReplaced() method is added to it.

    • C.

      This class will compile only if the attributeUpdated() method is added to it.

    • D.

      This class will compile only if the attributeChanged() method is added to it.

    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.

    Rate this question:

  • 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?

    • A.

      It will throw an InvalidStateException while serving a request.

    • B.

      It will throw a ServletException while serving a request.

    • C.

      It will throw a NullPointerException while serving a request.

    • D.

      It will throw an IllegalStateException while serving a request.

    • E.

      It will not throw an exception.

    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.

    Rate this question:

  • 9. 

    Which of the following tags can you use to print the value of an expression to the output stream?

    • A.

    • B.

    • C.

    • D.

    • E.

    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
    .

    Rate this question:

  • 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?

    • A.

    • B.

    • C.

    • D.

      None of the above.

    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.

    Rate this question:

  • 11. 

    What will be the output of the following code? (Select one) x = ,

    • A.

      X = 3, 5

    • B.

      X = 3, 7

    • C.

      X = 5, 3

    • D.

      Compilation error

    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.

    Rate this question:

  • 12. 

    Which of the following implicit objects is not available to a JSP page by default?

    • A.

      Application

    • B.

      Session

    • C.

      Exception

    • D.

      config

    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:

    Rate this question:

  • 13. 

    Consider the following code: state = Which of the following are equivalent to the third line above? (Select three)

    • A.

    • B.

    • C.

    • D.

    • E.

      State =

    • F.

      State =

    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.

    Rate this question:

  • 14. 

    Which of the options locate the bean equivalent to the following action? (Select three)

    • A.

      Request.getAttribute("address");

    • B.

      Request.getParameter("address");

    • C.

      GetServletContext().getRequestAttribute("address");

    • D.

      PageContext.getAttribute("address",PageContext.REQUEST_SCOPE);

    • E.

      PageContext.getRequest().getAttribute("address");

    • F.

      PageContext.getRequestAttribute("address");

    • G.

      PageContext.getRequestParameter("address");

    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.

    Rate this question:

  • 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?

    • A.

      The bean instance will not be available in that.jsp

    • B.

      The bean instance may or may not be available in that.jsp, depending on the threading model implemented by that.jsp.

    • C.

      The bean instance will be available in that.jsp, and the that.jsp page can print the values of the beans properties using .

    • D.

      The bean instance will be available in that.jsp and the that.jsp page can print the values of the bean’s properties using only if that.jsp also contains a declaration identical to the one in this.jsp and before using .

    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.

    Rate this question:

  • 16. 

    Which of the following is a valid taglib directive?

    • A.

    • B.

    • C.

    • D.

    • E.

    Correct Answer
    B.
    Explanation
    A taglib directive requires uri and prefix attributes

    Rate this question:

  • 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?

    • A.

      Front Controller

    • B.

      Session Facade

    • C.

      Value Object

    • D.

      Model-View-Controller

    • E.

      Data Access Object

    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.

    Rate this question:

  • 18. 

    What are the benefits of using the Data Access Object pattern? (Select two)

    • 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.

    • C.

      It increases the performance of data-accessing routines.

    • D.

      It allows the clients to access the data source through EJBs.

    • E.

      It allows resource locking in an efficient way.

    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.

    Rate this question:

  • 19. 

    Which design pattern allows you to decouple the business logic, data representation, and data presentation? (Select one)

    • A.

      Model-View-Controller

    • B.

      Value Object

    • C.

      Bimodal Data Access

    • D.

      Business Delegate

    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.

    Rate this question:

  • 20. 

    Which of the following are the benefits of using the Value Object design pattern? (Select two)

    • A.

      It improves the response time for data access.

    • B.

      It improves the efficiency of object operations.

    • C.

      It reduces the network traffic.

    • D.

      It reduces the coupling between the data access module and the database.

    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.

    Rate this question:

  • 21. 

    Which of the following statements are correct? (Select two)

    • A.

      The Value Object pattern ensures that the data is not stale at the time of use.

    • 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.

    • D.

      A Value Object exists only on the server side.

    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.

    Rate this question:

  • 22. 

    What are the benefits of using the Business Delegate pattern? (Select three)

    • A.

      It implements the business service functionality locally to improve performance.

    • 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.

    • E.

      It reduces the number of remote calls and reduces network overhead.

    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.

    Rate this question:

Samy Boulos |MSC, Computer Science |
Computer Expert
With over 25 years of expertise, Samy is a seasoned Senior Technology Consultant. His extensive background spans diverse areas such as software development, data migration, Apple and Office 365 integration, computer helpdesk support, data engineering, and cloud computing. A dedicated professional, Samy combines technical proficiency with a strategic mindset, ensuring optimal solutions for complex technological challenges. His wealth of experience positions him as a reliable and knowledgeable consultant in navigating the ever-evolving landscape of IT and technology.

Quiz Review Timeline +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Feb 07, 2024
    Quiz Edited by
    ProProfs Editorial Team

    Expert Reviewed by
    Samy Boulos
  • Jan 18, 2012
    Quiz Created by
    JQuizMaster
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.