# Grapecity Java Questions: AWT Windows Graphics and Fonts

Examrace Placement Series prepares you for the toughest placement exams to top companies.

1. 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); Which is correct? Red Green Yellow Cyan Black Ans: d

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

3. Explain the code? A red vertical line that is 40 pixels long and a red square with sides of 150 pixels A black vertical line that is 40 pixels long and a red square with sides of 150 pixels A black vertical line that is 50 pixels long and a red square with sides of 150 pixels A red vertical line that is 50 pixels long and a red square with sides of 150 pixels 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?

1. A polyline is always filled.

2. A polyline can not be filled.

3. A polygon is always filled.

4. A polygon is always closed

5. 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? new Font (Font. SERIF, 24, Font. BOLD); new Font ( “SERIF” 24, “BOLD” ); new Font ( “BOLD” 24, Font. SERIF); new Font ( “SERIF” Font. BOLD, 24); 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); } 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: b

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: 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. What are the Component subclasses that support painting. Ans: The Canvas, Frame, Panel and Applet classes support painting.

9. 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.

10. 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.

11. Which of the following are passed as an argument to the paint () method? A Canvas object A Graphics object An Image object A paint object Ans: b

12. Which of the following methods are invoked by the AWT to support paint and repaint operations? paint () repaint () draw () redraw () Ans: a

13. Which of the following classes have a paint () method? Canvas Image Frame Graphics Ans: a and c

14. Which of the following are methods of the Graphics class? drawRect () drawImage () drawPoint () drawString () Ans: a, b and d

15. Which Font attributes are available through the FontMetrics class? ascent leading case height Ans: a, b and d

16. Which of the following are true? The AWT automatically causes a window to be repainted when a portion of a window has been minimized and then maximized. The AWT automatically causes a window to be repainted when a portion of a window has been covered and then uncovered. The AWT automatically causes a window to be repainted when application data is changed. The AWT does not support repainting operations. Ans: a and b

17. Which method is used to size a graphics object to fit the current size of the window? Ans: GetSize () method.

18. What are the methods to be used to set foreground and background colors? Ans: SetForeground () and setBackground () methods.

19. 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?

1. The string “Dolly” will be displayed at the centre of the frame

2. An error at compilation complaining at the signature of the paint method

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

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

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

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

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

4. 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

1. s setBackground (Color. Pink)

2. s setColor (PINK)

3. s Background (pink)

4. s color = Color. Pink

Ans: a

22. 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