
CMSC330 Spring 2009 Practice Problems 6
1. Programming languages
a. Describe the difference between ad-hoc and parametric polymorphism.
b. Describe the difference between starvation and deadlock.
c. Describe how functional programming may be used to simulate OOP.
d. Describe 2 differences between HTML and XML.
e. Describe the difference between query languages and programming languages.
2. Polymorphism
Consider the following Java classes:
class A { public void a( ) { … } }
class B extends A { public void b( ) { … } }
class C extends B { public void c( ) { … }}
Explain why the following code is or is not legal
a. int count(Set<A> s) { … } … count(new Set<A>);
b. int count(Set<A> s) { … } … count(new Set<B>);
c. int count(Set s) { … } … count(new Set<A>);
d. int count(Set<?> s) { … } … count(new Set<A>);
e. int count(Set<? extends A> s) { … } … count(new Set<B>);
f. int count(Set<? extends B> s) { … } … count(new Set<A>);
g. int count(Set<? extends B> s) { for (A x : s) x.a( ); }
h. int count(Set<? extends B> s) { for (C x : s) x.c( ); }
i. int count(Set<? super B> s) { for (A x : s) x.a( ); }
j. int count(Set<? super B> s) { for (C x : s) x.c( ); }
3. Multithreading
a. Using Java Conditions, you must implement a synchronization construct called
MyBarrier. A MyBarrier object is created with a certain value n. When a thread
calls the method enter( ), it enters the barrier and blocks until a total of n threads
have entered the barrier. When the n
th
threads enters the barrier, all the threads
waiting at the barrier wake up and unblock, and the n
th
thread continues without
blocking. When a thread calls the method reset( ), the barrier is reset so that it
starts fresh in counting up to n (i.e., n more threads must enter the MyBarrier).
You may start by modifying the following code fragment:
public class MyBarrier {
public void MyBarrier (int n) { … }
public enter( ) { … }
public reset( ) { … }
}
b. Implement MyBarrier using Ruby monitors.
c. Write a Ruby program that creates a barrier for 2 threads, then creates 2 threads
that each print out “hello”, enters the barrier, then prints out “goodbye”.