Introduction to Java Programming, Seventh Edition, Y. Daniel Liang

Chapter 17 Applets and Multimedia


Section 17.2 The Applet Class
1  Which of the following is true?

A. Any applet must be an instance of java.awt.Applet.
B. You must always provide a no-arg constructor for an applet.
C. You must always provide a main method for an applet.
D. You must always override the init method in an applet.

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 17.3 The JApplet Class
6  Which of the following is true?

A. The Applet class is in javax.swing package.
B. The JApplet class is in javax.swing package.
C. Applet is a subclass of JApplet.
D. JApplet is a subclass of Applet.

7  The default layout of the contentPane of a JApplet is __________.

A. FlowLayout
B. GridLayout
C. BorderLayout
D. None

Section 17.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

10  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, then Init method is invoked
B. Init method is invoked, then Default constructor is invoked
C. Default constructor is invoked
D. Init method is invoked

11  Analyze the following code.

import javax.swing.*;

public class Test extends JApplet {
   public void init() {
     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 17.5 Enabling Applets to Run as Applications
12  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

13  Which of the following statements are true?

A. If an applet has a main method, it can be executed standalone.
B. An applet is a GUI component and it can be added to a container.
C. When an applet runs from a browser, its main method (if exists) is not invoked by the browser.
D. When an applet runs from a browser, its init method is not invoked after its no-arg constructor is invoked.

14  Which of the following statements are true?

A. Applets are not allowed to read from, or write to, the file system of the computer.
B. Applets are not allowed to run programs on the browser's computer.
C. Applets are not allowed to establish connections between the user?s computer and any other computer, except for the server where the applets are stored.
D. In general, an applet can be converted to an application without loss of functionality.
E. An application can be converted to an applet as long as it does not violate the security restrictions imposed on applets.

Section 17.6 Passing Strings to Applets
15  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.

16  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. String s = getParameter("Message");

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

A. You will get a compile 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.

18  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");
     add(new JLabel(message));
   }
}

A. The program has a compile error because parameter names are case-sensitive. You should replace MESSAGE with message.
B. The program has a compile 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 17.9 (Optional) Locating Resource Using the URL Class
19  Which of the following statements are true?

A. You can create a URL object for any public accessible resource on the Internet.
B. You can create a URL object for a local file.
C. A URL object may be created using the new URL(urlString) constructor.
D. A URL object may be returned using the getSource method on a meta object (instance of java.lang.Class).

20  Which of the following statements are true?

A. Classes are dynamically loaded when needed.
B. When a class (e.g., java.lang.Math) is loaded, the JVM creates a meta-object for the class.
C. Two objects of the same class share the same meta-object.
D. A class will be loaded only once in an application or applet.

21  To get an ImageIcon for the a specified URL, you use __________.

A. getImage(url)
B. createImage(url)
C. url.getImage()
D. url.createImage()
E. new ImageIcon(url);

22  What format of image files are currently supported?

A. BMP
B. GIF
C. JPEG
D. PNG

23  To create a URL for an image file located in the same directory with the class, use

A. Class metaObject = this.getResource(imageFilenameString);
B. URL url = this.getClass().getResource(imageFilenameString);
C. Class metaObject = this.getClass(); URL url = metaObject.getResource(imageFilenameString);
D. Object metaObject = this.getClass(); URL url = metaObject.getResource(imageFilenameString);

24  To obtain an instance of Image from an ImageIcon, use

A. Image image = ImageIcon.getImage();
B. Image image = imageIcon.getImage();
C. Image image = ImageIcon.createImage();
D. Image image = imageIcon.createImage();

25  To draw the image im to fill in the whole viewing area, you use __________.

A. drawImage(im, 10, 10)
B. drawImage(im, 0, 0, getSize().width, getSize().height)
C. drawImage(im, 0, 0, this)
D. drawImage(im)

26  The ImageViewer class was presented in Chapter 17. You can use the method _________ to create an Image from a file "image/us.gif" in the same directory of the this class.

A. ImageViewer.createImage("image/us.gif", this)
B. ImageViewer.createImageIcon("image/us.gif", this)
C. ImageViewer.createImage("image/us.gif", null)
D. ImageViewer.createImageIcon("image/us.gif", null)

27  The ImageViewer class was presented in Chapter 17. Which of the following statements are true?

A. ImageViewer is a subclass of JPanel.
B. You can display an image in an ImageViewer.
C. You can specify where an image is displayed in an ImageViewer.
D. You can specify whether an image can be stretched in an ImageViewer.

Section 17.10 (Optional) Playing Audio
28  What format of audio files are currently supported?

A. WAV
B. AIFF
C. MIDI
D. AU
E. RMF

29  To create an audio clip for a file named beep.au in the same directory of the applet, you use __________.

A. URL url = this.getClass().getResource("beep.au"); AudioClip audioClip = Applet.newAudioClip(url);
B. Class metaObject = this.getClass(); URL url = metaObject.getResource("beep.au"); AudioClip audioClip = Applet.newAudioClip(url);
C. URL url = this.getClass().getResource("beep.au"); AudioClip audioClip = Applet.createAudioClip(url);
D. Class metaObject = this.getClass(); URL url = metaObject.getResource("beep.au"); AudioClip audioClip = applet.createAudioClip(url);

30  To repeatedly play an instance of audio clip ac, you use __________.

A. ac.loop()
B. ac.repeat()
C. ac.repeating()
D. ac.continue()

Section 17.12 (Optional) Packaging and Deploying Java Projects
31  The _________ is a special file that contains information about the files packaged in a JAR file.

A. class file
B. source file
C. text file
D. manifest file

32  Suppose the manifest file in temp.jar specifies a main class Test, to run the main class from the jar file, use the following command:
A. java Test -jar temp.jar
B. jar -jar temp.jar Test
C. java -jar temp.jar
D. java -jar temp.jar Test