CS/IT 115 Midterm Sample Questions
Spring 2013
The
questions on this page are only meant to give you an idea of the kinds of
questions we can ask. The actual exam
may look very similar or very different from the examples here.
The first two questions refers to the RaceCar class:
2 // Robert Cohen, October 2002
5 * A simulation of a race car
13 * Construct a RaceCar
instance.
15 * @param
myName the name of the car.
16 * @param
mySpeed the average speed of the car.
18 public RaceCar(String myName, int mySpeed)
{
31 * Return the car's position at time t.
33 * @param
time the time into a race.
35 public int positionAt(int time) {
36 return time * averageSpeed;
1. List the API of the RaceCar class.
2. Write client code that creates a RaceCar called “speeder” that has an average speed of 100 (miles/hour). Then your code uses positionAt to find out how many miles the car has gone in 2 hours.
3. a. If counts[] = { 0,0,4,2,3,0,0,1,0} before the following is called, and age = 2, what happens when the following is called?
static boolean
matchAge(int[] counts, int age)
{
if (age < 0 || age >= counts.length)
return false;
if (counts[age]> 0)
return true;
else return false;
}
b. Give
a value for age that will make the method return false.
c.What
is the purpose of this method?
4. Suppose a program, a client of Point (code on pg. 530), has an array of Point objects as follows:
Point[]
points = new Point[2];
points[0] = new Point(1,5);
points[1] = new Point(2,3);
Give an assignment statement that defines a double variable named “distance0” and gives it a value of the distance from the origin of the last point in array points.
5. In the Automobile/Truck/Vehicle example we covered in class, we can write the involved APIs as follows:
API for Automobile, which implements Vehicle:
Automobile(String make, String model, int year, String vehicleID)
String getMake() car’s
make
String getModel() car’s model
int getYear()
car’s model year
String getVIN()
car’s vehicle ID
API for Truck, which implements
Vehicle:
Truck(String make, int truckClass, String vehicleID)
String getMake()
truck’s make
int getClass()
truck’s class (1 to 8)
String getVIN()
truck’s vehicle ID
a. Write down the API for Vehicle:
b.
Create
a Automobile object named “oldCar” of make “Ford”, model “Taurus”, VIN
“1G12345”, and year 2002. Note that this involves setting up an Automobile
variable named “oldCar” that references an Automobile
object.
c.
Create
a Truck object named “newTruck” of make and VIN of
your choice, and class 3.
d.
Create
a Vehicle variable named “vehicle1” that references the Automobile named “oldCar”
Note that after
this executes, the Vehicle (or more specifically Automobile) object has two
references to it, so two names, “oldCar” and
“vehicle1”.
e.
Create
a Vehicle variable “vehicle2” that references the Truck named “newTruck”
f. Draw a little picture showing the
objects and references of this problem.
6. Continuing with the Automobile/Truck/Vehicle example, suppose you have an array of Vehicles “inventory” already filled with Vehicle objects. Explain what you can do with this array, that is, what kind of information can you access about these objects by using the fact that the objects are all of type Vehicle?
7. Write
a static method called stringRepeat that takes
two parameters: a String
s
and an int n and returns a new String
that consists of n
copies of String
s. You can assume that n
is non-negative.
For example:
· StringRepeat( “Bob”, 3 ) should return “BobBobBob”
· StringRepeat( “Grape”, 0 ) should return “”