CSC148H: Introduction to Computer Science
Department of Computer Science, University of Toronto

St. George Campus, Summer 2000


L0101 Midterm Solution

Question 1 [15 marks]

Part (a) [2 marks]

Marking Scheme

A. -1 if a student number is missing on one page
B. -2 if a student number is missing on more than one page

Part (b) [6 marks]

Solution

(in order) False, False, True

Marking Scheme 2 marks for each correct answer (no part marks)
Common Errors and Comments Mostly well done; the last question was missed more often than the
other two.

Part (c) [3 marks]

Solution

Data and operations.

Marking Scheme

A. 1 for "data"
B. 2 for "operations"
C. -1 for each wrong statement

Common Errors and Comments

If you said "data type", that would make the definition circular!

If you mentioned anything that implied implementation details (e.g. instance variables or methods), you did not get full marks. Remember: An abstract data type does not need a corresponding (concrete) Java class or interface.

Part (d) [4 marks]

Solution

class NewException extends Exception {}

Marking Scheme

A. 2 for extends Exception
B. 2 for class NewException ... {}
C. -1 for each syntax error

Common Errors and Comments

Generally well done. Some students misinterpreted the question to
mean, "How would one throw/catch an exception?", which was unfortunate.

Question 2 [20 marks]

Part (a) [5 marks]

Solution

items stores the Strings currently in this list, at positions 0 to n-1, in non-decreasing sorted order, where n is the size of the Vector (returned by calling method size()).

Marking Scheme A. 1 for "items stores the Strings currently in this list"
B. 1 for "in non-decreasing sorted order"
C. 1 for "at positions 0 to n-1"
D. 1 for "n is the size of the Vector"
E. 1 for having nothing else (e.g. method specifications, code)
Common Errors and Comments Not many people got C or D. A few people did not understand entirely what a representation invariant is. Remember that a representation invariant should describe the purpose and state of all of the instance variables when no methods are executing, at any point during the execution of the program. It should not mention anything else.

Part (b) [15 marks]

Solution
/**
 * Inserts the String data into the linked list.
 * @param  data  the String to be inserted into the list.
 * Pre: data != null
 * Post: the linked list is sorted after the insertion
 */
public void insert(String data) {
    Node previous = null;
    Node current = head;
    while (current != null 
            && current.data.compareTo(data) < 0) {
        previous = current;
        current = current.link;
    }
    
    Node inserted = new Node(data);
    if (previous == null) {
        inserted.link = head;
        head = inserted;
    }
    else {
        previous.link = inserted;
        inserted.link = current;
    }
}
Marking Scheme A. 1 for the precondition
B. 1 for the postcondition (described formally or informally)
C. 1 for mention the parameter
D. -1 for mentioning implementation details
E. 1 for using a previous pointer
F. 3 for using a current pointer and looping through the list
G. 3 for stopping the loop when current.data.compareTo(data) >= 0
H. 1 for creating the new node for 'data'
I. 1 for handling the case where the node must be inserted at the beginning
J. 2 for handling the case where the node is inserted anywhere else
K. 1 for providing good comments, or making the code clear enough that I could understand how it would work
Common Errors and Comments

Many students made lots of small mistakes.

A very common error was to address I at the start of the method. Note that you may still have to insert the new node at the beginning even after traversing the list to look for the place to insert the new node.

Question 3 [20 marks]

Part (a) [10 marks]

Solution
6 4
5 19
0 0
Marking Scheme A. 1 for having 6 numbers, printed two per line
B. 4 for having the first line correct
C. 2 for having the second line logically follow from the second
D. 3 for having the third line logically follow from the first and third
Common Errors and Comments A very common error was to begin with the line 15 30. Of course, this is incorrect because you are supposed to use the stuff() method inside Jetta and PassatWagon.

Another common error was to write 4 4 as the last line. This
is wrong because Jetta.stuff() is called first. The Jetta has 4
passengers to begin, and so after stuff(4) is called, it still has 4
passengers. Then unstuff(4) is called, reducing the passenger load to 0.

Note that part-marks were assigned. I tried to mark your answers so that even if you were wrong on the first answer, you could still get part-marks if the second and third lines logically followed from the first.

A few students told me after the midterm that they spent a lot of time on this question. Many of those students spent time drawing out the entire memory model. This question did not ask you to draw the memory model; it just asked you to trace the program. So, if you can trace the program without drawing out the memory model, then avoid the extra work! Only bring in the memory model when you get into trouble.

Part (b) [10 marks]

Solution
----------------------------
transfer: 1             0001

    c 0001
    n 4
----------------------------
main: 13              Driver

    c1 0001
    c2 0001
    c3 0001
----------------------------
Marking Scheme

A. -1 for extra frames
B. -1 for not having main
C. -1 for not having the correct line number for main (it had to be close to 13)
D. -1 for not having Driver as the class on which main() is called
E. -1 for not having the correct value for c1
F. -1 for not having the correct value for c2
G. -1 for not having the correct value for c3
H. -1 for not having transfer
I. -1 for not having the correct value for the address of the object on which transfer() is called
J. -1 for not having the correct value for c
K. -1 for not having the correct value for n

Common Errors and Comments

Most students made many small mistakes.

A few students told me after the midterm that they did not have time for this question. Many of those students did this question last. I am sure that if they had done this question first, they would have picked up many of the 10 marks, because this question was meant to be easy. All you had to do was look at the last four statements and you would see that the same object address was used for all the references involved (half the marks for this question!).

Moral of the story: Read through the entire midterm first, and then start with the questions with which you are comfortable.


Last updated on 2000-07-29 by Ray Ortigas.