Advance Java MCQ Questions and Answers

11. How constructor can be used for a servlet?

  1. Initialization and Constructor function
  2. Constructor function
  3. Setup() method
  4. Initialization

Answer : A
Explanation: Correct answer is the Initialization and Constructor function as we cannot declare constructors for interface in Java. This means we cannot enforce this requirement to any class which implements the Servlet interface and also, Servlet requires ServletConfig object for initialization which is created by the container.

12. Which package is used for importing swing components?

  1. javax.swing
  2. java.swing
  3. java.awt.
  4. All of the mentioned

Answer : A
Explanation: javax.swing is used for importing swing components.

13. Which of the following is/are steps for the servlet life cycle?

  1. Servlet class is loaded
  2. Servlet instance is created
  3. init,Service,destroy method is invoked
  4. All of the mentioned

Answer : D
Explanation: All of the above are steps for servlet life cycle.

14. A servlet is a Java program that is invoked via HTTP on the webserver.

  1. True
  2. False

Answer : A
Explanation: Yes it is correct that a servlet is a Java program that is invoked via HTTP on the webserver.

15. Which of the following code is used to get an attribute in a HTTP Session object in servlets?

  1. session.alterAttribute(String name)
  2. session.updateAttribute(String name)
  3. session.getAttribute(String name)
  4. session.setAttribute(String name)

Answer : C
Explanation: session.getAttribute(String name) is used to get an attribute in a HTTP Session object in servlets.

16. How can a servlet call a JSP error page?

  1. When the servlet throws the exception, it will automatically be caught by the calling JSP page
  2. This capability is not supported
  3. The servlet needs to forward the request to the specific error page URL. The exception is passed along as an attribute named “javax.servlet.jsp.jspException”
  4. The servlet needs to redirect the response to the specific error page, saving the exception off in a cookie

Answer : C
Explanation: The servlet needs to forward the request to the specific error page URL. The exception is passed along as an attribute named “javax.servlet.jsp.jspException” and so option C is correct.

18. __________ is used to read data from a client request.

  1. ServletResponse
  2. ServletRequest
  3. Servlet
  4. ServletConfig

Answer : B
Explanation: ServletRequest is used to read data from a client request.

19. Which of the following ways to create a servlet?

  1. Using Servlet Interface
  2. Using GenericServlet class
  3. Using HttpServlet class
  4. All of given

Answer : D
Explanation: Different ways to create a servlet are: using Servlet Interface, using GenericServlet class, using HttpServlet class.

20. Which component is used to compile, debug and execute the java program?

  1. JIT
  2. JDK
  3. JVM
  4. JRE

Answer : B
Explanation: JDK is a core component of Java Environment and provides all the tools, executables, and binaries required to compile, debug and execute a Java Program so option B is correct.

This Post Has 2 Comments

Leave a Reply