Java

40 MCQs on Introduction to JSP

In this blog, you will find 40 MCQs on Introduction to JSP.

Explore the fundamentals of JavaServer Pages (JSP) with this comprehensive set of 40 multiple-choice questions. Test your knowledge on the essential concepts, features, and usage of JSP, an integral technology for building dynamic web applications in Java. Whether you’re a beginner or looking to brush up on your JSP skills, these MCQs provide a valuable resource for learning and self-assessment.

1. What does JSP stand for?

a) Java Server Page

b) JavaScript Page

c) Java Scripted Page

d) Java Standard Page

2. Which file extension is used for JSP files?

a) .java

b) .jspf

c) .jsp

d) .jsf

3. Which tag is used to define a JSP declaration?

a) <%@ %>

b) <%! %>

c) <%= %>

d) <%# %>

4. In JSP, what is the purpose of the <%– … –%> tag?

a) To define a comment

b) To include external JavaScript

c) To declare variables

d) To print output

5. Which JSP directive is used to include content from another resource at translation time?

a) <%@ include %>

b) <%! include %>

c) <%# include %>

d) <%& include %>

6. In JSP, how do you import a Java class for use in a JSP page?

a) <import>

b) <jsp:import>

c) <%@ page import>

d) <jsp:useBean>

7. Which JSP element is used to display the result of Java expression evaluation in the output?

a) <%! %>

b) <%= %>

c) <%# %>

d) <%? %>

8. How do you include another JSP page within the current JSP page?

a) <jsp:include>

b) <jsp:page>

c) <jsp:import>

d) <jsp:component>

9. Which method is called when a JSP page is first requested by a client?

a) init()

b) service()

c) doGet()

d) jspInit()

10. What is the default scope of a JSP page’s attribute if no scope is specified explicitly?

a) Page

b) Request

c) Session

d) Application

11. Which JSP tag is used to create reusable content that can be included in other JSP pages?

a) <jsp:include>

b) <jsp:directive.include>

c) <jsp:taglib>

d) <jsp:useBean>

12. In JSP, how do you define a session attribute named “username” with the value “John”?

a) <session:username value=”John”/>

b) <session:setAttribute name=”username” value=”John”/>

c) <session:setProperty name=”username” value=”John”/>

d) <session:set name=”username” value=”John”/>

13. What is the correct syntax for including a JSP file named “header.jsp” in another JSP file?

a) <jsp:include file=”header.jsp”/>

b) <jsp:include page=”header.jsp”/>

c) <jsp:include src=”header.jsp”/>

d) <jsp:include source=”header.jsp”/>

14. Which JSP directive is used to specify the character encoding for a JSP page?

a) <%@ page contentType=”text/html; charset=UTF-8″ %>

b) <%@ charset=”UTF-8″ %>

c) <%@ encoding=”UTF-8″ %>

d) <%@ charset=UTF-8 %>

15. In JSP, how can you retrieve a parameter from the request object?

a) request.getParameter()

b) request.getParameters()

c) request.getParameterValues()

d) request.getParameterList()

16. Which JSP tag is used to iterate over a collection in JSP?

a) <c:for>

b) <c:loop>

c) <c:forEach>

d) <c:iterate>

17. In JSP, how can you include a JavaBean object in the page scope?

a) <jsp:useBean>

b) <jsp:setBean>

c) <jsp:includeBean>

d) <jsp:object>

18. Which JSP tag is used for conditional processing in JSP pages?

a) <c:choose>

b) <c:if>

c) <c:when>

d) <c:case>

19. How do you access a session attribute named “user” in JSP?

a) <%= session.getAttribute(“user”) %>

b) <%= request.getSessionAttribute(“user”) %>

c) <%= page.getAttribute(“user”) %>

d) <%= application.getAttribute(“user”) %>

20. What is the correct syntax for defining a custom JSP tag in a JSP file?

a) <@tag:mytag>

b) <jsp:custom-tag>

c) <custom:mytag>

d) <%@taglib uri=”…” prefix=”custom” %>

21. Which JSP tag is used for including external resources like CSS or JavaScript files?

a) <jsp:resource>

b) <jsp:import>

c) <link>

d) <jsp:external-resource>

22. How can you set the content type of a JSP page to JSON?

a) <%@ page contentType=”application/json” %>

b) <%@ page type=”application/json” %>

