For individuals aiming to excel in the field of software development, understanding Core Java is fundamental. Whether you’re getting ready for an interview or just want to get better at programming in Java, a structured Java course can help you a lot.
Java is the most widely used programming language in the current IT industry. A big reason why so many people are interested in and already working in programming is that knowing Java can help them get jobs. This article is dedicated to the same purpose. There is a full guide here to help you answer the most common Core Java interview questions.
Java Applets have been a crucial part of Java programming for creating dynamic and interactive web content. Although modern web technologies have reduced their usage, applets are still relevant for legacy systems.
So as a Java developer you can expect to encounter applet-related questions during a technical interview. This article will explore some frequently asked Java applet interview questions and provide sample answers to help you prepare.
What is a Java Applet?
A Java Applet is a mini-application written in Java that can run inside a web browser. Applets are embedded in HTML pages and get executed by the browser’s Java Virtual Machine. They enhance user experience by enabling dynamic content and interactivity.
Compared to standalone Java applications, applets have some key differences:
-
They run within a browser under security restrictions enforced by the sandbox model.
-
Applets don’t have a main() method They rely on lifecycle methods like init() and start() for execution
-
They can leverage the browser for resources like obtaining images or connecting to a server.
-
They are constrained in terms of what they can access on the client’s system due to security considerations.
Explain the Lifecycle of a Java Applet
A Java applet goes through the following key phases during its lifecycle:
Initialization: The init() method is called by the browser to initialize the applet. Resources like images are loaded here.
Starting: The start() method is invoked after init() to start any threads and perform tasks that will run continuously.
Stopping: This stops applet execution when the user navigates away from the applet’s page. The stop() method is called.
Destroying: The destroy() method performs cleanup before the applet is terminated when the browser shuts down.
Painting: The paint() method renders the applet’s output to the screen whenever required.
What Happens When an Applet is Loaded?
When an applet is loaded by the browser, the following key steps occur:
-
The browser first contacts the web server and downloads the bytecode (.class) file for the applet.
-
The applet’s bytecode is verified by the bytecode verifier for security.
-
The JVM creates an instance of the applet class and calls init() to initialize it. Resources are allocated here.
-
The start() method is invoked to start the applet. This kickstarts any threads or repetitive tasks.
-
Further methods like paint(), stop() etc. are called automatically during the applet’s lifetime based on actions like window resizing or the user leaving the page.
What is the Difference Between an Applet and a Java Application?
The main differences between an applet and a standalone Java application are:
-
Applets run inside a browser while applications run independently on the client machine.
-
Applets are constrained by the sandbox security model while applications can freely access system resources.
-
Applets don’t have a main() method to start execution. Applications require it.
-
Applets rely on lifecycle methods like init() and start() for execution. Applications don’t have these.
-
Applets can leverage the browser for resources. Applications have to manage resources on their own.
-
Applets are embedded in webpages using the APPLET tag. Applications are compiled into executable files.
What are the Restrictions Imposed on Java Applets?
Java applets are constrained by the sandbox security model. Some key restrictions are:
-
They cannot access client resources like the file system, system clipboard etc.
-
They can only connect to the host from which they are served, not arbitrary sites.
-
They cannot load libraries or define native methods dynamically.
-
Their network access is limited to only the host they originate from.
-
They cannot read certain system properties.
-
By default, they run in a restricted execution environment and cannot run commands on the client system.
These restrictions are relaxed if the applet is signed or trusted by the user. But in general, the sandbox aims to prevent untrusted code from accessing sensitive resources.
What are Untrusted Applets?
Untrusted applets refer to Java applets that run in the default restricted environment without any special permissions. They are constrained by the sandbox model. The applet classloader treats these applets as coming from an untrusted source.
By default, all unsigned applets downloaded over the internet are considered untrusted. The user can manually trust an untrusted applet to give it more access permissions. Once trusted, it can perform operations like file I/O, loading libraries etc.
However, explicitly trusting an internet-sourced applet is risky. The sandbox provides safety by limiting what untrusted code can do on the client machine. So most unsigned applets on the web run with untrusted status in the browser.
How do You Create a Java Applet? Walk Through the Steps.
Here are the typical steps to create a simple Java Applet:
-
Create a new class that extends Applet.
-
Override the necessary lifecycle methods like init(), start(), stop() etc. based on your applet’s needs.
-
Add the applet’s GUI components and declare the instance variables. Perform other initialization in init().
-
Implement the programming logic and event handling code.
-
Compile the Java file to get the .class file for the applet.
-
Create an HTML file that uses APPLET tag to embed the applet. Set width, height etc.
-
Host the HTML file and .class file on a web server.
-
Access the HTML file through a web browser to run the applet.
This allows the applet to be loaded and executed by the browser’s JVM, providing interactive functionality to the webpage.
How Can You Dynamically Resize an Applet?
To dynamically resize an applet, the applet needs to override the resize() method from the Applet class. For example:
import java.applet.Applet;import java.awt.Dimension;public class MyApplet extends Applet { public void resize(Dimension d) { // Resize applet components }}
When the browser resizes the applet, the resize() method will automatically be called. The Dimension parameter contains the new width and height. The applet can then re-layout its components accordingly.
Additionally, re-implementing the paint() method allows redrawing the applet area. The repaint() method can be called to refresh the display.
Explain the Applet Classloader and Security Mechanism
The applet classloader is responsible for loading classes into the JVM when an applet executes. It ensures applet classes are kept isolated from others.
When an applet class is loaded, the classloader checks if the class is trusted before defining it. This verification ensures untrusted code is not malicious.
The applet security mechanism relies on the sandbox model. Applets are confined to a limited execution environment with restricted permissions.
Some key security techniques used:
-
Bytecode verification checks for illegal code.
-
Classloaders isolate untrusted classes.
-
The security manager controls access to system resources.
-
The sandbox restricts file, network, and system access.
Together, these mechanisms aim to prevent untrusted applets from compromising client systems. Permissions can be increased for trusted applets but most run in restricted mode.
What is the Purpose of the paint() Method When Creating an Applet?
The paint() method in an applet renders the output within the applet window. It is responsible for:
-
Drawing the initial view of the applet UI.
-
Redrawing the applet when the window is uncovered or resized.
-
Refreshing the applet’s graphics periodically for animations.
The paint() method receives a Graphics object which provides drawing methods to render text, shapes, images etc. After initialization in init(), paint() is called automatically whenever the applet needs to be redrawn.
Applets override paint() and implement drawing logic within it. For animations, double buffering can be used by drawing to an off-screen image first and transferring it to the on-screen Graphics in paint().
What is the Difference Between AppletContext and AppletStub?
AppletContext and AppletStub are interfaces that applets can use to interact with their environment:
AppletContext provides methods that allow an applet to communicate with the browser or applet viewer running it. Using the AppletContext, applets can:
- Stream audio/video
- Get applet parameters
- Show status messages
- Print to the console
AppletStub provides the basic callbacks that the applet needs to operate such as starting, stopping and destroying itself. AppletStub also enables applets to:
- Request GUIDANCE codebase and documentbase
- Check if they are active
- Get the parameters passed to them
How Can You Implement Multithreading in a Java Applet?
16 Can you have virtual functions in Java?
In Java, all non-static methods are virtual functions by default.
10 What are the observer and observable classes?
Objects that inherit the “Observable class” take care of a list of “observers.”Â
When an Observable object gets upgraded, it calls the update() method of each of its observers.Â
After that, it notifies all the observers that there is a change of state.Â
The Observer interface gets implemented by objects that observe Observable objects.
What is an Applet | JAVA INTERVIEW QUESTIONS AND ANSWERS
FAQ
What are the 4 methods of applet class?
What are the 4 attributes of an applet?
What is Java applet used for?
What is the difference between an applet and a Java application?
Applets are executed within a java enabled browser, but a Java application is a standalone Java program that can be executed outside of a browser. However, they both require the existence of a Java Virtual Machine (JVM). Furthermore, a Java application requires a main method with a specific signature, in order to start its execution.
What is myapplet in Java?
The rest of the definition of MyApplet will consist of additions and modifications to the stuff that is inherited from Applet. (Note: The Applet class is actually defined in the package java. applet, so the above line has to be preceded by “import java.applet.*” or “import java.applet.Applet;” to make the applet class available.)
How do applets interact with JavaScript?
Java Applets can interact with JavaScript through the JSObject class, which provides a bridge between Java and JavaScript. The key methods include: 1. “getWindow (Applet applet)”: This method returns a JSObject corresponding to the browser’s window object. 2. “eval (String s)”: It evaluates the JavaScript expression.
How can I run a Java applet?
You can run a Java applet in two standard ways: Executing the applet within a Java-compatible web browser. Using an applet viewer, such as the standard tool, applet-viewer. An applet viewer executes your applet in a window, making it the fastest and easiest way to test your applet.