Important Web Design and Development Short Questions
Chapter: 20
Question 1:
How do you create an applet in Java?
Answer:
To create an applet, extend the Applet or JApplet class and override the paint method to draw or display content. No main method is required because the browser or applet viewer handles the applet's execution. You also need to include HTML code to embed the applet in a web page.
Chapter: 20
Question 2:
What does the init() method do in an applet's lifecycle?
Answer:
The init() method is called only once when the applet is loaded. It is used to initialize the applet, similar to a constructor in other Java classes. This method sets up the applet's initial state before it becomes visible.
Chapter: 20
Question 3:
Explain the purpose of the start() method in an applet.
Answer:
The start() method is called when the applet becomes visible or when the user revisits a page containing the applet. It starts the applet’s execution and is also invoked when the browser is maximized. This method is used to resume or initiate actions when the applet is shown.
Chapter: 20
Question 4:
What happens if you do not call super.paintComponent(g) in the paint method of a Swing applet?
Answer:
If super.paintComponent(g) is not called, previous drawings will not be cleared before new ones are painted. This can lead to artifacts or overlapping drawings, as the previous content remains visible, potentially resulting in an unintended visual effect.
Chapter: 20
Question 5:
Describe the paint() method in the context of applets.
Answer:
The paint() method is responsible for drawing or updating the display of the applet. It is first called when the applet becomes visible and then repeatedly called whenever the applet needs to be repainted. This method should contain all the drawing logic for the applet’s visual representation.
Chapter: 20
Question 6:
What is the role of the stop() method in an applet's lifecycle?
Answer:
The stop() method is called to halt the applet's execution when the user leaves the applet’s page or minimizes the browser. It is used to stop ongoing activities or threads in the applet, ensuring that resources are freed when the applet is not in use.
Chapter: 20
Question 7:
When is the destroy() method called in an applet?
Answer:
The destroy() method is called once when the applet is unloaded from memory. It is used for final cleanup tasks, such as releasing resources or performing any last-minute operations before the applet is completely removed.
Chapter: 20
Question 8:
How does the AppletDemo class demonstrate the applet life cycle methods?
Answer:
The AppletDemo class overrides all key life cycle methods (init(), start(), paint(), stop(), destroy()) and prints a message to the console each time a method is called. This helps in understanding the sequence of method calls and applet behavior during different stages of its lifecycle.
Chapter: 20
Question 9:
How can you view an applet in a web browser?
Answer:
To view an applet in a web browser, you need to create an HTML file with an <applet> tag that specifies the applet's class file, width, and height. Double-clicking the HTML file or accessing it via a browser will render the applet. You can also use the appletviewer tool to test applets without a browser.
Chapter: 21
Question 10:
What is a socket in network communication?
Answer:
A socket is one endpoint of a two-way communication link between two programs running on a network. It acts as a bi-directional communication channel between hosts.