Java Applet Interview Questions in Java

List of topic-wise frequently asked java interview questions with the best possible answers for job interviews.

Java Applet Interview Questions in Java

Question 1.
What is the difference between applications and applets?
Answer:

a) Application must be run on a local machine whereas the applet needs no explicit installation on the local machine.
b) Application must be run explicitly within a Java-compatible virtual machine whereas applet loads and runs itself automatically in a java-enabled browser.
d) Application starts execution with its main method whereas applet starts execution with its init method.
e) Application can run with or without the graphical user interface whereas applet must run within a graphical user interface.

Question 2.
How does the applet recognize the height and width?
Answer:
Using getParameters( ) method.

Question 3.
When do you use codebase in the applet?
Answer:
When the applet class file is not in the same directory, the codebase is used.

Question 4.
What is the lifecycle of an applet?
Answer:
init( ) method – Can be called when an applet is first loaded
start( ) method – Can be called each time an applet is started
paint( ) method – Can be called when the applet is minimized or maximized
stop( ) method – Can be used when the browser moves off the applet’s page
destroy( ) method – Can be called when the browser is finished with the applet

Question 5.
How do you set security in applets?
Answer:
using setSecurityManager( ) method

Question 6.
How will you initialize an Applet?
Answer:
By including the initialization code in the init( ) method.

Question 7.
What is the order of method invocation in an Applet?
Answer:
init( ), start( ), paint( ), stop( ), destroy( ),

Question 8.
When is the update method called?
Answer:
Whenever we minimize, maximize, restart the applet, and explicitly calling the repaint( ) method in the code. Repaint( ) method will implicitly call the update!) method.

Question 9.
How will you communicate between two Applets?
Answer:
By creating URL object, URLConnection object and getting InputStream , OutputStream Using getlnputStream( ), getOutputStream( )

Question 10.
What is an applet?
Answer: Applet is a dynamic and interactive program that runs inside a web page displayed by a java capable browser.

Question 11.
How does the applet recognize the height and width?
Answer.
’Using getParameters( ) method.

Question 12.
When do you use codebase in the applet?
Answer:
When the applet class file is not in the same directory, the codebase is used.

Question 13.
How do you set security in applets?
Answer:
using setSecurityManager( ) method

Question 14.
The definition of an applet named MyApplet would begin with the line

class MyApplet extends Applet {

What is going on here? What is “Applet”? What is the word “extends” doing there?
Answer:
The applet is the name of a class that is a standard part of Java. Here, MyApplet is a new class that is being defined as a subclass of Applet. The word “extends” is used to create subclasses in this way. The class MyApplet inherits everything that is in the class Applet. 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.)

Question 15.
An applet can define a method
public boolean keyDown(Event evt, int key)
What is the purpose of such a method? What is the key parameter for? Why is this method “public”?
Answer:
This keyDown method will be called by the “system” when the user presses a key on the keyboard (assuming that the applet has the input focus at that time). You would write such a method to react to the user’s keystrokes. The key parameter tells which key the user pressed. This method has to be public since it is called by the system, from outside the class that contains the method.

Question 16.
Is it possible to access the network resources through an Applet on the browser?
Answer:
There is no way that an applet can access network resources. You have to implement a Java Query Server that will be running on the same machine from where you are receiving your applet (that is Internet Server). Your applet will communicate with that server which will fulfill all the requirements of the applet. You can communicate between applet and Java query server using sockets RMI (preferred, as this will return the whole object).

Question 17.
Java applets can be downloaded from anywhere and run on a client’s system. What are the restrictions imposed on Java applets that prevent them from causing system damage or security breaches?
Answer:
The restrictions that are there for Java applets are: –

  • Applets cannot read or write to the local file system where it is being executed.
  • Applets can only communicate back to the server from where it was downloaded by the browser.
  • Applets cannot execute any local program on the client’s system.

Question 18.
What is the sequence of interpretation, compilation of a java applet?
Answer:
A java program is first compiled to bytecode which is stored in a ‘.class file’. This file is downloaded as an applet to a browser which then interprets it by converting it into machine code appropriate to the hardware platform on which it is running. •

Question 19.
Is it possible to access the network resources through an Applet on the browser?
Answer:
There is no way that an applet can access network resources. You have to implement a Java Query Server that will be running on the same machine from where you are receiving your applet (that is Internet Server). Your applet will communicate with that server which will fulfill all the requirements of the applet. You can communicate between applet and Java query server using sockets RMI (preferred, as this will return the whole object).

Question 20.
When we give the resize function within an applet then it does not reflect the changes when we see the applet in the IE. Why does this happen?
Answer:
Applets are resized only in applet viewer, not in browsers. Change the applet height/width attributes in the HTML file and refresh IE.

Question 21.
What is the relationship between clipping and repainting?
Answer:
When a window is repainted by the AWT painting thread, it sets the clipping regions to the area of the window that requires repainting.

Question 22.
How do I load a serialized applet?
Answer:
Instead of specifying the CODE attribute of a <APPLET> tag, you use the OBJECT attribute, as in:

<APPLET 
OBJ ECT=TheApp1et.ser
WIDTH=300 
HEIGHT=300 
>
</APPLET>

This allows you to pre-initialize your applet to some saved state, instead of having to reinitialize your applet each time it is loaded.

Question 23.
“Why does it take so much time to access an Applet having Swing Components the first time?
Answer:
Because behind every swing component are many Java objects and resources. This takes time to create them in memory. JDK 1.3 from Sun has some improvements which may lead to faster execution of Swing applications.

Question 24.
Which method is called by the Applet class to load an image?
Answer:
getImage(URL object, filename) is used for this purpose.