PIC 20A Lecture 1, Spring 2001
Instructor: Fedor A. Andrianov
MIDTERM EXAM
STUDENT DATA:
- NAME: __________________________________
- ID#: _____________________________________
- SECTION#: ________________________________
- 1. (20 points):
- Fill in the blanks in each of the following:
- Operator ________________ determines whether its first operand is
an instance of its second operand.
- In order to support
java.lang.Object.clone() method,
an object should implement ________________ interface.
- The ________________ class is the top of the class hierarchy. All
classes are descendants from this class and inherit methods from it.
- A class member that is not declared as ________________ is implicitly
an instance member.
- You can prevent a class from being subclassed by using
the ________________ keyword.
- 2. (20 points):
- The following program contains several bugs. Please find and fix them.
public class SomethingIsWrong {
public interface anInterface {
void aMethod(int aValue) { System.out.println("Hi Mom"); }
}
public static void main(String[] args) {
StringBuffer[] stringBuffers = new StringBuffer[10];
for (int i = 0; i < stringBuffers.length; i++) {
stringBuffers[i].append("String Buffer at index " + i);
}
}
}
Please write your answer on the other side of page 1 --->
- 3. (20 points):
- Answer the following questions:
- What is the value of the following expression and why?
new Integer(1).equals(new Long(1))
- Is the following code legal?
try {
// do something
} finally {
// do something
}
- What is wrong with using the following type of exception handler?
catch (Exception e) {
// do something
}
- What is the value of
i after the following code snippet
executes?
int i = 17; i >>= 2; i = i++%5;
- Answers:
-
- 4. (20 points):
- Suppose that you have written a time server, which periodically notifies
its clients of the current date and time. Write an interface that the
server could use to enforce a particular protocol on its clients.
Please write your answer on the other side of page 2 --->
- 5. (20 points):
- Create a program that reads an unspecified number of integer arguments
from the command line and adds them together. For example, suppose that
you enter the following:
java Adder 1 3 2 10
The program should display 16 and exit. In addition, your program should
catch and handle all exceptions that might arise because of a bad input.
Each type of exception should be handled independently, by printing an
appropriate error message which would specify what exactly was wrong
with the input. Hint: to convert a string representing an
integral number into an integer, you can use method
public static int java.lang.Integer.parseInt(String s)
throws NumberFormatException
Good Luck !
Last modified on
by fedandr@math.ucla.edu.