DOC PREVIEW
UMD CMSC 433 - Final Exam

This preview shows page 1-2-3-4 out of 13 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Name:FinalCMSC 433Programming Language Technologies and ParadigmsSpring 2005Final ExamInstructionsThis exam contains 13 pages, including this one. Make sure you have all the pages. Writeyour name on the top of this page before starting the exam.Write your answers on the exam sheets. If you finish at least 15 minutes early, bring your exam to thefront when you are finished; otherwise, wait until the end of the exam to turn it in. Please be as quiet aspossible.If you have a question, raise your hand. If you feel an exam question assumes something that is notwritten, write it down on your exam sheet. Barring some unforeseen error on the exam, however, youshouldn’t need to do this at all, so be careful when making assumptions.You may avail yourself of the punt rule. If you write down punt for any numbered or lettered part of aquestion, you will earn 1/5 of the points for that question (rounded down).Question Score Max1 202 123 134 205 256 10Total 1001Question 1. Short A nswer (20 points).a. (4 points) What is the difference between a regular inner class and a static inner class?Answer: An instance of a regular inner class has an implicit reference to an instance of theenclosing, outer class. This reference is used whenever the inner class refers to a field or methodof the outer class. A static inner class, on the other hand, has no such reference, and hence itmay not implicitly use fields and methods of the outer class.b. ( 4 points) Explain the difference between o.notify() and o.notifyAll(), and explain why in classwe said that o.notifyAll() should almost always be used instead of o.notify().Answer: Invoking o.notifyAll() wakes up every thread waiting on o. In contrast, invokingo.notify() wakes up exactly one thread. If that thread happens to be the wrong one (becausethe condition it is waiting for is not met), and if that thread goes back to sleep without itselfnotifying other waiting threads, the program may deadlo ck. Thus it is much trickier to usenotify() correctly, and notifyAll() is usually the right one to pick.c. (4 points) Below are four design features of Projects 5 and 6. For each feature, list which one of thefollowing design patterns it corresponds to: Singleton, Proxy, Command, Bridge, Observer. (Each patternmay be used 0, 1, or more times.)• Whiteboards attach and detach themselves to the RemoteBoard to listen for updates.• The Swing GUI provides an interface to the underlying GUI of the host platform.• TimedObject wraps an object o to intercept calls to o’s methods (so that calls to them time out afterms milliseconds).• Programs use RMI stubs to invoke a method remotely on another machine.Answer: In order, Observer, Bridge (or Proxy), Proxy, Proxy.2d. (4 points) In class we s aid that JUnit was created partially to aid refactoring. Explain how auto-mated testing is useful for refactoring. (In your explanation, you should also explain the difference betweenrefactoring and arbitrary code changes.)Answer: A refactoring is a change that preserves the behavior of the program. Typicallythe programmer will apply many (small) refactorings to transform his or her program, and theywould like to ensure that none of the refactorings alters the program’s behavior. Hence automatedtesting makes it easy the programmer to rerun all tests between small refactorings and make surethat nothing has changed.e. (4 points) Explain what pre- and post-conditions are. In Java, methods list checked exceptions thatthey may throw. Are these exceptions conceptually part of the precondition or the postcondition?Answer: A precondition on a method m is a s pec ification of what must be true before when m iscalled. A postcondition of a method m states what is true after the function has been executed,assuming that the precondition was met. Conceptually, exceptions are part of the postconditionof a method, since they describe the result of running the method.3Question 2. Double Dispatch, Again (12 points). The following program uses instanceof to simulatedynamically dispatching on multiple arguments. On the next page, rewrite A, B, and the body of Main touse the double dispatch of the Visitor pattern to invoke the doXX methods of class Do, following the samelogic as below. We’ve provided you the new version of the Thing interface and part of Main to get started.public interface Thing {}public class A implements Thing {}public class B implements Thing {}public class Do {public static void doAA(A left, A right) { ... }public static void doAB(A left, B right) { ... }public static void doBA(B left, A right) { ... }public static void doBB(B left, B right) { ... }}public class Main throws Exception {public static void main(String[] args) {Thing t0 = (Thing) Class.forname(args[0]).newInstance();Thing t1 = (Thing) Class.forname(args[1]).newInstance();if ((t0 instanceof A) && (t1 instanceof A))Do.doAA(t0, t1);else if ((t0 instanceof A) && (t1 instanceof B))Do.doAB(t0, t1);else if ((t0 instanceof B) && (t1 instanceof A))Do.doBA(t0, t1);else if ((t0 instanceof B) && (t1 instanceof B))Do.doBB(t0, t1);}}4public interface Thing {// Your implementations of doIt() should call the appropriate Do.doXX methodvoid doIt(A a);void doIt(B b);void accept(Thing t);}public class Main throws Exception {public static void main(String[] args) {Thing t0 = (Thing) Class.forname(args[0]).newInstance();Thing t1 = (Thing) Class.forname(args[1]).newInstance();5Question 3. Multi-threading (13 points). Construct a Java program that may deadlock. Your programshould be a complete, runnable Java program with a main() method that, when invoked, shows the deadlock.Also, write a short explanation of the sequence of events leading to the deadlock.Answer: There are many possible answers to this question. Here is one program.public class Foo {public Object lock1 = new Object();public Object lock2 = new Object();public class Thread1 extends Thread {public void run() {synchronized (lock1) {synchronized (lock2) {System.out.println("Got both locks.");}}}}public class Thread2 extends Thread {public void run() {synchronized (lock2) {synchronized (lock1) {System.out.println("Got both locks.");}}}}public static void main(String[] args) {(new Thread1()).start();(new Thread2()).start();}}This program may deadlock if Thread1 acquires lock1, and then Thread2 acquires lock2. In thiscase, neither thread can proceed, because each is waiting for the other’s lock.6Question 4. Locking (20 points). In Java 1.4, synchronization must start and end


View Full Document

UMD CMSC 433 - Final Exam

Documents in this Course
Trace 1

Trace 1

62 pages

Reflection

Reflection

137 pages

Testing

Testing

25 pages

Paradigms

Paradigms

10 pages

Testing

Testing

17 pages

Java RMI

Java RMI

17 pages

Java RMI

Java RMI

17 pages

Java RMI

Java RMI

17 pages

Trace 1

Trace 1

46 pages

Jini

Jini

4 pages

Final

Final

15 pages

Java RMI

Java RMI

13 pages

Testing

Testing

16 pages

Load more
Download Final Exam
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Final Exam and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Final Exam 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?