Midterm Exam for CMSC 433: Programming Language Technologies and Paradigms, Exams of Programming Languages

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

Pre 2010

Uploaded on 07/30/2009

koofers-user-y3l
koofers-user-y3l 🇺🇸

10 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Midterm Exam
CMSC 433
Programming Language Technologies and Paradigms
Spring 2008
April 3, 2008
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
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Midterm Exam for CMSC 433: Programming Language Technologies and Paradigms and more Exams Programming Languages in PDF only on Docsity!

Midterm Exam

CMSC 433

Programming Language Technologies and Paradigms

Spring 2008

April 3, 2008

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

  1. Short answers (15 points). Give very short (1 to 2 sentences for each issue) answers to the following questions. 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 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

  1. Visitor Pattern (30 points). Implement 2 visitor classes for the following scenario. The class VisitorDemo simulates an airline flight attendant taking care of various passengers. The flight attendant can help seat a passanger and can feed a passenger. These two tasks are implemented in the HelpSeat and Feed classes. These 2 tasks are applied to a cabin of passengers. Passengers come in 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 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

  • ECLP - 0
    • EPLP 0
    • XPLP 0
    • AL 0
    • RL 1
  • XPLP - 1
  • AL - 1
  • RL - 1 XPLP - - 1 AL - - 1 RL - - 1

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.

  1. Java Concurrency (20 points) Some executions of the following program result in deadlock. Read the code carefully to determine how deadlock could occur. Then draw a wait graph that illustrates the deadlock situation resulting from your execution. Remember that wait graphs show Threads as circles, objects as rectangles, lines from Thread t to Object o when t holds a lock on o, and lines from Object o to Thread t when t is blocked while trying to acquire the lock on o.

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(); } }