CS61B Midterm #1, Fall 1997: Problem Set, Exams of Data Structures and Algorithms

Cs61b midterm #1 problem set for professor k. Yelick's fall 1997 class. The problem set includes three problems related to java programming concepts such as method call, list manipulation, and stack sorting.

Typology: Exams

2012/2013

Uploaded on 04/02/2013

shashidhar_p43
shashidhar_p43 🇮🇳

4.5

(53)

72 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS61B, Fall 1997
Midterm #1
Professor K. Yelick
Problem #1
(2 points) What is y after the following code executes?
static void addOne (int x){
x += 1;
}
int y = 3;
addOne(y);
Answer:
Problem #2
(8 points) Answer questions about the following classes. For parts b-e, choose one of the following:
CE: The code will result in a compiler error from javac.
RT: The code will compile without errors, but will cause an error of some kind run time.
OK: The code will compile and run without errors. Show what the program will print.
abstract class A {
abstract public void foo ();
}
class B extends A {
public void foo () { System.out.println("Calling B.foo");}
protected int value = 0;
}
class C extends B {
public void foo () { System.out.println("Calling C.foo," +
value);}
}
class D extends C {
public void foo () { System.out.println("Calling D.foo()," +
value);}
}
a. (2 points)
A a1 = new A();
a1.foo();
CS61B, Fall 1997 Midterm #1 Professor K. Yelick 1
pf3
pf4

Partial preview of the text

Download CS61B Midterm #1, Fall 1997: Problem Set and more Exams Data Structures and Algorithms in PDF only on Docsity!

CS61B, Fall 1997

Midterm

Professor K. Yelick

Problem

(2 points) What is y after the following code executes?

static void addOne (int x){ x += 1; } int y = 3; addOne(y);

Answer:

Problem

(8 points) Answer questions about the following classes. For parts b-e, choose one of the following:

CE: The code will result in a compiler error from javac. RT: The code will compile without errors, but will cause an error of some kind run time. OK: The code will compile and run without errors. Show what the program will print.

abstract class A { abstract public void foo (); } class B extends A { public void foo () { System.out.println("Calling B.foo");} protected int value = 0; } class C extends B { public void foo () { System.out.println("Calling C.foo," + value);} } class D extends C { public void foo () { System.out.println("Calling D.foo()," + value);} }

a. (2 points)

A a1 = new A(); a1.foo();

CS61B, Fall 1997 Midterm #1 Professor K. Yelick 1

b. (2 points)

A a2 = new B(); a2.foo();

c. (2 points)

A a3 = new C(); a3.foo();

d. (2 points)

B b4 = new D(); ((C) b4).foo();

Problem

(12 points) Consider the following ListNode class definition.

class ListNode { int item; ListNode next; /** Postcondition: Constructs a new listnode containing i and n */ ListNode (int i, ListNode n) { item = i; next = n; } }

a. (4 Points) Complete the following code to copy a list.

/** Postcondition; returns a copy of l. (Copies all the nodes). */ private static ListNode copy(ListNode l) { if (l == null) return l; else { return (new ListNode (_____________ , _____________)); } }

b. (4 Points) Complete the following code to merge 2 sorted lists.

/** Precondition: ln1 and ln2 are sorted

  • Postcondition: returns a new sorted list with all the elements of ln1 and ln2, modifying ln1 and ln2 in the process. */

s2.push(tmp); } } s1.push(min); while (!s2.isEmpty()) { s1.push(s2.pop()); } count--; } }

/** * file : IntStack.java * desc : Implements the class Stack / public class IntStack { /* * post : constructs an empty stack / public IntStack() { max = 10; elems = new int [max]; top = 0; } /* * post : returns true <==> stack is empty / public boolean isEmpty() { return (top == 0); } /* * post : returns the number of elements in the stack / public int size() { return (top); } /* * pre : isEmpty() == false * post: removes and returns element at the top / public int pop() { return elems[--top]; } /* * post : put ‘ elem’ at the top / public void push(int elem) { checkSize(); elems[top++] = elem; } public String toString () { String result = “ [ “ ; for (int i = 0; i < top; i++) { result += elems[i] + “ “ ; } result += “ ]”; return result; } // private fields private int max; // Current capacity of stack private int top; // Current number of stack elements. private int[] elems; // Data in stack (elems[t-1] is top). /* * post : If the stack was full, the capcity is expanded * (doubled) */ private void checkSize() { if (top == max) { // double the capacity int newmax = max << 2; int[] newelems = new int [newmax]; // copy the data int i; for ( i = 0; i < max; i++ ) newlems[i] = elems[i]; // point to the new data max = newmax; elems = newelems; } } }

Posted by HKN (Electrical Engineering and Computer Science Honor Society)

University of California at Berkeley

If you have any questions about these online exams

please contact [email protected].