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?



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


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?



C.   GET


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


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) { }