JSP and Servlets MCQs

JSP and Servlets MCQs

These JSP and Servlets multiple-choice questions and their answers will help you strengthen your grip on the subject of JSP and Servlets. You can prepare for an upcoming exam or job interview with these 100+ JSP and Servlets MCQs.
So scroll down and start answering.

1: Given the following code from a servlet, and given that myvar is a reference to either an HttpSession or a ServletContext: 1. myVar.setAttribute(“myName”, “myVal”); 2. String s = (String) myVar.getAttribute(“myName”); // more code After line 2 executes, which is true?

A.   If myVar is an HttpSession, s is guaranteed to have the value “myVal”

B.   The value of s cannot be guaranteed

C.   If myVar is a ServletContext, compilation will fail

D.   If myVar is an HttpSession, compilation will fail

2: init() or jspInit() is invoked by the container on a servlet or a jsp?

A.   For each request that causes a new session to be created

B.   For each request that causes a new thread to be created

C.   For each request

D.   Only once in the life time

3: Which of the following is an INVALID JSP action?

A.   jsp:useBean

B.   jsp:setProperty

C.   jsp:setSession

D.   jsp:getProperty

4: JSP stands for ____.

A.   Java Servlet Pages

B.   Justified Secure Pages

C.   Java Server Pages

D.   Java Server Program

5: JSP uses what kind of syntax for an expression?

A.   < expression />

B.   <=% expression %>

C.   <%= expression %>

D.   <% expression %>

6: How does container process a JSP page after compilation?

A.   POJO

B.   HTML

C.   Applet

D.   Servlet

7: a JavaBean is basically a:

A.   a taglib

B.   a http request

C.   a sql update

D.   class

8: A scriptlet is a piece of code that can be embedded inside the HTML, however it has to be inside which tags?

A.   <@ java code @>

B.   <% java code %>

C.   <$ java code $>

D.   <# java code #>

9: If you wanted to print out the html from your program, which would you use?

A.   browser.println();

B.   page.println();

C.   out.println();

D.   html.println();

10: JSP caches data on a web server called___.

A.   WildCat

B.   Tomcat

C.   MySQL

D.   FaceBook

11: JSP's are written in____.

A.   PHP

B.   C#

C.   javascript

D.   Java

12: The correct way to import the input/ouput java library is:

A.   <%% Page import="java.io.*" %>

B.   <%@ page import="java.io.*" %>

C.   <%-- Page import="java.io.*" %>

D.   <%&& Page import="java.io.*" %>

13: JavaScript is to the client as JSP is to the ____.

A.   database

B.   the server

C.   C#

D.   smartphone

14: What is the proper Try Catch clause?

A.   try() catch()

B.   catch() { } try { }

C.   try [] catch() []

D.   try { } catch () { }

15: The correct way to import the text library in your JSP page is:

A.   <%@ page import="java.text.*" %>

B.   <%= Page import="java.text.*" %>

C.   <%& Page import="java.text.*" %>

D.   <%() Page import="java.text.*" %>

16: The correct syntax for a IF statement in Java is:

A.   if {} ( )

B.   if [] { }

C.   if <> {}

D.   if () { }

17: JSP are coded in:

A.   VB.Net

B.   Java

C.   Python

D.   C#

18: The correct way to instantiate a new date object is:

A.   Date() myDate[];

B.   Declare myDate = new Date();

C.   myDate = Date();

D.   Date myDate = new Date();

19: Data runs between the client and the server via a ___ request.

A.   Kornshell script

B.   HTTP

C.   port 1433

D.   a sql request

20: What is the correct way to test if x is equal to 1?

A.   if(x ==+ 1) { }

B.   if(x == 1) { }

C.   if(x += 1) { }

D.   if(x = 1) { }

21: Which file is a configuration file for JSP?

A.   JSP.xml

B.   Client.xml

C.   HTML.aspx

D.   web.xml