c) <%@ contentType=”application/json” %>

d) <%@ type=”application/json” %>

23. Which JSP tag is used to handle exceptions in JSP pages?

a) <%@ page errorPage=”errorPage.jsp” %>

b) <c:try>

c) <c:exception>

d) <c:catch-exception>

24. In JSP, what is the purpose of the <%@ page errorPage=”error.jsp” %> directive?

a) To specify a custom error page

b) To define an error message

c) To include an error message

d) To handle exceptions

25. How do you include the contents of an external file in a JSP page without using a JSP tag?

a) <jsp:include file=”external.jsp”/>

b) <%@ include file=”external.jsp” %>

c) <jsp:import file=”external.jsp”/>

d) <jsp:content file=”external.jsp”/>

26. In JSP, which object is used to store information about a user’s session?

a) request

b) session

c) pageContext

d) application

27. What is the purpose of the out object in JSP?

a) To handle exceptions

b) To set session attributes

c) To write output to the response

d) To manage database connections

28. How do you include a JSP file based on a condition using the <c:choose> tag?

a) <c:choose><c:when test=”${condition}”><jsp:include page=”file.jsp”/></c:when></c:choose>

b) <c:if test=”${condition}”><jsp:include page=”file.jsp”/></c:if>

c) <c:when test=”${condition}”><jsp:include page=”file.jsp”/></c:when>

d) <c:include test=”${condition}”><jsp:include page=”file.jsp”/></c:include>

29. In JSP, how do you redirect the user to another page or URL?

a) response.redirect(“newPage.jsp”)

b) request.forward(“newPage.jsp”)

c) response.sendRedirect(“newPage.jsp”)

d) request.send(“newPage.jsp”)

30. Which JSTL tag is used to set a variable in JSP?

a) <c:set>

b) <c:var>

c) <c:variable>

d) <c:define>

31. In JSP, what is the purpose of the <%@ page import=”java.util.*” %> directive?

a) To import all classes from the java.util package

b) To import a specific class from the java.util package

c) To declare a custom Java class

d) To declare a custom JSP tag

32. What is the purpose of the JSP Expression Language (EL)?

a) To define custom JSP tags

b) To write Java code in JSP pages

c) To evaluate and display Java expressions in JSP

d) To handle exceptions in JSP

33. How do you iterate over the elements of a collection using JSTL?

a) <c:forEach>

b) <c:for>

c) <c:repeat>

d) <c:loop>

34. Which JSP tag is used to define a function in JSTL?

a) <c:function>

b) <c:defineFunction>

c) <c:fn>

d) <c:method>

35. In JSP, what is the purpose of the application scope?

a) To store data that is specific to a single user’s session

b) To store data that is shared among all users of the web application

c) To store data that is specific to a single JSP page

d) To store data that is specific to a single HTTP request

36. How can you access the value of a session attribute named “user” using JSTL?

a) ${session.user}

b) ${request.user}

c) ${pageContext.user}

d) ${application.user}

37. What is the purpose of the JSTL <c:if> tag?

a) To define custom JSP tags

b) To conditionally display content in a JSP page

c) To create a loop in a JSP page

d) To include external resources in a JSP page

38. Which JSTL tag is used to format numbers and dates?

a) <c:format>

b) <c:convert>

c) <c:parse>

d) <c:output>

39. How do you include the contents of a JSP fragment into another JSP page using JSTL?

a) <c:fragment>

b) <c:import>

c) <c:include>

d) <c:insert>

40. What is the primary purpose of the <jsp:forward> tag in JSP?

a) To redirect the user to another page or URL

b) To include the contents of another JSP page

c) To handle exceptions in JSP

d) To send an HTTP request to another servlet

Answers

1. What does JSP stand for?

Answer: a) Java Server Page

2. Which file extension is used for JSP files?

Answer: c) .jsp

3. Which tag is used to define a JSP declaration?

Answer: b) <%! %>

4. In JSP, what is the purpose of the <%– … –%> tag?

Answer: a) To define a comment

5. Which JSP directive is used to include content from another resource at translation time?

Answer: a) <%@ include %>

6. In JSP, how do you import a Java class for use in a JSP page?

Answer: c) <%@ page import>

7. Which JSP element is used to display the result of Java expression evaluation in the output?

Answer: b) <%= %>

