









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










Programming Language Technologies and Paradigms Spring 2004
May 19, 2004
This exam contains 15 pages, including this one. Make sure you have all the pages. Write your 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 the front when you are finished; otherwise, wait until the end of the exam to turn it in. Please be as quiet as possible. If you have a question, raise your hand. If you feel an exam question assumes something that is not written, write it down on your exam sheet. Barring some unforeseen error on the exam, however, you shouldn’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 a question, your will earn 1/5 of the points for that question (rounded down).
Question Score Max
Question 1. Short Answer (20 points).
a. (4 points) Explain the difference between method overriding and method overloading.
Answer: Overriding is when a subclass replaces code inherited from a superclass. Overloading is when a class has multiple methods with the same name and return type but different numbers and/or types of parameters.
b. (4 points) What does the following program print? Does it always print the same thing? (Hint: Yes, this is a trick question.) Explain your answer.
public class Foo extends Thread { public void run() { System.out.print("run "); } public static void main(String[] args) { Foo f = new Foo(); f.run(); System.out.print("main "); } }
Answer: This program always prints “run main.” This is a single-threaded program; the call to f.run() is just a normal method invocation. Separate threads are only started when their start() method is invoked, which we don’t do here.
c. (4 points) Explain one advantage and one disadvantage of delegation as compared to inheritance.
Answer: Delegation is more flexible than inheritance, because the inheritance hierarchy is fixed at compile time, whereas delegation relationships can change dynamically at run time. Delegation also allows for some reuse that’s not easy with inheritance, such as the kind of recursion in the decorator pattern. On the other hand, the dynamic reconfigurability of delegation can make programs harder to understand than inheritance, and it also increases the number of objects in the system, adding further complication. Delegation can also affect performance, since it increases the number of method calls.
Question 2. Multi-threading (10 points). For each of the following multi-threaded programs, state whether the program has a problem related to synchronization. If there is a problem, explain what the problem is, and give a sequence of actions that exhibits the problem.
a. (5 points) The following code implements the observer pattern. Assume that the attach(), detach(), and notify() methods may be called arbitrarily by other threads.
public interface Observer { void update(); } public class SynchronizedList { private LinkedList l = new LinkedList(); public synchronized void add(Object o) { l.add(o); } public synchronized void remove(Object o) { l.remove(o); } public synchronized int size() { return l.size(); } public synchronized Object get(int index) { return l.get(index); } }
public class Subject { private SynchronizedList observers = new SynchronizedList();
public void attach(Observer s) { observers.add(s); }
public void detach(Observer s) { observers.remove(s); }
public void notify() { for (int i = 0; i < observers.size(); i++) { Observer s = (Observer) o.get(i); s.update(); } } }
Answer: This program has a race condition. Each individual operation is synchronized, but that is not sufficient. In particular, the following sequence of events could happen. Suppose there is one observer that has been attached. (1) One thread calls notify(), (2) the check i < observers.size() succeeds, (3) Another thread calls detach(), which removes the observer from the list, (4) The first thread calls o.get(0), which fails.
b. (5 points) In the following example, assume that foo() and bar() may both be called arbitrarily by other threads.
public class Outer { private class Inner { private int counter = 0; public synchronized void incCounter() { counter++; } } private Inner myCount = new Inner();
public synchronized void foo() { System.out.println("Foo!"); myCount.incCounter(); }
public synchronized void bar() { System.out.println("Bar!"); myCount.incCounter(); } }
Answer: This program does not have any synchronization problems. The extra synchronization on the inner class object is redundant, but it doesn’t hurt anything: The inner class object lock is only acquired when the outer class lock is already held, so there’s no danger of deadlock.
Describe at least two different refactorings we could perform on the sample code, and explain why those refactorings are useful. Your refactorings should be different not just in what source code they operate on but what they actually do (e.g., don’t list three uses of Rename Variable). You may not use renaming of variables, methods, or fields by themselves as refactorings. You may use any reasonable refactoring, not just those we discussed in class. Don’t worry about getting the names exactly right. Do worry about describing the refactorings clearly and precisely.
Answer:
This page intentionally left blank.
public class ReaderWriterLock {
// More space to continue class ReaderWriterLock
public class SecureObject {
public Object createSecureObject(Object o, Permission p) {
Question 6. RMI (10 points). This question involves the following program, which uses RMI. We first give the shared interfaces followed by the Client code, which runs on the client machine, and the Server code, which runs on the server machine. The ... in the call to Naming.lookup() represents the appropriate URL.
public interface Start extends Remote { public void start(Ping a) throws RemoteException; } public interface Ping extends Serializable { public void ping(Pong b); } public interface Pong extends Serializable { public void pong(); }
public class Client implements Ping { public void ping(Pong b) { System.out.println("Ping!"); b.pong(); } }
public class ClientMain { public static void main(String[] args) { Start s = (Start) Naming.lookup(".../Server"); s.start(new Client()); } }
public class Server implements Pong { public void pong() { System.out.println("Pong!"); } }
public class ServerMain extends UnicastRemoteObject implements Start {
public void start(Ping a) { a.ping(new Server()); }
public static void main(String[] args) { ServerMain s = new ServerMain(); Registry r = LocateRegistry.createRegistry(1099); r.bind("Server", s); } }
What does this program print on the server machine? What does this program print on the client machine?
Answer: The program prints “Ping! Pong!” on the server machine. It prints nothing on the client machine.
If both Ping and Pong are changed to Remote interfaces (and the appropriate other changes are made), what does this program print on the client machine? What does this program print on the server machine?
Answer: The program prints “Pong!” on the server machine. It prints “Ping!” on the client machine.
If Ping remains Serializable and Pong is changed to a Remote interface (and the appropriate other changes are made), what does this program print on the client machine? What does this program print on the server machine?
Answer: The program prints “Ping! Pong!” on the server machine. It prints nothing on the client machine.