22: How are JSP comments written?

A.   <%-- comment --%>

B.   <// comment //>

C.   < comment />

D.   <%= comment =>

23: The correct way to create a single line comment is:

A.   () comment

B.   // comment

C.   ?/ comment

D.   # comment

24: The correct way to create a string in Java is:

A.   var_str = String("this");

B.   var_str = "this";

C.   String var_str = "this";

D.   Declare var_str = "this";

25: Which one is the correct syntax for JSTL?

A.   <%= taglib prefix="c" uri="http..." %>

B.   <%@ taglib prefix="c" uri="http..." %>

C.   <%-- taglib prefix="c" uri="http..." %>

D.   <%() taglib prefix="c" uri="http..." %>

26: A Servlet runs on ____

A.   the client

B.   a mobile phone

C.   the server

D.   in the browser

27: HTTP is stateless. What does that mean?

A.   Javascript holds all the data.

B.   All data is stored in the database

C.   Once a connection is open then closed, the web server doesn't recognize the request or any past requests.

D.   Nothing. Its not an important concept.

28: Which of the following files contains the mapping for and ?

A.   web.xml

B.   spring.xml

C.   server.xml

D.   config.xml

29: Service method of a generic servlet takes which of the following parameters?

A.   ServletConfig

B.   Both ServletRequest & ServletResponse

C.   ServletResponse

D.   ServletRequest

30: Which of the following methods is most appropriate to send the large amount of data?

A.   POST

B.   HIDDEN

C.   GET

D.   HEAD

31: A JSP directive has the following form:

A.   <%@ page %>

B.   <%^ Page %>

C.   <%= Page %>

D.   <%# Page %>

32: What is the proper taglib for SQL?

A.   <sql:update> </sql:update/>

B.   <sql:update> </sql:update>

C.   <sql|update> </sql=update>

D.   <sql:update> <//sql:update>

A.   Cookie cookie = new Cookie("key","value")

B.   JSP cookie = new JSPCookie("key","value")

C.   Create new cookie()

D.   There is no way to call a cookie

34: To deploy and run JavaServer Pages you need...

A.   a compatible web server container

B.   a compatible web server with a servlet container

C.   a compatible web server without a servlet container

35: Another way to track user data between pages is:

A.   session and Cookies

B.   Cookies

C.   HTML

D.   ASP

E.   session

36: Which is the best place to define error pages for JSPs?

A.   server.xml

B.   hibernate.cfg.xml

C.   context.xml

D.   web.xml

37: If submit a form without mentioning the method type(get or post) and called servlet. which method will invoke? doGet() or doPost().

A.   By default it is a GET request

B.   By default it is a Post request

38: When a web browser asks for a JSP page, the first thing the JSP engine does is:

A.   checks to see if HTML 5 is being used

B.   see if the page needs to be compiled

C.   checks to see if the browser is Safari or IE

D.   checks to see if the cookies are enabled

39: public class SomeServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setAttribute("a", "value") ; .... } } What would be the scope of a attribute ?

A.   Page Scope

B.   Session Scope

C.   Application Scope

D.   Request Scope

40: The 3 ways to maintain session between web client and the server are:

A.   Cookies, HTML, URL rewrite

B.   Web server, HTML, ASP

C.   Cookies, hidden fields, URL rewrite.

D.   HTML, Database, javascript

41: A container invokes ____ method before taking care of any requests.

A.   page_load()

B.   jspDestroy()

C.   jspInit()

42: Which of the follolwing servlet methods gets called for each client request?

A.   ServletConfig getServletConfig()

B.   public void init() throws ServletException

C.   public void destroy()