8. How do you include another JSP page within the current JSP page?

Answer: a) <jsp:include>

9. Which method is called when a JSP page is first requested by a client?

Answer: a) init()

10. What is the default scope of a JSP page’s attribute if no scope is specified explicitly?

Answer: a) Page

11. Which JSP tag is used to create reusable content that can be included in other JSP pages?

Answer: a) <jsp:include>

12. In JSP, how do you define a session attribute named “username” with the value “John”?

Answer: b) <session:setAttribute name=”username” value=”John”/>

13. What is the correct syntax for including a JSP file named “header.jsp” in another JSP file?

Answer: b) <jsp:include page=”header.jsp”/>

14. Which JSP directive is used to specify the character encoding for a JSP page?

Answer: a) <%@ page contentType=”text/html; charset=UTF-8″ %>

15. In JSP, how can you retrieve a parameter from the request object?

Answer: a) request.getParameter()

16. Which JSP tag is used to iterate over a collection in JSP?

Answer: c) <c:forEach>

17. In JSP, how can you include a JavaBean object in the page scope?

Answer: a) <jsp:useBean>

18. Which JSP tag is used for conditional processing in JSP pages?

Answer: a) <c:choose>

19. How do you access a session attribute named “user” in JSP?

Answer: a) <%= session.getAttribute(“user”) %>

20. What is the correct syntax for defining a custom JSP tag in a JSP file?

Answer: d) <%@taglib uri=”…” prefix=”custom” %>

21. Which JSP tag is used for including external resources like CSS or JavaScript files?

Answer: c) <link>

22. How can you set the content type of a JSP page to JSON?

Answer: a) <%@ page contentType=”application/json” %>

23. Which JSP tag is used to handle exceptions in JSP pages?

Answer: a) <%@ page errorPage=”errorPage.jsp” %>

24. In JSP, what is the purpose of the <%@ page errorPage=”error.jsp” %> directive?

Answer: a) To specify a custom error page

25. How do you include the contents of an external file in a JSP page without using a JSP tag?

Answer: b) <%@ include file=”external.jsp” %>

26. In JSP, which object is used to store information about a user’s session?

Answer: b) session

27. What is the purpose of the out object in JSP?

Answer: c) To write output to the response

28. How do you include a JSP file based on a condition using the <c:choose> tag?

Answer: a) <c:choose><c:when test=”${condition}”><jsp:include page=”file.jsp”/></c:when></c:choose>

29. In JSP, how do you redirect the user to another page or URL?

Answer: c) response.sendRedirect(“newPage.jsp”)

30. Which JSTL tag is used to set a variable in JSP?

Answer: a) <c:set>

31. In JSP, what is the purpose of the <%@ page import=”java.util.*” %> directive?

Answer: a) To import all classes from the java.util package

32. What is the purpose of the JSP Expression Language (EL)?

Answer: c) To evaluate and display Java expressions in JSP

33. How do you iterate over the elements of a collection using JSTL?

Answer: a) <c:forEach>

34. Which JSTL tag is used to define a function in JSTL?

Answer: a) <c:function>

35. In JSP, what is the purpose of the application scope?

Answer: b) To store data that is shared among all users of the web application

36. How can you access the value of a session attribute named “user” using JSTL?

Answer: a) ${session.user}

37. What is the purpose of the JSTL <c:if> tag?

Answer: b) To conditionally display content in a JSP page

38. Which JSP tag is used to format numbers and dates?

Answer: a) <c:format>

39. How do you include the contents of a JSP fragment into another JSP page using JSTL?

Answer: b) <c:import>

40. What is the primary purpose of the <jsp:forward> tag in JSP?

Answer: a) To redirect the user to another page or URL


Further Reading

30 MCQs on Servlet Config and Servlet Context

JUnit Tutorial

30 MCQs on Assert Method and Annotations in Java

Spring Framework Practice Problems and Their Solutions

From Google to the World: The Story of Go Programming Language

Why Go? Understanding the Advantages of this Emerging Language

Creating and Executing Simple Programs in Go

40 MCQs on JavaBeans in JSP

20+ Interview Questions on Go Programming Language

100+ MCQs On Java Architecture

Java Practice Exercise

30 MCQs on Servlets – Get and Post Requests

40 MCQs on Cookies and Session Management with Servlets

programmingempire

Princites

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *