Copyright © 3S Technologies

Email: info@3s-technologies.com

Best viewed in Internet Explorer ver. 9.0 - 1024 X 768 resolution.

Java Objectives - Part 6

The reason for documenting this topic is to provide important technical questions and answers on Java language.  These questions can appear in an aptitude test, organized by different recruiting IT organizations.

 

Some questions and answers on Applets:

1) 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.

 

2) 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.

 

3) 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.

 

4) 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);

 

5) 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.

 

6) 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)

 

7) 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 ();

 

8) 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.

 

9) It is essential to have both the .java file and the .html file of an applet in the same directory.

a) True.

b) False.

Ans: b

 

10) The <PARAM> tag contains two attributes namely _________ and _______.

Ans: Name , value.

 

11) Passing values to parameters is done in the _________ file of an applet.

Ans: .html.

 

12) What tags are mandatory when creating HTML to display an applet

a)name, height, width

b) code, name

c) codebase, height, width

d) code, height, width

Ans: d

 

13) Applet’s getParameter( ) method can be used to get parameter values.

a) True

b) False

Ans: a

 

14) What are the Applet’s Life Cycle methods? Explain them?

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

stop( ) method - Can be called when the browser moves off the applet’s page.

destroy( ) method - Can be called when the browser is finished with the applet.

 

15) What are the Applet’s information methods?

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

 

16) All Applets are subclasses of Applet.

a) True.

b) False.

Ans: a

 

17) All Applets must import java.applet and java.awt.

a) True.

b) False.

Ans: a

 

18) What are the steps involved in Applet development?

Ans: a) Edit a Java source file,

b) Compile your program and

c) Execute the appletviewer, specifying the name of your applet’s source file.

 

19) Applets are executed by the console based Java run-time interpreter.

a) True.

b) False.

Ans: b.

 

20) Which classes and interfaces does Applet class consist?

Ans: Applet class consists of a single class, the Applet class and three interfaces: AppletContext,

AppletStub and AudioClip.

 

21) What is the sequence for calling the methods by AWT for applets?

Ans: 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 take place :

stop( )

destroy( )

 

22) Which method is used to output a string to an applet?

Ans: drawString ( ) method.

 

23) Every color is created from an RGB value.

a) True.

b) False

Ans: a.

 

Some question and answers on AWT : WINDOWS, GRAPHICS AND FONTS:

 

1) How would you set the color of a graphics context called g to cyan?

a) g.setColor(Color.cyan);

b) g.setCurrentColor(cyan);

c) g.setColor("Color.cyan");

d) g.setColor("cyan’);

e) g.setColor(new Color(cyan));

Ans: a

 

2) The code below draws a line. What color is the line?

g.setColor(Color.red.green.yellow.red.cyan);

g.drawLine(0, 0, 100,100);

 

a)Red

b) Green

c) Yellow

d) Cyan

e) Black

Ans: d

 

3) What does the following code draw?

g.setColor(Color.black);

g.drawLine(10, 10, 10, 50);

g.setColor(Color.RED);

g.drawRect(100, 100, 150, 150);

 

a) A red vertical line that is 40 pixels long and a red square with sides of 150 pixels

b) A black vertical line that is 40 pixels long and a red square with sides of 150 pixels

c) A black vertical line that is 50 pixels long and a red square with sides of 150 pixels

d) A red vertical line that is 50 pixels long and a red square with sides of 150 pixels

e) A black vertical line that is 40 pixels long and a red square with sides of 100 pixel

Ans: b

 

4) Which of the statements below are true?

a) A polyline is always filled.

b) A polyline can not be filled.

c) A polygon is always filled.

d) A polygon is always closed

e) A polygon may be filled or not filled

Ans: b, d and e

 

5) What code would you use to construct a 24-point bold serif font?

a) new Font(Font.SERIF, 24,Font.BOLD);

b) new Font("SERIF", 24, BOLD");

c) new Font("BOLD ", 24,Font.SERIF);

d) new Font("SERIF", Font.BOLD,24);

e) new Font(Font.SERIF, "BOLD", 24);

Ans: d

 

6) What does the following paint( ) method draw?

