Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

CS61B Midterm 1 Spring 2005: Problem Set Solutions, Exams of Data Structures and Algorithms

Solutions to problem 1 and problem 2 from the cs61b midterm 1 exam held in spring 2005. The problems cover topics such as instance variables, immutability, static methods, inheritance, and method overriding.

Typology: Exams

2012/2013

Uploaded on 04/02/2013

shashidhar_p43
shashidhar_p43 🇮🇳

4.5

(53)

80 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS61B Midterm 1 Spring 2005 Professor Shewchuk Name_________________
Problem 1. (4 points) Quickies
a. (1 point) Why is it usually better to declare an instance variable protected than to declare it
private?
b. (1 point) Are Circle objects (defined in full below) immutable? Why?
public class Circle{
private double radius;
public Circle(double r){
radius = r;
}
public double area(){
final double PI = 3.1415926535897;
return (2.0 * PI * radius * radius);
}
}
c. (1 point) In a static method, you can never use the keyword ______________ .
d. (1 point) In the following list, circle the types of statements that Java’s continue statement can
potentially jump back to the beginning of.
for switch if while class do case
pf3
pf4

Partial preview of the text

Download CS61B Midterm 1 Spring 2005: Problem Set Solutions and more Exams Data Structures and Algorithms in PDF only on Docsity!

CS61B Midterm 1 Spring 2005 Professor Shewchuk Name_________________

Problem 1. (4 points) Quickies a. (1 point) Why is it usually better to declare an instance variable protected than to declare it private?

b. (1 point) Are Circle objects (defined in full below) immutable? Why?

public class Circle{ private double radius;

public Circle(double r){ radius = r; }

public double area(){ final double PI = 3.1415926535897; return (2.0 * PI * radius * radius); } }

c. (1 point) In a static method, you can never use the keyword ______________.

d. (1 point) In the following list, circle the types of statements that Java’s continue statement can potentially jump back to the beginning of.

for switch if while class do case

Problem 2. (8 points) Inheritance.

public class A{ public A(){ System.out.println(“New A”); }

public A(int x) {}

public void method1() { System.out.println(“M1 in A”); }

public void method2(){ System.out.println(“M2 in “ + whichClass()); }

public String whichClass() { return “A”; } }

public interface B { public void method2(); }

public class C extends A implements B{ public C(){ System.out.println(“New C”); }

public C(int x){ super(x); }

public void method1() { System.out.println(“M1 in C”); }

public void method3() { super.method1(); }

public String whichClass() { return “C”; } }

What does each of the following code fragments print? If a code fragment causes an error, say whether it is a compile-time error or a run-time error. If a code fragment prints nothing and causes no error, say so.

a. A a = new C(3); a.method1();


b. B b = new C(5); b.method2();


c. C c = (C) new A(4); c.method1();


d. B b = new C(8); System.out.println(b.whichClass());


e. B b = new C(); ( (A) b).method1();


f. (new C(5) ).method3();


g. B b = new B(); b.method2();


Problem 4. (7 points) Reordering a Singly-Linked List.

Write a method called reorder in the SList class below. reorder changes the order of nodes in a singly- linked list. Upon completion, the nodes in the list appear in the following order: the former first node, the former third node, the former fifth node, followed by the remaining former odd-numbered nodes. Then comes the former second node, the former fourth node, and so on. For example, [ This is a list of coherent Strings ] becomes [ This a of Strings is list coherent ]. Your solution should manipulate next pointers directly. Do not call any method of SList provided in this course. Do not change any item references of create any new nodes.

public class SListNode{ public Object item; public SListNode next; }

public class SList{ private SListNode head; private int size;

public void reorder(){

} }