All Exams Test series for 1 year @ ₹349 only
Question

When an applet begins, which of the following sequence methods is CORRECT?

1) start()

2) paint()

3) init()

The correct answer is

3, 1, 2

Understanding Java Applet Lifecycle Methods

Java applets were small applications embedded in web pages. They had a specific lifecycle managed by the browser or applet viewer. Understanding the sequence of method calls during this lifecycle is crucial for developing applets.

When an applet starts its execution, several methods are invoked in a particular order. These methods handle the initialization, starting, running, stopping, and destruction phases of the applet.

The key methods involved in the initial startup sequence are:

  • init(): This method is called only once when the applet is first loaded into the browser or applet viewer. It's used for one-time initialization tasks, such as setting up the user interface or loading resources.
  • start(): This method is called after init() is complete. It's also called whenever the user navigates back to the page containing the applet after leaving it. This method is where the applet's main execution logic typically begins, such as starting animations or threads.
  • paint(Graphics g): This method is called whenever the applet needs to redraw its content on the screen. This happens after start() is called, when the applet is initially displayed, and also when the applet's area needs to be repainted (e.g., after being covered and then uncovered).

Therefore, when an applet begins execution for the first time, the sequence of calls involving these three methods is:

  1. init()
  2. start()
  3. paint()

Let's map this sequence to the options provided in the question, which uses the numbers 1 for start(), 2 for paint(), and 3 for init().

  • Method 1: start()
  • Method 2: paint()
  • Method 3: init()

The correct sequence is init(), then start(), then paint(). Using the numbers assigned in the options, this sequence is 3, 1, 2.

Now let's examine the given options based on this understanding:

  • Option 1: 2, 3, 1 (paint(), init(), start()) - Incorrect sequence. init() is called before paint() and start().
  • Option 2: 3, 1, 2 (init(), start(), paint()) - Correct sequence. This matches the standard applet startup lifecycle.
  • Option 3: 1, 3, 2 (start(), init(), paint()) - Incorrect sequence. init() is called before start().
  • Option 4: 3, 2, 1 (init(), paint(), start()) - Incorrect sequence. start() is typically called before the initial paint() after initialization.

Detailed Analysis of Applet Startup Sequence

The Java Applet API defines a clear order for these methods to ensure resources are initialized before the applet starts its main logic, and drawing happens when necessary.

Applet Startup Method Sequence
Step Method Purpose
1 init() Initialization tasks, setting up UI, loading resources. Called only once.
2 start() Starting main execution, threads, animations. Called after init() and on page revisits.
3 paint() Rendering the applet's output on the screen. Called after start() and whenever a repaint is needed.

This sequence ensures that the applet is properly set up (init), begins its active operations (start), and then displays its initial state or output (paint).

Conclusion on Correct Applet Method Order

Based on the standard Java Applet lifecycle, the correct sequence of methods called when an applet begins for the first time is init(), followed by start(), and then paint(). This corresponds to the sequence 3, 1, 2 using the numbering provided in the options.

Revision Table: Java Applet Lifecycle

Java Applet Method Lifecycle Summary
Method When Called Key Role
init() Once, when loaded. Setup and Initialization.
start() After init() and on page entry. Begin execution/activity.
paint(Graphics g) After start() and when repaint needed. Draw content.
stop() On page exit or browser minimize. Pause execution/activity.
destroy() When browser/applet viewer quits. Cleanup resources.

Additional Information: Extending Java Applet Understanding

While the question focuses on the startup, it's helpful to know the full lifecycle. The stop() method is called when the user leaves the web page or minimizes the browser. It's used to pause threads or activities that shouldn't run when the applet is not visible or active. The destroy() method is called just before the applet is terminated (e.g., when the browser is closed). It's used to release any resources the applet might be holding, such as file handles or network connections.

Understanding the entire lifecycle – init(), start(), stop(), destroy(), and paint() – is fundamental to writing correct and efficient Java applets.

Was this answer helpful?

Important Questions from Java

  1. What will be the output of the following Java program?

    class test

    {public static void main (String args [ ])

    {

    System.out.println(20 + 30 + "Java");

    System.out.println("Java" + 20 + 30);

    }

    }

  2. In Java, the 'PreparedStatement' interface is a sub interface which is used to execute __________ query.

  3. Which of the following is NOT a java primitive type?

  4. Which of the following is/are the rules to declare variables in Java?

  5. What is the use of 'javac' command?

Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App