This section contains carefully selected MCQs and Previous Year Questions with explanations to help students understand concepts and prepare effectively for examinations, interviews, and competitive tests.
Q: 1Which servlet lifecycle method is called by the servlet container to initialize a servlet?
Option B
A servlet is a Java class that runs inside a servlet container such as Apache Tomcat. The servlet container manages the servlet's lifecycle, including creating the servlet, initializing it, handling requests, and eventually destroying it.
The init() method is called by the servlet container once when the servlet is initialized. It is mainly used to perform initialization tasks, such as reading configuration information or preparing resources required by the servlet.

| Method | When It Is Called | Description |
|---|---|---|
| init() | When the servlet is initialized. | It is called by the servlet container to perform initialization tasks before the servlet starts handling requests. |
| service() | When a client request is received. | It is called by the servlet container to process each request. In HttpServlet, it dispatches the request to methods such as doGet() or doPost(). |
| destroy() | When the servlet is being removed. | It is called by the servlet container before the servlet is taken out of service, allowing the servlet to release resources. |
Q: 2Which method is responsible for handling an HTTP GET request in HttpServlet?
Option A
In a Java web application using HttpServlet, an HTTP GET request is generally handled by the doGet() method. The servlet container receives the clients GET request and invokes the appropriate servlet method to process it.
HTTP Methods in HttpServlet:
HTTP methods define what type of operation a client wants to perform on a web resource. When a browser or another client sends an HTTP request to a Java servlet, the servlet container identifies the HTTP method and, for an HttpServlet, dispatches the request to the corresponding method such as doGet() or doPost().
HttpServlet belongs to the jakarta.servlet.http package in modern Jakarta Servlet applications. In older Java EE applications, the corresponding package is javax.servlet.http.
| HTTP Method | HttpServlet Method | Description |
|---|---|---|
| GET | doGet() | It handles HTTP GET requests, which are generally used to retrieve or display information from the server. The request may contain parameters in the URL or query string, and the method normally produces a response containing the requested resource or information. |
| POST | doPost() | It handles HTTP POST requests, which are generally used to send data to the server. It is commonly used when submitting HTML forms, creating resources, or performing operations where the request data is sent in the request body. |
| PUT | doPut() | It handles HTTP PUT requests, which are generally used to create or replace/update a resource at a specified location. The request usually contains the representation of the resource that should be stored by the server. |
| DELETE | doDelete() | It handles HTTP DELETE requests, which are generally used to request the removal of a specified resource from the server. |
| HEAD | doHead() | It handles HTTP HEAD requests, which are similar to GET requests but are used to obtain response headers without normally returning the response body. It can be useful for checking information about a resource without downloading its content. |
| OPTIONS | doOptions() | It handles HTTP OPTIONS requests, which are used to determine the communication options or HTTP methods supported by a resource or server. |
| TRACE | doTrace() | It handles HTTP TRACE requests, which are primarily intended for diagnostic purposes by allowing the client to determine how a request is being received or processed along the HTTP path. |

Q: 3Which statement about JSP is correct?
Option A
JSP (Java Server Pages) is a server-side web technology used to create dynamic web pages.
A JSP page does not normally execute directly in the users browser. Instead, the web/servlet container processes the JSP page and translates it into a Java servlet. The servlet is then compiled and executed on the server to generate the response.

