Developed by Y. Daniel Liang using Java Servlets (Current time is Sat Nov 21 19:00:08 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 12: Applets and Advanced GUI

Section 12.2 The Applet Class
1   The default layout of the contentPane of a JApplet is __________.

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

2   If your applet does not have the init() method, which of the following will happen?

A. Your program will not compile.
B. Your program will compile, but not execute.
C. You must have a main method.
D. Your program will run just fine since the init() method is defined in the Applet class.

3   The method that executes immediately after the init() method in an applet is __________.

A. destroy()
B. start()
C. stop()
D. run()

4   The __________ method is executed when the page becomes inactive.

A. init()
B. start()
C. stop()
D. destroy()

5   When you run an applet, which of the following is invoked first.

A. The init method.
B. The start method.
C. The stop method.
D. The destroy method.
E. The applet's default constructor.

Section 12.3 The JApplet Class
6   When you run the following applet from a browser, what is displayed:

import javax.swing.*;

public class Test extends JApplet {
   public Test() {
     System.out.println("Default constructor is invoked");
   }

   public void init() {
     System.out.println("Init method is invoked");
   }
}



A. Default constructor is invoked
Init method is invoked
B. Init method is invoked
Default constructor is invoked
C. Default constructor is invoked
D. Init method is invoked

7   Analyze the following code.

import javax.swing.*;

public class Test extends JApplet {
   public void init() {
     getContentPane().add(new JLabel("OK"));
   }

   public static void main(String[] args) {
     Test applet = new Test();
   }
}


A. When you run the applet from a browser, the OK label is displayed.
B. When you run the applet standalone from the main method, nothing is displayed.
C. When you run the applet standalone from the main method , the OK label is not displayed because the init() was not invoked.
D. All of the above.

Section 12.4 The HTML File and the <applet> Tag
8   To use an applet in the HTML document, you use __________ in the <applet> tag.

A. .java source code file
B. .class bytecode
C. .exe executable file
D. .html file

9   The HTML tags are enclosed within __________.

A. curly braces
B. parentheses
C. angle brackets
D. square brackets

Section 12.5 Passing Parameters to Applets
10   To specify an HTML parameter named message with the value "Welcome to Java", you write the following HTML tag:

A. <param name=message, value="Welcome to Java">
B. <param name=message, value=Welcome to Java>
C. <param name=message value="Welcome to Java">
D. None of the above.

11   To retrieve an HTML parameter named message, you write the following code in the init() method:

A. String s = Parameter("message");
B. String s = getParameter("message");
C. String s = getParameter("MESSAGE");
D. Either b or c

12   If you try to retrieve an HTML parameter that is not defined in the HTML page, what will happen?

A. You will get a syntax error.
B. You will get a runtime error.
C. Your program will run with the parameter's value null.
D. Your program will run with an empty string in the parameter's value.

13   Suppose you pass parameter named message from HTML to the following applet, analyze the code:

import javax.swing.*;

public class Test extends JApplet {
   public Test() {
     String message = getParameter("MESSAGE");
     getContentPane().add(new JLabel(message));
   }
}


A. The program has a syntax error because parameter names are case-sensitive. You should replace MESSAGE with message.
B. The program has a syntax error because you cannot invoke getParameter("MESSAGE") from the constructor.
C. The program has a runtime error because you cannot invoke getParameter("MESSAGE") from the constructor.
D. The program runs fine and displays the label with the message passed from the HTML file.

Section 12.6 Enabling Applets to Run as Applications
14   Java applications and applets both __________.

A. have a main() method
B. are executed using the java command
C. are compiled using the javac command
D. are executed from the HTML file

Section 12.7 Mouse Events
15   To detect whether the right button of the mouse is pressed, you use the method __________ in the MouseEvent object evt.

A. evt.isAltDown()
B. evt.isControlDown()
C. evt.isMetaDown()
D. evt.isShiftDown()

16   To listen to mouse movement events, the listener must implement the __________ interface.

A. MouseListener()
B. MouseMotionListener()
C. WindowListener()
D. ComponentListener()

17   To get the x coordinate of the mouse pointer for the MouseEvent evt, you use __________.

A. evt.getX()
B. evt.getPoint().x
C. Either a or b
D. Neither a nor b

18   The listener's method __________ is invoked when a mouse button is released.

A. public void mousePressed(MouseEvent e)
B. public void mouseReleased(MouseEvent e)
C. public void mouseEntered(MouseEvent e)
D. public void mouseExited(MouseEvent e)
E. public void mouseClicked(MouseEvent e)

Section 12.8 Keyboard Events
19   To listen to keyboard actions, the listener must implement the __________ interface.

A. MouseListener()
B. KeyListener()
C. WindowListener()
D. ComponentListener()

20   The getKeyCode() method of the KeyEvent returns __________.

A. a character
B. the ASCII code of the character
C. the Unicode code of the character
D. None of the above.

21   What is the value of evt.getKeyCode() or evt.getChar() for the keyTyped() events?

A. A character
B. The ASCII code of the character
C. The Unicode code of the character
D. VK_UNDEFINED

22   To enable a component to listen to keyboard events, you need to ____

A. Implement the KeyListener interface for the component.
B. Override the isFocusTraversable method defined in the Component class to return true.
C. Invoke the component?s requestFocus method to set focus on this component.
D. All of the above.

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

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

24   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

Section 12.9 Modeling Dynamic Behavior Using Sequence Diagrams and Statecharts
25   __________ describe interactions among objects by depicting the time-ordering of method invocations.

A. Sequence diagrams
B. Statechart diagrams
C. Flowchart diagrams
D. Class diagrams

26   __________ describe the flow of control of an object.
A. Sequence diagrams
B. Statechart diagrams
C. Flowchart diagrams
D. Class diagrams