Tuesday, January 17, 2012

Java Applet interview questions

1. What is an Applet? Should Applets have constructors?
Applet is a program that can be downloaded by a browser and run. Applet is embedded in a web page and runs when browsers open that webpage.

Applets are small programs transferred through Internet, automatically installed and run as part of web-browser. Applets implements functionality of a client. Applet is a dynamic and interactive program that runs inside a Web page displayed by a Java-capable browser. We don’t have the concept of Constructors in Applets. Applets can be invoked either through browser or through Appletviewer utility provided by JDK.

2.How to embed an Applet into a webpage?
To embed an applet in your webpage you need to use tag in the webpage or program.
Eg:

3.Tell about Applet life cycle?
When applet viewer encounters this , then life cycle starts..
init()method is called when Applet is first loaded, this method is called once in whole Applet life cycle.
start() – this method is called every time an Applet is started.
paint() – this method is used for drawing, so whenever an Applet is refreshed or called initially this method is called. This method is not mandatory it is used based on users requirement.
stop() – It is called when users moves to nest page.
destroy() – when browser is finished with Applet it is destroyed.

4.What is an applet viewer ?
Applet viewer is a command line program to run Java applets. It is included in the SDK. It helps you to test an applet before you run it in a browser.

5.Can we develop standalone applet?
Yes, you can develop standalone Applet, by embed the code in tag.

6.Can I use instead of ?
Yes! But Sun recommends using .

7.Why to use Applet, what type of security it is providing?
When Applet arrives on client’s machine, it has limited access to resources, this helps to run Applet successfully without disturbing client`s machine disk.

8.How to determine width and height of my Applet?
Applet tag contains attributes width and height, with this we can determine applet size.

9.How to set background color for an Applet?





10. What is difference between Applet and Application in Java?
Applet is a small application that are accessed over internet server and automatically installed over client`s machine. But Applet has limited access to network systems and local systems.
A Java application is a standalone program which can access all network systems. An applet can be embedded in java application.

11. How to execute Applet on our system i.e standalone applet?
After writing a java program embed the tag between /*……..*/ then compile then program. Since there is no main() in Applet it can`t be run with the traditional command.

12. How will you initialize an applet?
Write my initialization code in the applets init method or applet constructor.

13.How to insert your applets into FrontPage?

1. Place the .class file in the directory containing the HTML document into which you want to insert the applet.
2. Copy the ... tag from your applet implementation or examples to the clipboard.
3. In FrontPage select the "HTML" tab from the lower left hand corner.
4. Paste the ... tag in an appropriate place between the and tags. You'll find a gray box with the aqua letter "J" in the "Normal" view indicating the the applet tag has been inserted.
5. To see the applet appearance select the "Preview" tab.


14.What is the sequence for calling the methods by AWT for applets?

When an applet begins, the AWT calls the following methods, in this sequence:

► init()
► start()
► paint()

When an applet is terminated, the following sequence of method calls takes place :

► stop()
► destroy()


15.Can we pass parameters to an applet from HTML page to an applet? How?

We can pass parameters to an applet using tag in the following way:




Access those parameters inside the applet is done by calling getParameter() method inside the applet. Note that getParameter() method returns String value corresponding to the parameter name.

16.How can I arrange for different applets on a web page to communicate with each other?

Name your applets inside the Applet tag and invoke AppletContext’s getApplet() method in your applet code to obtain references to the other applets on the page.

17.Which classes and interfaces does Applet class consist?

Applet class consists of a single class, the Applet class and three interfaces: AppletContext, AppletStub, and AudioClip.

18. What tags are mandatory when creating HTML to display an applet?

code, height, width

19. What are the Applets information methods?

The following are the Applet’s information methods: getAppletInfo() method: Returns a string describing the applet, its author, copyright information, etc. getParameterInfo( ) method: Returns an array of string describing the applet’s parameters.

20. What are the steps involved in Applet development?

Following are the steps involved in Applet development:

► Create/Edit a Java source file. This file must contain a class which extends Applet class.
► Compile your program using javac
► Execute the appletviewer, specifying the name of your applet’s source file or html file. In case the applet information is stored in html file then Applet can be invoked using java enabled web browser.

21. Which method is used to output a string to an applet? Which function is this method included in?

drawString( ) method is used to output a string to an applet. This method is included in the paint method of the Applet.

22. When is update method called?

Whenever a screen needs redrawing (e.g., upon creation, resizing, validating) the update method is called. By default, the update method clears the screen and then calls the paint method, which normally contains all the drawing code.

23.How will you communicate between two Applets?

The simplest method is to use the static variables of a shared class since there's only one instance of the class and hence only one copy of its static variables. A slightly more reliable method relies on the fact that all the applets on a given page share the same AppletContext. We obtain this applet context as follows:

AppletContext ac = getAppletContext();

AppletContext provides applets with methods such as getApplet(name), getApplets(),getAudioClip, getImage, showDocument and showStatus().

24. How do you communicate in between Applets and Servlets?

We can use the java.net.URLConnection and java.net.URL classes to open a standard HTTP connection and "tunnel" to the web server. The server then passes this information to the servlet in the normal way. Basically, the applet pretends to be a web browser, and the servlet doesn't know the difference. As far as the servlet is concerned, the applet is just another HTTP client.

25. What is the base class for all swing components?

JComponent (except top-level containers)

26. What is Difference between AWT and Swing?

Swing provides a richer set of components than AWT. They are 100% Java-based.

AWT on the other hand was developed with the mind set that if a component or capability of a component werent available on one platform, it wouldnt be available on any platform. Due to the peer-based nature of AWT, what might work on one implementation might not work on another, as the peer-integration might not be as robust. There are a few other advantages to Swing over AWT:

► Swing provides both additional components and added functionality to AWT-replacement components
► Swing components can change their appearance based on the current "look and feel" library that's being used.
► Swing components follow the Model-View-Controller (MVC) paradigm, and thus can provide a much more flexible UI.
► Swing provides "extras" for components, such as:
► Icons on many components
► Decorative borders for components
► Tool tips for components
► Swing components are lightweight (less resource intensive than AWT)
► Swing provides built-in double buffering
► Swing provides paint debugging support for when you build your own components

Swing also has a few disadvantages:
► It requires Java 2 or a separate JAR file
► If you're not very careful when programming, it can be slower than AWT (all components are drawn)
► Swing components that look like native components might not act exactly like native components

27.Why do you Canvas?

The Canvas class of java.awt is used to provide custom drawing and event handling. It provides a general GUI component for drawing images and text on the screen. It does not support any drawing methods of its own, but provides access to a Graphics object through its paint() method. The paint() method is invoked upon the creation and update of a canvas so that the Graphics object associated with a Canvas object can be updated.

28. What type of sound file formats can I use for the applets?

Java v1.02 only supports the "voice format" of the .au sound files. This is also know as "ยต-law, 8/16-bit, mono, 8000hz sample rate"

No comments: