README

These are questions culled from the written part of the weekly tests from Fall 2010 to form an 80 minute sample midterm (more or less).

You will not see any multiple choice questions on this year's midterm, and the midterm will be on paper (not electronic like these questions).


Chapter 1: Introduction to Programming

Question 1 What is a type?

A. A set of values.
B. A set of operations.
C. Something with a name and a value.
D. A set of values and the operations that can be performed with those values.



For Questions 2-5 consider following fragment of Java code:
double f = 1.0 / 49.0;
int one = (int) (f * 49);
Question 2 What is the type of the variable named one?




Question 3 What is the type of (f * 49)?




Question 4 Why is the cast to int required?




Question 5 If you print the value of one the output is the number zero.
Explain why the value computed by Java is zero whereas the mathematical value is one.

Chapter 2: Delegation

Question 1 A utility class advertises its services through a document called the interface, also called the application programming interface. Which features of the utility class appear in the interface? Choose the best answer.

A. its public attributes
B. its public methods
C. all of its attributes and methods
D. only its public attributes and methods


For Questions 2-5 consider the contract for the static method daysInYear in a utility class named PlanetEarth.

daysInYear

int daysInYear(long year)
Returns the number of days in the given year.

Parameters:
year - the year.
Returns:
the number of days.
Throws:
IndexOutOfBoundsException - If the year < 0

Question 2 What type of value does the method return?


Question 3 Does the method have any preconditions (not including trivial ones like true or false)?

A. yes
B. no

Question 4 Suppose that as a client, you write the following line of code:
double days = PlanetEarth.daysInYear(-1.0);
What do you expect to happen?

A. a runtime error because you broke a precondition
B. a compile-time error because you stored the return value in a double
C. an exception will be thrown by the method because the year was negative
D. a compile-time error because you used a double value for the year

Question 5 Suppose that as a client, you invoke the method and it returns the value -500. Who is responsible for this incorrect value and why?

Chapter 3: APIs

Load the java.lang.Integer API into your browser and answer the following questions:

Question 1 What types do the following methods return?

decode(String nm)
floatValue()
longValue()
reverse()
valueOf(String s)


Question 2 Which versions of each method are invoked? Assume that num is a variable of type Integer.

num.parseInt("123", 10);
Integer.toString(1);
num.toString();
Integer.valueOf("123");
Integer.valueOf(123);


Question 3 How many mutator methods does Integer have?.




Question 4 (Has nothing to do with Integer). Suppose that you are using a method that accepts a Fraction reference as one of its arguments.

Fraction f = new Fraction();
Fraction g = new Fraction(f);
SomeUtility.changeState(f);
System.out.println(f.equals(g));

Can the method SomeUtility.changeState() actually change the state of f so that the last line of code prints false?




Question 5 (Has nothing to do with Integer). Suppose that you are using a method that accepts a Fraction reference as one of its arguments.

Fraction f = new Fraction();
Fraction g = f;
SomeUtility.changeIdentity(f);
System.out.println(f == g);

Can the method SomeUtility.changeIdentity() actually change the identity of f so that the last line of code prints false?




Chapter 4: Using Objects

Question 1 The constructor has zero parameters.


Question 2 Suppose that f is a reference to a Fraction object; then the following code
Fraction g = new Fraction(f);
is an example of a constructor.


Question 3 What method or operator is used to determine if two reference variables refer to the same object?



Question 4 What method or operator is used to determine if two objects have the same state?


Question 5 The textbook says that an object has both identity and state; briefly explain what is meant by object identity and object state.

Chapter 5: Control

Question 1 Consider the following code fragment:
ArrayList<Integer> x = new ArrayList<Integer>();
x.add(2);
x.add(1);
x.add(4);
x.add(5);
x.add(7);

int limit = 3;
int i = 0;
int sum = 0;
while ( (sum < limit) && (i < x.size()))
{
   // A
   i++;
   sum = sum + x.get(i);
   // B
}

What value is in the variable i after this code is run? Hint: trace the values of i and sum at points A and B each time through the loop.

Chapter 6: Strings

Question 1 Strings in Java are a bit weird: They behave like primitive types in some respects, and behave like objects in other respects. Give two examples where a string behaves like a primitive type.
Question 2 The regular expression
String regex = "[xy]";

matches (choose the best answer):

A. "x"
B. "y"
C. both A and B
D. neither A nor B

Question 3 The regular expression
String regex = "[A-Z]*";
matches (choose the best answer):

A. any lowercase English letter
B. zero or more uppercase English letters
C. a single uppercase English letter
D. none of the above

Question 4 Give a regular expression that matches every string ending with a letter.


Question 5 Give a regular expression that matches lowercase hypenated English words; for example:
b-spline
twentieth-century
lily-of-the-valley