Developed by Y. Daniel Liang using Java Servlets (Current time is Sat Nov 21 19:36:01 EST 2009)

The questions are to accompany Introduction to Java Programming, 4E by Y. Daniel Liang. Please report errors to y.daniel.liang@gmail.com.

Introduction to Java Programming, 4E has been superseded by Introduction to Java Programming, 5E.

Chapter 10: Getting Started with GUI Programming

Section 10.2 The Java GUI API
1   Which of the following classes is a heavy weight component?

A. JButton
B. JTextField
C. JPanel
D. JFrame

2   Which component cannot be added to a container?

A. JPanel
B. JButton
C. JFrame
D. None of the above

Section 10.3 Frames
3   Analyze the following code.

import java.awt.*;
import javax.swing.*;

public class Test {
   public static void main(String[] args) {
     Component c = new JButton("OK");
     JFrame frame = new JFrame("My Frame");
     frame.add(c);
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setVisible(true);
   }
}


A. You cannot assign a JButton to a variable of java.awt.Component.
B. You can only add c to a container because c's type is Component.
C. You cannot add a Swing component directly to a JFrame. Instead, you have to add it to a JFrame's contentPane using frame.getContentPane().add(c).
D. None of the above.

4   Analyze the following code.

import java.awt.*;
import javax.swing.*;

public class Test {
   public static void main(String[] args) {
     JFrame frame = new JFrame("My Frame");
     frame.getContentPane().add(new JButton("OK"));
     frame.getContentPane().add(new JButton("Cancel"));
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setSize(200, 200);
     frame.setVisible(true);
   }
}


A. Only button OK is displayed.
B. Only button Cancel is displayed.
C. Both button OK and button Cancel are displayed and button OK is displayed on the left side of button OK.
D. Both button OK and button Cancel are displayed and button OK is displayed on the right side of button OK.

Section 10.4 Layout Managers
5   What layout manager should you use so that every component occupies the same size in the container?


A. a FlowLayout
B. a GridLayout
C. a BorderLayout
D. None of the above

6   What should you use to position a Button within an application Frame so that the size of the Button is NOT affected by the Frame size?

A. a FlowLayout
B. a GridLayout
C. the center area of a BorderLayout
D. the East or West area of a BorderLayout
E. the North or South area of a BorderLayout

7   To set a FlowLayout in panel jp, you can use the method __________.

A. jp.setLayout(new FlowLayout());
B. jp.setLayout(new FlowLayout(FlowLayout.CENTER));
C. jp.setLayout(new FlowLayout(FlowLayout.center));
D. jp.setLayout(FlowLayout());
E. a or b

8   The default layout out of a contentPane in a JFrame is __________.

A. FlowLayout
B. GridLayout
C. BorderLayout
D. None of the above.

9   The default layout out of a JPanel is __________.

A. FlowLayout
B. GridLayout
C. BorderLayout
D. None of the above.

Section 10.6 Drawing Graphics in Panels
10   The method __________ can be used to get the width of the component.

A. getDimension()
B. getWidth()
C. getHeight()
D. getSize()

11   You should override the __________ method to draw things.

A. repaint()
B. update()
C. paintComponent()
D. init()

12   Analyze the following code.

import java.awt.*;
import javax.swing.*;

public class Test {
   public static void main(String[] args) {
     JFrame frame = new JFrame("My Frame");
     frame.getContentPane().add(new MyDrawing("Welcome to Java!"));
     frame.setSize(300, 300);
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setVisible(true);
     frame.setVisible(true);
   }
}

class MyDrawing extends JPanel {
   String message;

   public MyDrawing(String message) {
     this.message = message;
   }

   public void paintcomponent(Graphics g) {
     super.paintComponent(g);

     g.drawString(message, 20 ,20);
   }
}


A. The program runs fine and displays Welcome to Java!
B. The program has a syntax error because the paintcomponent should be spelled as paintComponent.
C. The program has a runtime error because the paintcomponent should be spelled as paintComponent.
D. The program runs, but it does not display the message.
E. It is a runtime to invoke the setVisible(true) twice.

13   The coordinate of the upper-left corner of the frame is __________.

