Mid2 Princeton
Mid2 Princeton
Exam 2
This test has 11 questions worth a total of 50 points. You have 120 minutes. The exam is closed
book, except that you are allowed to use a one page cheatsheet, handwritten by you on both sides.
No calculators or other electronic devices are permitted. Give your answers and show your work
in the space provided. Partial credit will be given for partially correct answers. Write out and
sign the Honor Code pledge before turning in the test.
“I pledge my honor that I have not violated the Honor Code during this examination.”
—————————————————————–
Signature
1
2 PRINCETON UNIVERSITY
(a) Write your name and Princeton NetID in the space provided on the front of the exam,
and circle the name of the preceptor who grades your homework assignments.
(b) Write and sign the honor code on the front of the exam.
For each code fragment on the left, find the best matching description on the right.
H. Has no meaning.
COS 126 EXAM 2, FALL 2006 3
In other words, the String will be quoted, so args[0] will contain the whole thing. Flll in
the spaces below with your code (we’ve begun isPunc for you):
}
}
4 PRINCETON UNIVERSITY
4. Circuits (4 points)
Now design a logic circuit that will detect binary palindromes exactly 9 bits wide. (For
example, 000101000 and 101000101 are 9-bit palindromes, but 111100111 and 010101011 are
not.) The inputs to your circuit are the 9 bits. The 1-bit output should be 1 if the input is a
palindrome, and 0 if it is not. Use any number of the following parts in any combination:
You may represent your gates in any way you like. Boxes with labels will be just fine.
Hint: Of course you could build a 512-line truth table and design an absolutely gigantic
circuit, but there are much simpler solutions. Also, unlike what you did in the last two
questions, here you’ll be checking all of the bits simultaneously.
Once again, while any correct solution will receive full credit, this can be done with just six
gates from the above list (and a few wires).
6 PRINCETON UNIVERSITY
. . . methods go here . . .
In the space below, implement the method public void reverse() of DoublyLinkedList
that reverses the order of the entire list. Your implementation should iterate through the
entire list no more than once.
COS 126 EXAM 2, FALL 2006 7
private String [] a;
private int N; // the size of the ArrayQueue array
private int back; // where to put the next String to be enqueued
private int front; // where to get the next String to be dequeued
}
}
8 PRINCETON UNIVERSITY
7. True/False. (6 points)
Write T for true or F for false next to each of the following statements, according to their
veracity.
−−− A recursive function can always be converted into an equivalent non-recursive one.
−−− A while loop can always be converted into an equivalent for loop, and vice versa.
−−− The regular expression a*b* represents the set of all strings consisting of a’s and b’s.
−−− The undecidability of the halting problem is a statement about Turing machines; it is
not applicable to real computers.
−−− P is the class of search problems for which a polynomial-time Java program could, in
principle, be written.
−−− NP is the class of search problems for which no polynomial time Java program could
ever be written.
−−− NP is the class of search problems for which, in principle, a Java program could be
written to check a given proposed solution in polynomial time.
−−− If you discover a polynomial-time algorithm for any problem in NP, then all problems
in NP are solvable in polynomial-time.
−−− Since Factor is a problem in NP, any instance of Factor can be restated as an instance
of 3-Sat (3-satisfiability).
COS 126 EXAM 2, FALL 2006 9
Do NOT implement this data type! Instead, making use of the Interval API, and using the
space below, write a client that takes an int value N as a command–line argument, reads N
intervals (each represented by a pair of double values) from standard input, and prints all
Intervals that intersect. Any clear output format is fine.
10 PRINCETON UNIVERSITY
9. TigerSoft (6 points) It is the year 2019. After graduating from Princeton, you went to
Harvard Business School and got your MBA degree. You then went to work for the TigerSoft
software company, and due to your talent and ambition (not to mention your superior educa-
tion), you are now Chief Software Architect at the company. You provide technical leadership
and vision to a large team of programmers and also advise senior management on technical
challenges and opportunities.
All is not well at TigerSoft. Your team of programmers turns out great quantities of software,
but the programs are typically full of subtle bugs, resulting in unhappy customers, loss of
revenue, and a decline in the value of your stock options. The Chief Executive Officer of
TigerSoft, also a Princeton alum (but who somehow never took COS 126) is very upset and
seeks your advice. The CEO would like you to institute a Quality Assurance effort along the
following lines.
For each piece of software the responsible programmer will supply a list of some (but, of course,
not all) possible inputs and a matching list of the correct outputs, which the programmer
will figure out by hand. The CEO would like you to write a special program, to be called the
Verifier, which will automatically check that each new piece of software behaves according to
its list of inputs and matching outputs. The Verifier will accept as its own input an arbitrary
function f() to be tested (a function that computes cosines, for example), one possible set
of input data for f() (an angle, in this example), and the correct output for that input
(the correct cosine, in this example). The Verifier will then check whether or not f() will
eventually produce the correct output given that input, and then report the result by printing
“Yes” or “No”. The Verifier can be used to check all the inputs on the programmer-supplied
list simply by running it over again for each new input/output combination.
How would you advise the TigerSoft CEO on this idea? Can such a Verifier be developed? If
so, what would be its basic structure? If not, why not?
The CEO, while clever, has a short attention span, so you’ll need to confine your answer to
one page (the following one).
COS 126 EXAM 2, FALL 2006 11
9. (continued)
MEMO