D.   public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException{

43: A JSP comment is ___ in the page code.

A.   highlighted

B.   not included

C.   promoted

D.   executed first

44: Which of the following JSP action can be described as following : Action to substitute the referenced resource's content for the forwarding page's content?

A.   jsp:forward

B.   jsp:useBean

C.   jsp:param

D.   jsp:include

45: Which of the following is an INVALID bean(object) scope?

A.   config

B.   page

C.   session

D.   request

46: Which of the following are valid sub-elements of taglib?

A.   tag-location

B.   taglib-uri

C.   taglib-location

D.   Both taglib-uri and taglib-location

47: What is the correct definition for ?

A.   tries to find an object, creates it if it does not exist

B.   none of these options are correct

C.   does not do anything

D.   converts HTML to a class

48: The JSP container calls the which of the following method if it needs the page to release resources?

A.   jspRelease()

B.   jspInit()

C.   jspDestroy()

D.   jspService()

49: If a jsp page uses classes from java.util package. Which of the following statements would import the package?

A.   <%@import java.util.* %>

B.   <%@ page import="java.util.*"%>

C.   <%import="java.util.*"@%>

D.   <%@ page java="java.util.*"@%>

50: Which code block is correct?

A.   public string myFunc(String name) { return name;}

B.   public myFunc(String name) { return name;}

C.   None of these

D.   public string myFunc(String name) { }

51: Which of the implicit JSP object represents the servlet configuration information?

A.   config

B.   request

C.   response

D.   pageContext

52: What's the difference between servlets and applets? 1.Servlets executes on Servers, where as Applets executes on Browser 2.Servlets have no GUI, where as an Applet has GUI 3. Both of them 4. None of them

A.   1

B.   4

C.   3

D.   2

53: JSP renders pages vis a ____ Life Cycle.

A.   JSP

B.   HTML

C.   Python

D.   Java

54: What does JSTL stand for?

A.   Java Supports Tag Library

B.   Just Serve The Library

C.   JavaScript Teacher Library

D.   Java Server Pages Standard Library

55: Which of the following actions is a specialized tag that generates the appropriate or tag to load the Java Plug-in software?

A.   jsp:plugin

B.   jsp:setProperty

C.   jsp:getProperty

D.   jsp:useBean

56: Which of the following root element provides the global configuration information for the JSP files in a Web application?

A.   jsp-config

B.   taglib

C.   taglib-uri

D.   jsp-property-group

57: Which of the following is a Scriplet?

A.   <% Java statements %>

B.   <%! declarations %>

C.   <%= Java expression %>

D.   <%-- Comment --%>

58: If req is a reference to an HttpServletRequest and there is no current session, what is true about req.getSession() ?

A.   Invoking req.getSession(false) will return a new session

B.   Invoking req.getSession() will return null

C.   Invoking req.getSession(true) will return a new session

D.   Invoking req.getSession(null) will return a new session

59: The Web container creates JSP implicit objects like...

A.   pageContext and servletContext but not session object

B.   pageContext, servletContext, session, request & response

C.   session, request & response but not servletContext

60: Which of the following provides a handle to access page directive attributes in the scripting environment?

A.   application

B.   pageContext

C.   config

D.   context

61: Which of the following is a valid JSP expression?

A.   <%some-java-expression %>

B.   <%= some-java-expression ;%>

C.   <%some-java-expression ;%>

D.   <%= some-java-expression %>

62: When might a JSP get translated (Choose all that apply)? a. When the application is started b. When the developer compiles code in the src folder c. The first time a user requests the JSP d. After jspdestroy() is called, it gets retranslated

A.   a and d

B.   a and c

C.   a only

D.   c and d

63: How can you call a servlet to autofill a dropdown list which depends on selection from previous 2 dropdown lists? 1. Print all possible values of the 2nd and 3rd dropdown out as a Javascript object 2. Make use of XMLHttpRequest in Javascript to fire an asynchronous request to a servlet during the onchange event 3. Both of them 4. None of them

A.   2

B.   3

C.   4

D.   1

64: Which of the following statement correctly describe attribute 'buffer'?

A.   By default, the page output is buffered with an implementation buffer size no smaller than 80 MB

B.   It indicates whether the content output from the page will be buffered

C.   By default, the page output is buffered with an implementation buffer size no smaller than 800 MB

D.   By default, the page output is buffered with an implementation buffer size no smaller than 8 MB

65: Which of the following methods is abstract in HttpServlet?

A.   service

B.   doPost

C.   doGet

D.   It has no abstract methods

66: Which of the following is not a valid implicit object in a JSP?

A.   out

B.   context

C.   response

D.   request

67: Once the JSP has been requested and the page is loaded and initialized, the JSP engine calls the:

A.   _jspService() method

B.   jspDestroy() method

C.   jspInit() method

D.   jspPreRender() method

68: Objects which when compiled down are contained in (a) ____ file.

A.   JSP

B.   jar

C.   kornshell

D.   txt

69: Which types can be used in conjunction with HttpServletResponse methods to stream output data?

A.   java.io.FileOutputStream

B.   java.io.PrintStream

C.   java.io.OutputStream

D.   java.io.PrintWriter

70: Which of the following is not a 'page' directive attribute?

A.   extends

B.   language

C.   implements

D.   import

71: Which of the following classes has an implementation of getSession method that returns an HttpSession object?

A.   SessionContext

B.   SessionConfig

C.   HttpServletRequest

D.   HttpServletResponse

72: Which of the following is not a valid standard method called as part of the JSP life cycle by the container?

A.   jspService

B.   _jspService

C.   jspDestroy

D.   jspInit

73: Which of the following methods can be used to get initialization and startup parameters of a servlet?

A.   getServletInfo

B.   init

C.   service

D.   getServletConfig

74: Which one is not a part of the JSTL library group?

A.   Core

B.   XML

C.   HTML

D.   SQL

75: Which of the following is an INVALID implicit EL objects?

A.   applicationScope

B.   session

C.   requestScope

D.   pageScope

76: Which methods are needed to get the complete and exact URL that causes the call to arrive at the servlet? 1. getRequestURL() 2. getQueryString() 3. Both of them 4. None of them

A.   3

B.   4

C.   2

D.   1

A.   b and d

B.   b and c

C.   a and b

D.   a only

78: By default, the servlet API uses a ______ to store a session ID.

A.   request

B.   response

C.   session

D.   sessionfactory

E.   cookie

79: Considering the following Servlet code, choose the statement which is true: package com.nullhaus; import javax.servlet.annotation.*; import javax.servlet.http.*; @WebServlet("nullHausServlet") public class NullServlet extends HttpServlet { }

A.   This is valid usage of @WebServlet annotations which creates a Servlet with “nullHausServlet” url-pattern value

B.   This is an invalid usage of @WebServlet annotations because of the wrongly formed url-pattern value

C.   This code doesn’t compile, because NullHausServlet need to implement one of doGet(-), doPost(-), etc. methods

D.   This is valid usage of @WebServlet annotations which creates a Servlet with “nullHausServlet” name

80: Which of the following HTTP method sends the same response as request?

A.   HEAD

B.   TRACE

C.   OPTIONS

D.   DEBUG

81: JSP implicit object 'out' is an object of which of the following classes?

A.   DataOutputStream

B.   OutputStream

C.   PrintStream

D.   JspWriter

82: What results from a call to the getInitParameterNames() method on ServletContext when there are no context parameters set up in the deployment descriptor?

A.   null is returned

B.   A NoParametersExistException is thrown

C.   An empty Enumeration object is returned

D.   An ArrayList object of size zero is returned

83: Given: 1. <%@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core” %> 2. <%@ taglib prefix = “tables” uri= http://www.javaranch.com/tables %> 3. <%@ taglib prefix = “jsp” tagdir=”/WEB_INF/tags” %> 4. <%@ taglib uri=”UtilityFunctions” prefix=”util” %> What about the above taglib directives would cause the JSP to not function?

A.   Line 4 is wrong because the prefix attribute must come before the uri attribute

B.   Line 4 is wrong because the uri value must begin with http://

C.   Line 3 is wrong because the prefix jsp is reserved for standard actions

D.   Line 3 is wrong because there is no uri attribute

84: Which of the following method is called before the page service any requests?

A.   jspService()

B.   _jspInit()

C.   init()

D.   jspInit()

85: JSP implicit object 'exception' is an object of which of the following classes?

A.   java.lang.Throwable

B.   java.lang.Exception

C.   java.lang.JSPException

D.   java.lang.RunTimeException

86: Given a servlet containing the following code, what is the outcome of attempting to compile and run the servlet? ServletContext context = getServletContext(); String s = context.getAttribute(

A.   String s has a valid directory as its value

B.   String s has a null value

C.   The servlet won’t compile

D.   The servlet won’t run

87: The http GET, PUT, and DELETE verbs are idempotent. What does the term "idempotent" stand for?

A.   None of the above

B.   The same operation applied multiple times yields the same result

C.   This term has nothing to do with http verbs

D.   The same operation applied multiple times yields different results

88: JSP implicit object 'application' is an object of which of the following class?

A.   javax.servlet.ServletContext

B.   javax.servlet.ServletConfig

C.   javax.servlet.Application

D.   javax.servlet.Context

89: A programmer needs to update a live, running servlet’s initialization parameters so that the web application will begin to use the new parameters immediately. In order to accomplish this, which must be true (although not necessarily sufficient)? 1. The container must destroy and then reinitialize the servlet 2. The servlet’s constructor must retrieve the updated DD parameter from the servlet’s ServletConfig object 3. For each parameter, the DD must have a separate <init-param> tag 4. For each parameter, you must modify a DD tag that specifies the name of the servlet, the name of the parameters, and the new value of the parameter.

A.   2 and 4

B.   1 and 3

C.   2 and 3

D.   1 only

90: Which of the following is valid about 'page' JSP directive?

A.   Includes the contents of a file into the JSP at translation time

B.   Controls properties of the entire application

C.   Makes a custom tag library available within the including page

D.   Controls properties of the JSP

91: Why do you get this error? NoClassDefFoundError: org.apache.commons.lang.StringUtils. 1. The given class could not be found. 2. The given class could be found, but something went wrong when initializing it 3. None of them

A.   1

B.   2

C.   3

92: Which of the following is a non-idempotent HTTP method

A.   HEAD

B.   PUT

C.   GET

D.   DELETE

E.   POST

93: Which of the following is not a valid java bean (POJO) scope in JSP?

A.   response

B.   session

C.   request

D.   application

94: Which of the following object scope is defined as follows : Objects with this scope are accessible from pages processing requests in the same application as the one in which they were created?

A.   session

B.   page

C.   application

D.   request

95: Where in JSP page source can EL functions be used?

A.   In the body of a tag where body-content is set to tagdependent

B.   Within a JSP expression

C.   Within a JSP scriptlet

D.   In the body of a tag where body-content is set to JSP

96: What is the limit of data to be passed from HTML when doGet() method is used?

A.   8k

B.   2k

C.   4k

D.   1k

97: Which of the following is an INVALID attribute for tag directive?

A.   dynamic-attributes

B.   display-name

C.   body-content

D.   tag-attributes

98: javax.servlet.jsp.JspPage extends which of the following interfaces?

A.   GenericServlet

B.   HttpServlet

C.   Servlet

D.   HttpJspPage

99: How can you make the JSP class implement the SingleThreadModel interface?

A.   <%@ page isThreadSafe=

B.   <%@ page ThreadSafe=

C.   <%@ page ThreadSafe=

D.   <%@ page isThreadSafe=

100:

A.   Include static page into a JSP page

B.   throw an exception

C.   Invoke a Servlet from a JSP page.

D.   forward to another servlet

E.   redirect to another resource