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 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: 2What 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: 3Which of the following is true about Java applets?
Option D
In Java, an applet is a small program designed to be embedded in a web page. The core idea of applets is that they run inside a web browser on the client’s machine, not on the server. This allows interactive features, animations, or graphics to be displayed on the user’s browser.
import java.applet.Applet;
import java.awt.Graphics;
public class MyApplet extends Applet {
public void paint(Graphics g) {
g.drawString("Hello, Suraku", 60, 60);
}
}
HTML FILE TO RUN THE APPLET:
<html>
<body>
<applet code="MyApplet.class" width="250" height="100"></applet>
</body>
</html>
Q: 4Which of the following methods can be executed more than once on the same applet object by the applet context?
Option B
In Java Applets, different lifecycle methods are called at different stages of execution. Among these methods, some are called only once per applet object, while others can be called multiple times as the user moves between pages or reloads the same page.
The start() method is invoked by the applet context every time the applet becomes visible or active again, for example when the user:
So start() is the method that can be executed more than once on the same applet object.
| Applet Method | Description |
|---|---|
| init() | Called once when the applet is loaded and used for initialization such as setting variables and creating objects. |
| start() | Called after init and each time the applet becomes visible or active again so it can resume animations or background tasks. |
| paint() | Called whenever the applet needs to redraw its display for example when the window is resized or uncovered so it can draw graphics and text. |
| stop() | Called when the applet is stopped or hidden such as when the user switches to another page so it can pause animations or background threads. |
| destroy() | Called once when the applet is about to be removed from memory so it can release system resources such as threads files or network connections. |
Q: 5Which of the following methods is executed to bring an applet to ‘Running’ state?
Option A
In Java applets, the lifecycle consists of several methods such as init(), start(), stop(), and destroy().
The start() method is responsible for bringing the applet into the running state. It is called after init() completes and whenever the applet becomes active again, such as when the user revisits the page.
This method is used to start or resume execution of the applet.
| Method | Used For |
|---|---|
| init() | Initializes the applet. It is called once. |
| start() | Starts or resumes execution. |
| stop() | Pauses execution. |
| destroy() | Terminates the applet. |
Q: 6Which of the following Java technology is used for server-side programming in Internet applications?
Option A
In Java, Java Servlets are used for server-side programming. Servlets run on a web server or application server and generate responses, usually in the form of HTML, to be sent to web browsers.
Java Swing and JavaFX are used for creating desktop GUI applications, and Java Applets are designed for client-side execution within web browsers.
Q: 7What is the correct sequence of steps for handling a client in a Java servlet lifecycle of a servlet?
Option D
The lifecycle of a Java servlet involves these key steps in sequence:
You have reached the end of this topic. Continue learning with the next topic below.
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.