



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Cornell university cs exam questions and solutions from previous years. The questions cover various topics such as loops, classes, and inheritance. Students can use this document as a study resource to prepare for exams, quizzes, or assignments. Exam questions with solutions for classes point, animal, elephant, and asianelephant.
Typology: Exams
1 / 6
This page cannot be seen from the preview
Don't miss anything!




Cornell net id ________________________ Name ________________________________
Section day __________________________ Section time ___________________________
This 90-minute exam has 6 questions (numbered 0..5) worth a total of
100 points. Spend a few minutes looking at all questions before begin-
ning. Use the back of the pages if you need more space.
Question 0 (2 points). Fill in the information, legibly, at the top of
each page (Hint: do it now.)
Question 1 (18 points)
(a) What is a loop invariant?
(b) Below is a loop. Fill in the invariant, then the initialization, then the loop body.
// n > 0.
// Assume that function f( int i) returns an int value.
// Store in m the maximum value of f(i) for i in the range 0..n-1.
int max= ;
// invariant:
for ( int k= 1; k < n; k= k+1) {
}
// m = maximum value of f(i) for i in the range 0..n– 1
Cornell net id ________________________ Name ________________________________
Section day __________________________ Section time ___________________________
Question 2 (20 points): Below is a diagram of the x-y plane. Each point in the plane is
determined by its x-coordinate and y-coordinate. In the diagram, point P has x-coordinate 3 and
y-coordinate 5.
An instance of class Point contains the x-coordinate and y-coordinate of a point. Note that an object
of class Point is “immutable”: there is no way to change its fields.
On the back of the previous page, write the body of the procedure that is specified below. If you write
a loop, you need not write a loop invariant, although it may help you to do so.
/** Replace all points in v whose x- and y-coordinates are both negative by the corresponding
points whose x- and y-coordinates are positive. E.g. replace a point (-3, - 5) in v by
point (3, 5). All other points in v remain unchanged. E.g. change
v = [(-3, 5), (-3, - 5), (-2, - 6), (3, 5)] to [(-3, 5), (3, 5), (2, 6), (3, 5)] */
public static void makePos(Vector< Point > v)
You can use the following methods:
Assume v has type Vector
Return Method Purpose
C v.get( int k) = v[k].
int v.size() = the number of elements in v.
v.set( int k, C ob) Store ob in v[k].
v.remove( int k) Remove element v[k], changing v so that it
contains v[0..k–1] followed by v[k+1..]
/** instance is a pt in x-y plane */
public class Point {
private int x; // x coordinate
private int y; // y coordinate
/** Constructor: Point with x
coordinate a and y-coordinate b. */
public Point ( int a, int b) {
x= a; y= b;
/** = x-coordinate */
public int getX() { return x; }
/** = y-coordinate */
public int getY() { return y; }
(b) Draw a folder (object) of class AsianElephant. Do not include the partition for class Object.
(c) Each of the five cases below consists of a statement followed by an expression. Write the value of the
expression after the statement is executed —if you think that execution would lead to an error, then ex-
plain the error. Remember, it often helps to draw objects and variables.
(1) Elephant e= new AsianElephant("Elephas", 10000, 120);
"Elephant".equals(e.AnimalType())
(2) Animal c= new Elephant("Maximus", 9000, 100);
c.AnimalType()
(3) AsianElephant b= (AsianElephant)( new Elephant("Indicus", 8000, 90));
b.AnimalType()
(4) Animal f= (Animal)( new Elephant("Loxodonta", 12000, 130));
f.getHeight()
Question 4. (20 points).
(a) Consider the statement shown below. Draw the frame for the call on static function isIn, where isIn is
defined in class Animal on page 3. You do not have to assign argument values to parameters, and you do
not have to execute the method body. We want to see only what the frame for the call looks like after it
has been created.
Boolean b= Animal.isIn( null , new Vector
(b) Write an instance method equals(Object obj) for class Elephant in Question 3. To help you out, here is
the beginning of the class definition for Elephant, showing its one field and method equals, whose body
you have to write.
public class Elephant extends Animal {
private int ht; // elephant height in inches
/** = "obj is an Elephant with the same values in its fields as this Elephant" */
public boolean equals(Object obj) {
}
}