Integer Object - Data Structures - Solved Exams, Exams of Data Structures and Algorithms

Main points of this past exam are: Integer Object, Class Intervalenumeration, Interval Represents, Defines Nonempty, Class Defines, Consecutive Integers, Singly Linked, Linked List, Values Stored, Integer Objects

Typology: Exams

2012/2013

Uploaded on 04/02/2013

shashi_16star
shashi_16star 🇮🇳

4.6

(20)

99 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 61B (Clancy, Yelick) Solutions and grading standards for exam 2
Spring 2001
1
Exam information
345 students took the exam. Scores ranged from 3 to 25, with a median of 19 and an
average of 18.1. There were 176 scores between 19 and 25, 125 between 12.5 and
18.5, 42 between 6 and 12, and 2 less than 6. (Were you to receive 75% of the points
on all your exams, plus good grades on homework and lab, you would receive an A–;
similarly, a test grade of 50% may be projected to a B–.)
There were two versions of the exam, A, and B. (The version indicator appears at the
bottom of the first page.) Versions were essentially identical except for small changes
in some of the problems.
If you think we made a mistake in grading your exam, describe the mistake in writ-
ing and hand the description with the exam to your lab t.a. or to Mike Clancy. We
will regrade the entire exam.
Solutions and grading standards for versions A and B
Problem 0 (1 point)
You lost 1 point on this problem if you did any of the following:
you earned some credit on a problem and did not put your name on the page,
you did not indicate your lab section or t.a. (or gave conflicting information), or
you failed to put the names of your neighbors on the exam.
The reason for this apparent harshness is that exams can get misplaced or come
unstapled, and we would like to make sure that every page is identifiable. We also
need to know where you will expect to get your exam returned. Finally, we occasion-
ally need to know where students were sitting in the class room while the exam was
being administered.
Problem 1 (4 points)
This problem, based on lab assignments 5 and 6 and project 1, was identical on the
two versions. It asked you to fill in the code for an enumeration of integers in an
Interval
object. Enumerations you saw in lab had the following components:
an instance variable that refers to the next item to return;
a constructor that initializes the instance variable;
•a
hasMoreElements
method that checks the value of the instance variable to make
sure it refers to a legal element of the collection being enumerated;
•a
nextElement
method that saves the current item, updates the instance variable,
and then returns the item.
A corresponding instance variable in this problem would be something that indicates
the next value to return from the
Interval
object. You don’t need a reference, however;
you can store the
int
value from the interval directly. Here’s the code:
pf3
pf4
pf5

Partial preview of the text

Download Integer Object - Data Structures - Solved Exams and more Exams Data Structures and Algorithms in PDF only on Docsity!

Spring 2001

Exam information

345 students took the exam. Scores ranged from 3 to 25, with a median of 19 and an average of 18.1. There were 176 scores between 19 and 25, 125 between 12.5 and 18.5, 42 between 6 and 12, and 2 less than 6. (Were you to receive 75% of the points on all your exams, plus good grades on homework and lab, you would receive an A–; similarly, a test grade of 50% may be projected to a B–.)

There were two versions of the exam, A, and B. (The version indicator appears at the bottom of the first page.) Versions were essentially identical except for small changes in some of the problems.

If you think we made a mistake in grading your exam, describe the mistake in writ- ing and hand the description with the exam to your lab t.a. or to Mike Clancy. We will regrade the entire exam.

Solutions and grading standards for versions A and B

Problem 0 (1 point)

You lost 1 point on this problem if you did any of the following:

  • you earned some credit on a problem and did not put your name on the page,
  • you did not indicate your lab section or t.a. (or gave conflicting information), or
  • you failed to put the names of your neighbors on the exam.

The reason for this apparent harshness is that exams can get misplaced or come unstapled, and we would like to make sure that every page is identifiable. We also need to know where you will expect to get your exam returned. Finally, we occasion- ally need to know where students were sitting in the class room while the exam was being administered.

Problem 1 (4 points)

This problem, based on lab assignments 5 and 6 and project 1, was identical on the two versions. It asked you to fill in the code for an enumeration of integers in an Interval object. Enumerations you saw in lab had the following components:

  • an instance variable that refers to the next item to return;
  • a constructor that initializes the instance variable;
  • a hasMoreElements method that checks the value of the instance variable to make sure it refers to a legal element of the collection being enumerated;
  • a nextElement method that saves the current item, updates the instance variable, and then returns the item.

A corresponding instance variable in this problem would be something that indicates the next value to return from the Interval object. You don’t need a reference, however; you can store the int value from the interval directly. Here’s the code:

Spring 2001

public IntervalEnumeration ( ) { nextInt = myLow; } public boolean hasMoreElements ( ) { return nextInt <= myHigh; } public Object nextElement ( ) { if (!hasMoreElements ( )) { throw new NoSuchElementException ("interval ran out"); } nextInt++; return new Integer (nextInt - 1); } private int nextInt;

Note, incidentally, that IntervalEnumeration is declared inside Interval, so its methods are allowed to access myLow and myHigh.