A. (0, 0)
B. (25, 25)
C. (100, 100)
D. None of the above.

14   Analyze the following code.

import java.awt.*;
import javax.swing.*;

public class Test extends JFrame {
   public Test() {
     getContentPane().add(new MyDrawing("Welcome to Java!"));
   }

   public static void main(String[] args) {
     JFrame frame = new JFrame();
     frame.setSize(300, 300);
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setVisible(true);
   }
}

class MyDrawing extends JPanel {
   String message;

   public MyDrawing(String message) {
     this.message = message;
   }

   public void paintComponent(Graphics g) {
     super.paintComponent(g);

     g.drawString(message, 20 ,20);
   }
}


A. The program runs fine and displays Welcome to Java!
B. The program would display Welcome to Java! if new JFrame() is replaced by Test().
C. The program would display Welcome to Java! if new JFrame() is replaced by new Test().
D. The program would display Welcome to Java! if new JFrame() is replaced by new Test("My Frame").
E. None of the above.

15   Analyze the following code.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Test1 extends JFrame {
   public Test1() {
     getContentPane().add(new MyCanvas());
   }

   public static void main(String[] args) {
     JFrame frame = new Test1();
     frame.setSize(300, 300);
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setVisible(true);
   }
}

class MyCanvas extends JPanel {
   private String message;

   public void setMessage(String message) {
     this.message = message;
   }

   public void paintComponent(Graphics g) {
     super.paintComponent(g);

     g.drawString(message, 20, 20);
   }
}


A. The program runs fine and displays nothing since you have not set a string value.
B. The program would display Welcome to Java! if you replace new MyCanvas() by new MyCanvas("Welcome to Java!").
C. The program has a syntax error because new Test1() is assigned to frame.
D. The program has a NullPointerException since message is null when g.drawString(message, 20, 20) is executed.

Section 10.7 The Color Class
16   The method __________ sets the background color to yellow in JFrame f.

A. setBackground(Color.yellow)
B. f.setBackground(Color.YELLOW)
C. f.setBackground(Color.yellow)
D. setBackground(Color.YELLOW)
E. b and c

Section 10.8 The Font and FontMetrics Classes
17   The method __________ sets the font (Helvetica, 20-point bold) in Graphics object g.

A. g.setFont(new Font("Helvetica", Font.bold, 20))
B. g.setFont(new Font("helvetica", BOLD, 20))
C. g.setFont(Font("Helvetica", Font.BOLD, 20))
D. g.setFont(new Font("Helvetica", Font.BOLD, 20))

Section 10.9 Drawing Geometric Figures
18   Given a Graphics object g, to draw an outline of a rectangle of width 20 and height 50 with the upper-left corner at (20, 20), you use __________.

A. g.drawRect(20, 50, 20, 20)
B. g.drawRectFill(20, 20, 20, 50)
C. g.drawRect(20, 20, 20, 50)
D. g.drawRectFill(20, 50, 20, 20)

19   Given a Graphics object g, to draw an circle with radius 20 centered at (50, 50), you use __________.

A. g.drawOval(50, 50, 20, 20)
B. g.drawOval(50, 50, 40, 40)
C. g.drawOval(30, 30, 20, 20)
D. g.drawOval(30, 30, 40, 40)

20   Given a Graphics object g, to draw a filled arc with radius 20 centered at (50, 50) and start angle 0 and spanning angle 90, you use __________.

A. g.fillArc(50, 50, 40, 40, 0, Math.toRadian(90))
B. g.fillArc(50, 50, 40, 40, 0, 90)
C. g.fillArc(30, 30, 40, 40, 0, Math.toRadian(90))
D. g.fillArc(30, 30, 40, 40, 0, 90)
E. g.fillArc(50, 50, 20, 20, 0, 90)

Section 10.11 Event-Driven Programming
21   Pressing a button generates a(n) __________ event.

A. ItemEvent
B. MouseEvent
C. MouseMotionEvent
D. ActionEvent
E. ContainerEvent

22   Clicking the closing button on the upper-right corner of a frame generates a(n) __________ event.

A. ItemEvent
B. WindowEvent
C. MouseMotionEvent
D. ComponentEvent
E. ContainerEvent

