Docsity

Exam Solutions for COS 126 - Spring 2006 - Computer Science, Exams of Computer Science

Toggle sidebar
bg1
COS 126 General Computer Science Spring 2006
Exam 2 Solutions
1. Deconstructing Hello, World.
G C B A E F
2. Strings, references and debugging.
Elis
Tigers
Tigers
Big Red
In the constructor, the local variable name shadows the instance variable name, so the instance
variable is always initialized to "Elis".
The setName() method works as intended.
Changing what string xpoints to will not change team.
In Java, strings are immutable, so x.replaceAll() has no effect on x. To change x, you
would use x = x.replaceAll("Red", "Green"); .
3. Data types.
(a)
public class Point {
private double x, y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public double distanceTo(Point b) {
double dx = b.x - x;
double dy = b.y - y;
return Math.sqrt(dx*dx + dy*dy);
}
public String toString() {
return "(" + x + ", " + y + ")";
}
}
1
docsity.com
pf3
pf4

Partial preview of the text

Download Exam Solutions for COS 126 - Spring 2006 - Computer Science and more Exams Computer Science in PDF only on Docsity!

COS 126 General Computer Science Spring 2006

Exam 2 Solutions

  1. Deconstructing Hello, World.

G C B A E F

  1. Strings, references and debugging.

Elis Tigers Tigers Big Red

In the constructor, the local variable name shadows the instance variable name, so the instance variable is always initialized to "Elis". The setName() method works as intended. Changing what string x points to will not change team. In Java, strings are immutable, so x.replaceAll() has no effect on x. To change x, you would use x = x.replaceAll("Red", "Green");.

  1. Data types.

(a)

public class Point { private double x, y;

public Point(double x, double y) { this.x = x; this.y = y; }

public double distanceTo(Point b) { double dx = b.x - x; double dy = b.y - y; return Math.sqrt(dxdx + dydy); }

public String toString() { return "(" + x + ", " + y + ")"; } }

(b)

// find furthest point from a Point furthest = a; while (!StdIn.isEmpty()) { double bx = StdIn.readDouble(); double by = StdIn.readDouble(); Point b = new Point(bx, by); if (a.distanceTo(b) > a.distanceTo(furthest)) furthest = b; }

A common optimization is to store the furthest distance encountered so far in a separate variable to avoid recomputing each time.

  1. Encapsulation.
    1. The data type didn’t use the access modifier private with the instance variable temp[], so the client can directly manipulate it.

gauge.temp[0] = -1.0;

  1. Arrays are mutable, so changing the array in the client will change the instance variable. To protect against such access, the method setTemp() should store a copy of the array.

gauge.setTemp(t); t[0] = -1.0;

  1. Arrays are mutable, so changing the array in the client will change the instance variable. To protect against such access, the method setTemp() should return a copy of the array.

t = gauge.getTemp(); t[0] = -1.0;

  1. The client changes the instance variable N, thereby defeating the check for negative values in isConsistent(). This defect is created because instance variable N was not declared private and because the constant 86,400 is hardwired into isConsistent() (which uses this constant instead of N).

gauge.N = 0; t[0] = -1; gauge.setTemp(t); gauge.N = 86400;

  1. Intractability, Universality, and Computabaility.

F The undecidability of the halting problem is a statement about Turing machines: it is not applicable to real computers.

T The Church-Turing thesis is a theory about our universe: it cannot be proven mathe- matically, but it can be falsified.

T The Turing machine is a universal model of computation: with a Turing machine we can solve any decision problem that can be solved with a DFA or with a Pentium M running Linux 2.6.16.

T P is the class of yes-no problems for which a polynomial-time Java program could, in principle, be written.

F NP is the class of yes-no problems for which no polynomial time Java program could ever be written.

T NP is the class of yes-no problems for which, in principle, you could write a Java program to check a given proposed solution in polynomial-time.

F If you discover a polynomial-time algorithm for any problem in NP, then all problems in NP are solvable in polynomial-time.

F Most Princeton computer science faculty believe that P = NP.

T Since Factor is a problem in NP, any instance of Factor can be restated as an instance of 3-Sat (3-satisfiability).

  1. Circuits.

(a) x 2 x 1 x 0 f 0 0 0 0 0 0 1 0 0 1 0 0 0 1 1 0 1 0 0 0 1 0 1 1 1 1 0 1 1 1 1 1 (b) f = x 2 x′ 1 x 0 + x 2 x 1 x′ 0 + x 2 x 1 x 0. (c) Observe that f = x 2 (x 1 + x 0 ) and draw the resulting circuit.