Note:
JSP and Servlet are closely related. A Servlet is primarily Java code that handles requests and responses, while JSP provides a page-oriented way to generate dynamic web content. Internally, the container uses a servlet representation to execute a JSP.
Q: 4Which of the following is true about JSP?
Option B
In JSP (Java Server Pages) technology, JSP pages are first translated into Java servlets. These servlets are then compiled and executed by the web server. This allows JSP to create dynamic and interactive web pages.
<%@ page language="java" %>
<html>
<body>
<h1>Hello, JSP Example</h1>
<%
String name = "Suresh";
out.println("Welcome, " + name);
%>
</body>
</html>
When you save this file as myfile.jsp and run it on a server like Tomcat, the server automatically converts it into a servlet like this:
public final class myfile_jsp extends HttpJspBase {
public void _jspService(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("text/html");
JspWriter out = response.getWriter();
out.write("<html><body>");
out.write("<h1>Hello, JSP!</h1>");
String name = "Suresh";
out.write("Welcome, " + name);
out.write("</body></html>");
}
}
Q: 5What will be the value of ‘val’ displayed when JSP page is accessed for the third time since server startup?
<%! int val = 0; %>
<% val++; %>
<html><body><p>val : <% = val %></p></body></html>
Option C
In JSP (Java Server Pages), understanding how variables behave across multiple requests is essential. JSP provides different scripting elements such as declarations <%! %>, scriptlets <% %>, and expressions <%= %>, each with a specific role and scope.
Declarations <%! %>
Scriptlets <% %>
| Request Number | Initial val | Operation (val++) | Value Displayed |
|---|---|---|---|
| 1st Request | 0 | 0 to 1 | 1 |
| 2nd Request | 1 | 1 to 2 | 2 |
| 3rd Request | 2 | 2 to 3 | 3 |
Q: 6Which JSP mechanism is used to include another resource at request time?
Option D
JSP provides different mechanisms for including or transferring control to another resource. The <jsp:include> action is used to include the output of another resource at request time. The included resource may be another JSP page, servlet, or other resource that can process the request.
E.g.:
<jsp:include page="header.jsp" />
When this JSP page is requested, the container processes header.jsp and includes its output in the current response.
The <jsp:include> vs Include Directive:
| Mechanism | Syntax | When Inclusion Occurs | Description |
|---|---|---|---|
| Include directive | <%@ include file="header.jsp" %> | Translation Time | Content is included when the JSP is translated into a servlet. |
| <jsp:include> | <jsp:include page="header.jsp" /> | Request Time | The included resource is processed when the request is being handled. |
Q: 7Which among the following describes an Applet?
Option B
A Java Applet is a small Java program designed to be embedded in another application or environment, historically most commonly a web page. Unlike a normal Java application, an applet does not normally contain a main() method as its entry point. Its execution is controlled by the applet environment.
E.g.:
import java.applet.Applet;
import java.awt.Graphics;
public class Example extends Applet
{
public void paint(Graphics g)
{
g.drawString("Suraku Academy", 75, 75);
}
}
Java applets are now a legacy technology and are no longer supported by modern web browsers.
Q: 8Package of drawString() method is?
Option D
In Java, the drawString() method is used to display a string of text at a specified position when working with graphics. It is defined in the Graphics class, which belongs to the java.awt package.
E.g.:
import java.awt.Graphics;
class Example
{
public void paint(Graphics g)
{
g.drawString("Banjara Nirmohi", 50, 50);
}
}
Here, g is an object of the Graphics class, and drawString() displays " Banjara Nirmohi" at the specified (x, y) position.
| Package | Description | Important Classes |
|---|---|---|
| java.applet | Provides classes related to creating and managing Java applets. | Applet |
| java.io | Provides classes for input and output, including file and stream operations. | File, FileReader, FileWriter, InputStream, OutputStream |
| javax.swing | Provides classes for building GUI applications and graphical user interfaces. | JFrame, JButton, JLabel, JTextField, JPanel |
| java.awt | Provides classes for GUI components, graphics, fonts, colors, and drawing operations. | Graphics, Color, Font, Frame, Button |
Q: 9In Java if you wish to change an applet’s background color dynamically, what method must be called to make this change visible?
Option A
In a Java applet, setBackground() changes the background color, but the visual change may not appear immediately on the screen. To request the applet to be redrawn, the repaint() method is called.
E.g.:
import java.applet.Applet;
import java.awt.Color;
public class Example extends Applet
{
public void changeColor()
{
setBackground(Color.GREEN);
repaint();
}
}
Q: 10A java program that is embedded in HTML document and runs in the context of java capable browser is known as _________.
Option A
A Java applet is a small Java program designed to be embedded in an HTML document and executed within a Java-enabled web browser or applet environment. Applets were mainly used to provide interactive features on web pages.
For example, an applet could be used to display a simple animation, graphical object, or interactive component inside a web page.
A traditional Java applet is a small Java program that extends the Applet class and uses lifecycle methods such as init(), start(), stop(), and destroy().

E.g.:
import java.applet.Applet;
import java.awt.Graphics;
public class MyApplet extends Applet
{
public void paint(Graphics g)
{
g.drawString("Suraku Academy", 50, 50);
}
}
HTML Code:
<html>
<body>
<applet code="MyApplet.class" width="300" height="200">
</applet>
</body>
</html>
Thank you so much for taking the time to read my Computer Science MCQs section carefully. Your support and interest mean a lot, and I truly appreciate you being part of this journey. Stay connected for more insights and updates! If you'd like to explore more tutorials and insights, check out my YouTube channel.
Don’t forget to subscribe and stay connected for future updates.