厦门大学《Java 程序设计》课程试卷
软件 学院 软件工程 系 07 年级 软件工程 专业
主考教师:王美红 试卷类型:(A 卷/B 卷)
1、 Multiple Choice(20 point)
Identify the letter of the choice that best completes the statement or answers the question.
(1) The ____ statement identifies a block of statements that potentially may throw an exception.
a.
c.
catch
throw
b.
d.
finally
try
(2) Which method is used to invoke a constructor of the same class?
a.
c.
this()
super()
b.
d.
constructor()
class()
(3) The ____ is a wildcard symbol to tell a program to import all necessary classes from a package.
a.
c.
asterisk
period
b.
d.
comma
semicolon
(4) Which of the following is not a reference type in Java?
a.
c.
array
float
b. BufferedReader
d. String
(5) The ____ class decodes bytes into characters.
a. BufferedInputStream b. BufferedReader
c.
InputStreamReader
d. System.in
(6) ____ is a class used to display standard dialog boxes.
a. Box
c.
JOptionPane
b.
d.
JFrame
JWindow
(7) In java 2D, method _______of class Graphics2D sets the characteristics of a line used to draw a
shape.
a.
c.
setBackground
setPaint
(8) A ______ is needed to terminate recursion.
a.
c.
recursion step
break statement
b.
d.
b.
d.
setStroke
translate
void return type
base case
(9) What type of error will be produced first during the execution of the following code?
第 1 页 共 8 页
Object[] array = new Object[4];
for(int i=0;i<= array.length;i++) System.out.println(array[i] ) ;
a. ArrayIndexOutOfBoundsException
b. StringIndexOutOfBoundsException
c.
cannot resolve symbol
d. NullPointerException
(10)The following is a list of classes / interfaces defined in Java's standard packages, List all
interfaces in the above list.
a. Collection
d. Thread
g.
Integer
b. Math
e. Runnable
h. String
c. List
f. Collections
2、 True/False(10 points)
Indicate whether the sentence or statement is true or false.
(1) Multiple inheritances are not allowed in Java.
(2) If I do not provide any arguments on the command line, then the String array of Main method
will be null.
(3) java.lang package is by default loaded internally by the JVM.
(4) Importing a package imports the subpackages as well. e.g. Importing com.MyTest.* also import
com.MyTest.UnitTests.*.
(5) The default value of an object reference declared as an instance variable is null.
(6) If I write System.exit (0); at the end of the try block, the finally block will still execute.
(7) When a thread is created and started, its initial state is runnable state.
(8) The purpose of finalization is to give an unreachable object the opportunity to perform any
cleanup processing before the object is garbage collected.
(9) Static methods can be referenced with the name of the class rather than the name of a particular
object of the class.
(10)The local variables are not initialized to any default value, neither primitives nor object
references.
3、 Short answer(25 points)
(1) Which method can you use to perform binary search for elements in an ArrayList or a
LinkedList? Which method can you use to sort an array of strings?(4 points)
第 2 页 共 8 页
(2) Difference between Vector and ArrayList?(2 points)
(3) What is the difference between method overloading and method overriding? Can static methods
be overridden? Can constructor be overridden?(6 points)
(4) What is the purpose of garbage collection in Java, and when is it used?(2 points)
(5) Explain how the Java interface works. What is the difference between an abstract class and an
interface? Produce examples of “implements” relationships.(3 points)
(6) State the significance of public, private, protected, default modifiers both singly and in
combination and state the effect of package relationships on declared items qualified by these
modifiers.(4 points)
(7) What are Checked and UnChecked Exception?(2 points)
(8) Within a method m in a class C, isn't this.getClass() always C? (2 points)
4、 Completion(45 points)
Complete each sentence or statement.
(1) (2 points) An applet can also get references to all other applets on the same page using the
getApplets() method of
(1)
.
(2) (5 points) Java applets begin execution with a series of three method calls:
,
(3)
method is invoked for an applet each time the user
of a browser leaves an HTML page on which the applet resides. Every applet should extend
class
and
.The
(5)
(6)
.
(2)
(4)
(3) (4 points) Consider the following classes:
public class AnimalHouse {
private E animal;
public void setAnimal(E x) {
animal = x;
}
public E getAnimal() {
return animal;
}
}
public class Animal{
}
public class Cat extends Animal {
}
public class Dog extends Animal {
}
第 3 页 共 8 页
For the following code snippets(代码片段) from a to d, identify whether the code:
fails to compile
(7)
compiles with a warning
generates an error at runtime
none of the above (compiles and runs without problem.)
(8)
(9)
or
(10)
a. AnimalHouse
house = new AnimalHouse();
b. AnimalHouse house = new AnimalHouse();
c. AnimalHouse house = new AnimalHouse();
d. AnimalHouse house = new AnimalHouse();
house.setAnimal(new Dog());
(4) (5 points) Assume the following:
a = "abc";
Give the values of the following expressions, or illegal.
(11)
(12)
(13)
(14)
(15)
"Tomorrow".substring(2,4)
a.length() + a
a.length() + a.startsWith("a")
"a".compareTo("c")
"a = \"" + a + "\""
(5) (5 points)Write a method to copy a file from file1 to file2:
public static void copy(File file1, File file2) {
(16)
}
(6) (6 points) Producer and Consumer are two threads which both implement interface Runnable.
Complete the BlockingBufferTest class below to start the two threads.
public class BlockingBufferTest
{
public static void main( String[] args )
{
// create new thread pool with two threads
ExecutorService application =
(17)
;
// create BlockingBuffer to store ints
Buffer sharedLocation = new BlockingBuffer();
第 4 页 共 8 页
try // try to start producer and consumer
{
application.
application.
} // end try
(18)
(18)
( new Producer( sharedLocation ) );
( new Consumer( sharedLocation ) );
catch ( Exception exception )
{
exception.printStackTrace();
} // end catch
application.
} // end main
(19)
;
} // end class BlockingBufferTest
(7) (5 points) Establishing a simple server using stream sockets in java requires five steps.
Step 1, to create an object, such as
(20)
(portNumber,queueLength);
Step 2, the server listens indefinitely for an attempt by a client to connet. Such in
server = new
(20)
(21)
connection = server.
(22)
;
Step 3, to get the OutputStream and
the client by sending and receiving bytes.
Step 4, is the processing phase.
Step 5, when the transmission is complete, the server closes the connection by invoking the
method on the streams and on the Socket.
objects that enable that server to communicate with
(23)
(24)
(8) (4 points) Write method numInArray, as started below. numInArray should return the number
of times an even int parameter occurs in array A. For example, assume that array A is as shown
below.
[0]
6
[1]
3
[2]
4
[3]
5
[4]
2
[5]
7
then numInArray(A) should return 3 since 6, 4, and 2 are even.
Complete method numInArray below.
[6]
3
public static int numInArray(int[] A)
{
// precondition: A will contain integers greater than zero
// postcondition: returns the number of even integers found in A
(25)
}
(9) (9 points) Complete the following program to do the following:
第 5 页 共 8 页
a. Add buttons to the South region labeled "North", "South", "East", and "West".
b. Create an X-shaped cross 10 pixels wide and 10 pixels high.
c. Adds the cross so that its center is at the center of the graphics canvas.
Once you have completed these steps, the display should look like this:
d.Implement the actions for the button so that clicking on any of these buttons moves the cross
20 pixels in the specified direction.
//GraphicsProgram.java
import java.awt.BorderLayout;
public class GraphicsProgram extends JFrame {
private SouthPanel southPanel;
private CenterPanel centerPanel;
public GraphicsProgram(String title) {
super(title);
southPanel = new SouthPanel();
southPanel.setAlignmentX(FlowLayout.CENTER);
centerPanel = new CenterPanel();
add(centerPanel, BorderLayout.CENTER);
add(southPanel, BorderLayout.SOUTH);
}//end constructor
public static void main(String args[]) {
GraphicsProgram frame = new GraphicsProgram("GraphicsProgram");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 400);
frame.setVisible(true);
第 6 页 共 8 页
}
class SouthPanel extends JPanel implements ActionListener {
private JButton northButton, southButton, eastButton, westButton;
public SouthPanel() {
northButton = new JButton("North");
southButton = new JButton("South");
eastButton = new JButton("East");
westButton = new JButton("West");
northButton.addActionListener(this);
southButton.addActionListener(this);
eastButton.addActionListener(this);
westButton.addActionListener(this);
add(northButton);
add(southButton);
add(eastButton);
add(westButton);
}//end constructor
public void actionPerformed(ActionEvent arg0) {
(26)
}//end method actionPerformed.
}//end class SouthPanel
}
//CenterPanel.java
import java.awt.*;
import javax.swing.*;
public class CenterPanel extends JPanel {
private int centerX; // the horizontal x point in the ¡Á center
private int centerY; // the vertical y point in the ¡Á center
private Boolean initState = true; // not have been moved
public CenterPanel() {
this.setBackground(Color.WHITE);
}//end constructor
public void paintComponent(final Graphics g) {
(27)
第 7 页 共 8 页
//set and get method
public void setInit(Boolean init) {
this.initState = init;
public void setX(int centerX) {
if (centerX >= 0)
this.centerX = centerX;
paintComponent(this.getGraphics());
}
}
}
}
public void setY(int centerY) {
if (centerY >= 0)
this.centerY = centerY;
paintComponent(this.getGraphics());
public int getX() {
return centerX;
}
public int getY() {
return centerY;
}
}//end class CenterPanel
第 8 页 共 8 页