Subscribe Us

Core Java - Interview Questions and Answers

381. Suppose that you want to have an object eh handle the TextEvent of a TextArea object t. How should you add eh as the event handler for t?
a) t.addTextListener(eh);
b) eh.addTextListener(t);
c) addTextListener(eh.t);
d) addTextListener(t,eh);

Ans : a.

382. What is the preferred way to handle an object’s events in Java 2?
a) Override the object’s handleEvent( ) method.
b) Add one or more event listeners to handle the events.
c) Have the object override its processEvent( ) methods.
d) Have the object override its dispatchEvent( ) methods.

Ans : b.

383. Which of the following are true?
a) A component may handle its own events by adding itself as an event listener.
b) A component may handle its own events by overriding its event-dispatching method.
c) A component may not handle oits own events.
d) A component may handle its own events only if it implements the handleEvent( ) method.

Ans : a and b.


APPLETS

384. What is an Applet? Should applets have constructors?

Ans : 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.

385. How do we read number information from my applet’s parameters, given that Applet’s getParameter() method returns a string?

Ans : Use the parseInt() method in the Integer Class, the Float(String) constructor in the
Class Float, or the Double(String) constructor in the class Double.

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

Ans : 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.

387. How do I select a URL from my Applet and send the browser to that page?
Ans : Ask the applet for its applet context and invoke showDocument() on that context object.
Eg. URL targetURL;

String URLString
AppletContext context = getAppletContext();
try{
targetUR L = new URL(URLString);
} catch (Malformed URLException e){
// Code for recover from the exception 
}
context. showDocument (targetURL);
Can applets on different pages communicate with each other?

Ans : No. Not Directly. The applets will exchange the information at one meeting place
either on the local file system or at remote system.

388. How do Applets differ from Applications?

Ans :
Appln: Stand Alone
Applet: Needs no explicit installation on local m/c.
Appln: Execution starts with main() method.
Applet: Execution starts with init() method.
Appln: May or may not be a GUI
Applet: Must run within a GUI (Using AWT)

389. How do I determine the width and height of my application?

Ans : Use the getSize() method, which the Applet class inherits from the Component
class in the Java.awt package. The getSize() method returns the size of the applet as
a Dimension object, from which you extract separate width, height fields.

Eg. Dimension dim = getSize ();
int appletwidth = dim.width ();

390. What is AppletStub Interface?
Ans : The applet stub interface provides the means by which an applet and the browser communicate. Your code will not typically implement this interface.
It is essential to have both the .java file and the .html file of an applet in the same
directory.
True. / False.

Ans : False.


Post a Comment

0 Comments