Java Interview Questions With Answers
AWT : windows, graphics and fonts
Q: 1) How would you set the color of a graphics context called g to cyan?
g.setColor(Color.cyan);
g.setCurrentColor(cyan);
g.setColor("Color.cyan");
g.setColor("cyan’);
g.setColor(new Color(cyan));
Ans : a.
Q: 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.
Q: 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.
Q: 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.
Q: 5) What code would you use to construct a 24-point bold serif font?
1.
new Font(Font.SERIF, 24,Font.BOLD);
2.
new Font("SERIF", 24, BOLD");
3.
new Font("BOLD ", 24,Font.SERIF);
4.
new Font("SERIF", Font.BOLD,24);
5.
new Font(Font.SERIF, "BOLD", 24);
Ans : 4.
Q: 6) What does the following paint( ) method draw?
Public void paint(Graphics g) {
g.drawString("question #6",10,0);
}
The string "question #6", with its top-left corner at 10,0
A little squiggle coming down from the top of the component, a little way
in from the left edge
Ans : 2.
Q: 7) What does the following paint( ) method draw?
Public void paint(Graphics g) {
g.drawString("question #6",10,0);
}
A circle at (100, 100) with radius of 44
A circle at (100, 44) with radius of 100
A circle at (100, 44) with radius of 44
The code does not compile
Ans : 4.
Q: 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.
Q: 9) What are the Component subclasses that support painting.
Ans : The Canvas, Frame, Panel and Applet classes support painting.
Q: 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.
Q: 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.
Q: 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.
Q: 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.
Q: 14) Which of the following classes have a paint( ) method?
a.
Canvas
b
Image
c.
Frame
d
Graphics
Ans : a and c.
Q: 15) Which of the following are methods of the Graphics class?
a.
drawRect( )
b.
drawImage( )
c
drawPoint( )
d
drawString( )
Ans : a, b and d.
Q: 16) Which Font attributes are available through the FontMetrics class?
a.
ascent
b
leading
c.case
d
height
Ans : a, b and d.
Q: 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.
Q: 18) Which method is used to size a graphics object to fit the current size
of the window?
Ans : getSize( ) method.
Q: 19) What are the methods to be used to set foreground and background
colors?
Ans : setForeground( ) and setBackground( ) methods.
Q: 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.
Q: 21) 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.
Q: 22) 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.
<<<Previous Next>>>
500 Java Objective Questions and Answer