DOC PREVIEW
UMD CMSC 433 - Midterm Exam

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

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 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 9 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 9 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Midterm ExamCMSC 433Programming Language Technologies and ParadigmsSpring 2008April 3, 2008GuidelinesPut 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, thenbring 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 answergeneral questions however. If you feel an exam question assumes something that is not written, write itdown on your exam sheet. Barring some unforeseen error on the exam, however, you shouldn’t need todo 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 timewisely.Question Points Score1 152 103 304 255 20Total 1001. Short answe rs (15 points). Give very short (1 to 2 sentences for each issue) answers to the followingquestions. Longer responses to these questions will not be read.(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 answersaccepted.(b) Synchronization in Java serves three functions: visibility, ordering, atomicity. Briefly explaineach function.Answer:i. Atomicity. Allows operation to be done without interference from other threads.ii. Visibility. Signals when values will be updated across threadsiii. Ordering. Allows programmers to ensure that an operation A occurs before oper-ation B(c) Information hiding refers to a c riteria for modularizing software systems. What is this criteria?Answer:Modules should encapsulate things that are likely to change22. The Bridge Pattern (10 points). You are designing software for devices in a home. Each of thesedevices has a switch. Actual Switches come in several varieties, such as a pull chain, a two-pos itionswitch, or a dimme r switch. Explain how the Bridge pattern could be used in this context. Keepyou answer under 2–3 paragraphs.Answer:The Bridge pattern decouples an abstraction from its implementation, so that the twocan vary independently. A household switch controlling lights, ceiling fans, etc. is anexample of the Bridge. The purpose of the switch is to turn a device on or off. So theoperations interface would support at least two methods on() and off(). Subclasses couldadd dimmer capabilities. The actual switches can be implemented as a pull ch ain, simpletwo position switch, or a variety of dimmer switches. So the SwitchImpl interface wo uldimplement on() and off() in terms of the methods provided by t he actual devices.33. Visitor Pattern (30 points). Implement 2 visitor classes for the following scenario. The classVisitorDemo simulates an airline flight attendant taking care of various passengers. The flightattendant can help seat a passanger and can feed a passenger. These two tasks are implemented inthe HelpSeat and Feed classes. These 2 tasks are applied to a cabin of passengers. Passengers comein 3 types: First, Business and Economy. Different types of passengers get different treatment.Consider the driver program below.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 ClassFollow me Boss to BusinessEconomy is that way, palAnother glass of Champagne?May I pour you an after dinner cordial?You want more water? That will be $5Using the following interfaces, implement the First, Business, Economy, HelpSeat and Feed clas sesneeded 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 );}4Answer: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’’); }}54. Java Concurrency (25 points). Consider the following program.public class ProducerConsumer {private boolean valueReady = false;private int bufferValue;void produce(int i) {while (valueReady) {}; // busy loopsynchronized (this) {bufferValue = i;valueReady = true;}}int consume() {while (!valueReady) {}; // busy loopsynchronized (this) {valueReady = false;return bufferValue;}}public static void main(String[] args) {final ProducerConsumer p = new ProducerConsumer();// 3 THREADS CALL PRODUCE AND CONSUME ON P (see below).....}}(1) Although this program is intended to follow normal buffer semantics, under some circumstancesthe same data can be read multiple times from the buffer.Assuming 3 threads that loop forever, each one calling only p.produce() or only p.consume().Provide a smallest program trace you can that shows how the above program can fail.In describing your program trace, first briefly describe your 3 threads (i.e., are they producers orconsumers). Then create a table like the one below, showing the steps executed by each thread. Astep will be defined as an entry to p.produce’s busy loop (written EPLP), an exit from p.produce’sbusy loop (written XPLP), an entry to p.consume’s busy loop (written ECLP), an exit fromp.consumer’s busy loop


View Full Document

UMD CMSC 433 - Midterm 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 Midterm 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 Midterm 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 Midterm 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?