





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The guidelines and questions for the midterm exam of the cmsc 433 course, which focuses on programming language technologies and paradigms. The exam includes short answer questions on design principles, synchronization in java, information hiding, and the bridge and visitor patterns. There are also longer questions on the bridge pattern and the visitor pattern in the context of a flight attendant simulator and a java concurrency program.
Typology: Exams
1 / 9
This page cannot be seen from the preview
Don't miss anything!






Guidelines
Put your name on each page before starting the exam. Write your answers directly on the exam sheets, using the back of the page as necessary. If you finish with more than 15 minutes left in the class, then bring your exam to the front when you are finished and leave the class as quietly as possible. Otherwise, please stay in your seat until the end. If you have a question, raise your hand and I will come to you. Note, that I am unlikely to answer general questions however. 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. NOTE: There are quite a few questions to answer. Get started right away and budget your time wisely.
Question Points Score
1 15
2 10
3 30
4 25
5 20
Total 100
(a) One key principle of design patterns is to ”program to an interface, not to an implementation.” What does that mean? Give two reasons why this is beneficial.
Answer: Write code that relies on supertype APIs. Possible benefits: (1) Can change im- plementation, (2) Enables reuse / creation of resuable components. Other answers accepted.
(b) Synchronization in Java serves three functions: visibility, ordering, atomicity. Briefly explain each function.
Answer: i. Atomicity. Allows operation to be done without interference from other threads. ii. Visibility. Signals when values will be updated across threads iii. Ordering. Allows programmers to ensure that an operation A occurs before oper- ation B
(c) Information hiding refers to a criteria for modularizing software systems. What is this criteria?
Answer: Modules should encapsulate things that are likely to change
public class VisitorDemo { public static Passenger[] list = { new First(), new Business(), new Economy() };
public static void main(String[] args) { Visitor sit = new HelpSeat(), eat = new Feed(); for (int i = 0; i < list.length; i++) list[i].accept(sit); for (int i = 0; i < list.length; i++) list[i].accept(eat); } }
It should print out the text below:
This way your grand exalted Highness to First Class Follow me Boss to Business Economy is that way, pal Another glass of Champagne? May I pour you an after dinner cordial? You want more water? That will be $
Using the following interfaces, implement the First, Business, Economy, HelpSeat and Feed classes needed by the code above.
public interface Visitor { public void visit(First e); public void visit(Business e); public void visit(Economy e); }
public interface Passenger { public void accept( Visitor v ); }
Answer:
class First implements Passenger { public void accept( Visitor v ) { v.visit( this ); } } class Business implements Passenger { public void accept(Visitor v) { v.visit(this); } } class Economy implements Passenger { public void accept(Visitor v) { v.visit(this); } } public class HelpSeat implements Visitor { public void visit(First e) { System.out.println(‘‘This way your grand exalted Highness to First Class’’); } public void visit(Business e) { System.out.println(‘‘Follow me Boss to Business’’); } public void visit(Economy e) { System.out.println(‘‘Economy is that way, pal’’); } } class Feed implements Visitor { public void visit( First e ) { System.out.println( ‘‘Another glass of Champagne?’’); } public void visit( Business e ) { System.out.println( ‘‘May I pour you an after dinner cordial?’’); } public void visit( Economy e ) { System.out.println( ‘‘You want more water? That will be $5’’); } }
Answer:
Part 1. There are 2 consumer threads and 1 producer thread. C1 C2 P ECLP - - 0
Part 2. There is a race condition on the value of valueReady. It’s possible that valueReady could be seen as false by the consumer threads and as true by the producer thread. All three threads could then get stuck in the busy loop.
public class Worker extends Thread{ protected static class Resource { protected boolean inUse; public synchronized boolean get() { return inUse? false : (inUse = true); } public synchronized void put() { inUse = false; notify(); } public synchronized void waitFor() { while (!get()) try {wait();} catch (InterruptedException e) {} } }
protected Resource one, two;
Worker(Resource one, Resource two) { this.one = one; this.two = two; }
public void run() { for (;;) { one.waitFor(); two.waitFor(); // do work one.put(); two.put(); } }
public static void main(String args[]) { Resource f0 = new Resource(), f1 = new Resource(), f2 = new Resource(); (new Worker(f2, f1)).start(); (new Worker(f0, f2)).start(); (new Worker(f1, f0)).start(); } }