Public void paint(Graphics g) {

g.drawString("question #6",10,0);

}

 

a) The string "question #6", with its top-left corner at 10,0

b) A little squiggle coming down from the top of the component, a little way in from the left edge

Ans: b

 

 

7) What does the following paint( ) method draw?

Public void paint(Graphics g) {

g.drawString("question #6",10,0);

}

 

a) A circle at (100, 100) with radius of 44

b) A circle at (100, 44) with radius of 100

c) A circle at (100, 44) with radius of 44

d) The code does not compile

Ans: d

 

8) What is relationship between the Canvas class and the Graphics class?

Ans: A Canvas object provides access to a Graphics object via its paint( ) method.

 

9) What are the Component subclasses that support painting.

Ans: The Canvas, Frame, Panel and Applet classes support painting.

 

10) What is the difference between the paint( ) and repaint( ) method?

Ans: The paint( ) method supports painting via a Graphics object. The repaint( ) method is used to cause paint( ) to be invoked by the AWT painting method.

 

11) What is the difference between the Font and FontMetrics classes?

Ans: The FontMetrics class is used to define implementation-specific properties, such as ascent and descent, of a Font object.

 

12) Which of the following are passed as an argument to the paint( ) method?

a) A Canvas object

b) A Graphics object

c) An Image object

d) A paint object

Ans: b

 

13) Which of the following methods are invoked by the AWT to support paint and repaint operations?

a) paint( )

b) repaint( )

c) draw( )

d) redraw( )

Ans: a

 

14) Which of the following classes have a paint( ) method?

a) Canvas

b) Image

c) Frame

d) Graphics

Ans: a and c

 

15) Which of the following are methods of the Graphics class?

a) drawRect( )

b) drawImage( )

c) drawPoint( )

d) drawString( )

Ans: a, b and d

 

16) Which Font attributes are available through the FontMetrics class?

a) ascent

b) leading

c) case

d) height

Ans: a, b and d

 

17) Which of the following are true?

a) The AWT automatically causes a window to be repainted when a portion of a window has been minimized and then maximized.

b) The AWT automatically causes a window to be repainted when a portion of a window has been covered and then uncovered.

c) The AWT automatically causes a window to be repainted when application data is changed.

d) The AWT does not support repainting operations.

Ans: a and b

 

18) Which method is used to size a graphics object to fit the current size of the window?

Ans: getSize( ) method.

 

19) What are the methods to be used to set foreground and background colors?

Ans: setForeground( ) and setBackground( ) methods.

 

20) You have created a simple Frame and overridden the paint method as follows

public void paint(Graphics g){

 g.drawString("Dolly",50,10);

}

What will be the result when you attempt to compile and run the program?

 

a) The string "Dolly" will be displayed at the centre of the frame

b) An error at compilation complaining at the signature of the paint method

c) The lower part of the word Dolly will be seen at the top of the form, with the top hidden.

d) The string "Dolly" will be shown at the bottom of the form

Ans: c

 

20) Where g is a graphics instance what will the following code draw on the screen.

g.fillArc(45,90,50,50,90,180);

a) An arc bounded by a box of height 45, width 90 with a centre point of 50,50, starting at an angle of 90 degrees traversing through 180 degrees counter clockwise.

b) An arc bounded by a box of height 50, width 50, with a centre point of 45,90 starting at an angle of 90 degrees traversing through 180 degrees clockwise.

c) An arc bounded by a box of height 50, width 50, with a top left at coordinates of 45, 90, starting at 90 degrees and traversing through 180 degrees counter clockwise.

d) An arc starting at 45 degrees, traversing through 90 degrees clockwise bounded by a box of height 50, width 50 with a centre point of 90, 180.

Ans: c

 

21) Given the following code

import java.awt.*;

public class SetF extends Frame{

public static void main(String argv[]){

SetF s = new SetF();

s.setSize(300,200);

s.setVisible(true);

}

}

How could you set the frame surface color to pink ?

a)s.setBackground(Color.pink);

b)s.setColor(PINK);

c)s.Background(pink);

d)s.color=Color.pink

Ans: a

3S Technologies

Online Education for all