Another approach was to use a Vector or LinkedList object to keep track of the state of the enumeration. Initially, all the integers in the interval are stored in the vector. If the vector is nonempty, its first element is the next item to return; this item is then removed in the nextElement method. Here is code:

public IntervalEnumeration ( ) { v = new Vector ( ); for (int k=myLow; k<=myHigh; k++) { v.addElement (new Integer (k)); } } public boolean hasMoreElements ( ) { return v.size ( ) > 0; } public Object nextElement ( ) { if (!hasMoreElements ( )) { throw new NoSuchElementException ("interval ran out"); } Object x = v.elementAt (0); v.removeElementAt (0); return x; } Vector v;

This approach can be simplified by relying on Vector’s own enumeration method:

public IntervalEnumeration ( ) { v = new Vector ( ); for (int k=myLow; k<=myHigh; k++) { v.addElement (new Integer (k)); } Enumeration enum = v.elements ( ); }

Spring 2001

Grading (out of 5 points) was as follows. You lost all 5 points for any assignment to a myItem variable, as promised in the problem statement. In other solutions, 2 points were awarded for throwing an exception correctly for a 1-element list, and 3 points were earned for correctly modifying the three reference variables (myFirst, myFirst.myNext, and myFirst.myNext.myNext). Returning rather than throwing an exception lost you both exception points; you lost 1 point for a minor error in throw- ing the exception, for example, missing new, wrong condition, or missing parentheses in the call to the exception constructor. You lost 1 point for each missing reference update, 1 point for each minor error (e.g. wrong instance variable names, private local variables, etc.

Errors in linking were common: losing nodes, creating cycles, etc.. You needed to use at least one temporary variable in a correct solution.

A common error was to assume that the list had a size variable or method; this lost 1 point. Other students assumed that the list contained a sentinel node; nothing in the problem specifies this, however. (This error may have resulted in a solution that was substantially different from what we expected, and thus was misevaluated. If the assumption of a sentinel was the only error you made on this problem, you should receive 4 out of 5 points.) Another common error was to omit necessary references to myFirst, for example by using myNext consistently to mean myFirst.myNext. Many students also seemed not to realize that List and ListNode are two separate classes.

Problem 3 (4 points)

This problem involved analyzing code similar to that in lab assignment 7. Note that BetterInterval.equals does not override Object.equals because of the different param- eter type, so BetterInterval.equals will only be called when the compiler can deter- mine that two BetterInterval objects are involved in the comparison. Version A’s answers were the following:

In version B, the first two sets of statements were switched and the last two sets of statements were switched from corresponding code in version A. Thus the version B answers are false, true, true, false.

1 point was awarded per correct answer. No partial credit was awarded.

expression result + explanation b = intvl1.equals (intvl2); true; since both intvl1 and intvl2 are declared as BetterIntervals, BetterInterval.equals is used. b = v1.equals (v2); false; Vector comparisons use Object.equals to compare vector elements unless Object.equals is overridden. b = intvl3.equals (intvl4); false; the apparent type of intvl4 is Object, so the only match is Object.equals. b = ((BetterInterval) intvl3).equals ((BetterInterval) intvl4);

true; the apparent type of the recast intvl3 and intvl4 is BetterInterval, so BetterInterval.equals is used.

Spring 2001

Problem 4 (7 points)

Version A was identical to version B except that the order of the columns were reversed for all the choices. Here are solutions. Recall that Θ(1) means constant time.

Sorted doubly linked list implementation:

Vector implementation:

The answer to part c, identifying the total running time of the add method in the Vec- tor implementation, is Θ(n). This is Θ(log n) for finding k’s position + Θ(n) for insert- ing k. Θ(log n) is dominated by Θ(n), and thus disappears from the Θ estimate.

operation running time + explanation finding k or k’s proper position in the list (for insertion)

Θ(n); linear search is necessary

then inserting k into the list if it’s not already there

Θ(1); doubly linked lists allow in-place insertion or deletion then updating the median if necessary

Θ(1); there are four cases, depending on whether there are an odd or even number of values in the list and on whether k was inserted before or after the median locating the median (for deletion) Θ(1); one of the instance variables is keeping track of the median then removing it from the linked list, Θ(1); doubly linked lists allow in-place insertion or deletion then updating the median if necessary

Θ(1); the median moves to what was either its left or right neighbor depending on the number of values in the list

operation running time + explanation finding k or k’s proper position in the vector (for insertion)

Θ(log n); binary search is used

then inserting k into the vector if it’s not already there

Θ(n); insertion at the start of the vector requires shifting all the subsequent elements one position then updating the median if necessary

Θ(1); the median is always the element at position (myElements.size( )–1)/2, and Vector.insertElementAt has already incremented the size in the preceding step locating the median (for deletion) Θ(1); the median is at position (myElements.size( )–1)/ then removing it from the vector, Θ(n); subsequent elements must be shifted back then updating the median if necessary

Θ(1); Vector.removeElementAt decrements the size, from which the position of the median may immediately be computed