Section 12.3 Listeners, Registrations, and Handling Events
23   Which of the following statements registers a panel object p as a listener for a button variable jbt?

A. addActionListener(p);
B. jbt.addActionListener(p);
C. jbt.addActionEventListener(p);
D. jbt.addEventListener(p);

24   The interface __________ should be implemented to listen for a button action event.

A. MouseListener
B. ActionListener
C. FocusListener
D. WindowListener
E. ContainerListener

25   The method in the ActionEvent __________ returns the action command of the button.

A. getActionCommand()
B. getModifiers()
C. paramString()
D. getID()

26   To be a listener for ActionEvent, an object must be an instance of ____________.

A. ActionEvent
B. ActionListener
C. EventObject
D. WindowListener
E. WindowEvent

27   The handler (e.g., actionPerformed) is a method in ________.

A. a source object
B. a listener object
C. both source and listener object
D. the Object class
E. the EventObject class

28   Analyze the following code.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Test extends JFrame implements ActionListener {
   public Test() {
     JButton jbtOK = new JButton("OK");
     getContentPane().add(jbtOK);
   }

   public void actionPerformed(ActionEvent e) {
     System.out.println("The OK button is clicked");
   }

   public static void main(String[] args) {
     JFrame frame = new Test();
     frame.setSize(300, 300);
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setVisible(true);
   }
}


A. The program has a syntax error because no listeners are registered with jbtOK.
B. The program has a runtime error because no listeners are registered with jbtOK.
C. The message "The OK button is clicked" is displayed when you click the OK button.
D. The actionPerformed method is not executed when you click the OK button, because no instance of Test is registered with jbtOK.
E. None of the above.

29   Analyze the following code.


1. import java.awt.*;
2. import java.awt.event.*;
3. import javax.swing.*;
4.
5. public class Test extends JFrame {
6. public Test() {
7. JButton jbtOK = new JButton("OK");
8. JButton jbtCancel = new JButton("Cancel");
9. getContentPane().add(jbtOK);
10. getContentPane().add(jbtCancel);
11. jbtOK.addActionListener(this);
12. jbtCancel.addActionListener(this);
13. }
14.
15. public void actionperformed(ActionEvent e) {
16. System.out.println("A button is clicked");
17. }
18.
19. public static void main(String[] args) {
20. JFrame frame = new Test();
21. frame.setSize(300, 300);
22. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
23. frame.setVisible(true);
24. }
25.}


A. The program has syntax errors on Lines 11 and 12 because Test does not implement ActionListener.
B. The program has syntax errors on Line 15 because the signature of actionperformed is wrong.
C. The program has syntax errors on Line 20 because new Test() is assigned to frame (a variable of JFrame).
D. The program has runtime errors on Lines 9 and 10 because jbtOK and jbtCancel are added to the same location in the container.
E. None of the above.

30   Analyze the following code.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Test extends A {
   public static void main(String[] args) {
     A frame = new Test();
     frame.setSize(200, 100);
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setVisible(true);
   }

   JButton jbtOK = new JButton("OK");

   public Test() {
     getContentPane().add(jbtOK);
     jbtOK.addActionListener(this);
   }

   public void actionPerformed(ActionEvent e) {
     super.actionPerformed(e);

     if (e.getSource() == jbtOK)
       System.out.println("OK button is clicked");
   }
}

class A extends JFrame implements ActionListener {
   JButton jbtCancel = new JButton("Cancel");

   public A() {
     getContentPane().setLayout(new FlowLayout());
     getContentPane().add(jbtCancel);
     jbtCancel.addActionListener(this);
   }

   public void actionPerformed(ActionEvent e) {
     if (e.getSource() == jbtCancel)
       System.out.println("Cancel button is clicked");
   }
}

A. The program displays Cancel button on the left of the OK button.
B. When you click the OK button the message "OK button is clicked" is displayed.
C. When you click the Cancel button the message "Cancel button is clicked" is displayed.
D. If the super.actionPerformed(e) statement in the actionPerformed method in the Test class is omitted, no message is displayed if you click the Cancel button.
E. All of the above.