DOC PREVIEW
UW CSE 341 - Study Notes

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

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

Unformatted text preview:

CSE 341 - Programming LanguagesFinal exam - Autumn 2008Your Name:Open book and notes. No laptop computers, PDAs, or similar devices. (Calculators are OK, althoughyou won’t need one.) Please answer the problems on the exam paper — if you need extra space use the backof a page.90 points total1. (10 points) Suppose we have the following definition of the map function in Haskell.map f [] = []map f (x:xs) = f x : map f xsCircle each type declaration that is a correct type for map. (Not necessarily the most general type,just a correct one.)1map :: ([Integer] -> [Integer]) -> [[Integer]] -> [[Integer]]map :: (a -> a) -> [a] -> [a]map :: (a -> b) -> [b] -> [a]map :: (a -> b) -> [a] -> [b]Which of the above types, if any, is the most general type for map?1Oh no, I hear you cry! Not this question again . . .12. (6 points) Consider the following Scheme expression:(let*((x a)(y (+ x 10)))(+ x y))Write an equivalent Scheme expression that binds x and y to the same values as above and returns thevalue of (+ x y) but that only uses let and not let*.3. (10 points) Write a Ruby class Delay that implements delays (as we saw in Scheme). The followingcode shows how it should work:n = 0d = Delay.new {n=n+1; 3+4}d.forced.forcev = d.forcee = Delay.new {1/0}After we evaluate these statements v should be 7, but n should only be 1 (since we only evaluate theblock once). Further, since we never force e, we shouldn’t get a divide-by-zero error.24. (10 points)(a) Write a repeat rule in CLP(R) with three arguments: N, X, and Xs. It succeeds if Xs is a listconsisting of N copies of X. Here are some sample goals and results:?- repeat(3,a,Xs).Xs = [a,a,a]?- repeat(0,a,Xs).Xs = []?- repeat(-2,a,Xs).no.(b) What answers are returned for the following goals using the rule you just defined for Question4a? (Part of answering Question 4b is deciding what a reasonable behavior is in these situations.)Continue on the back of this page as needed. If there are a finite number of answers give themall; if an infinite number give at least the first three.?- repeat(N,Q,[a,a,a,a]).?- repeat(N,Q,[a,b,c]).?- repeat(N,a,As).35. (12 points) Consider the following CLP(R) rule:sum([],0).sum([X|Xs],X+S) :- sum(Xs,S).(a) Draw the simplified derivation tree for the goal sum([100], N).(b) Draw the simplified derivation tree for the goal sum(As, 10). (This is an infinite tree; in-clude at least 3 answers in the tree that you draw.)Again, continue on the back of the page if necessary.46. (10 points) This question use the following hierarchy of Java classes for simple geometric shapes.(This is the same hierarchy you used for the last homework assignment.)public interface GeometricShape {public void describe();}public interface TwoDShape extends GeometricShape {public double area();}public interface ThreeDShape extends GeometricShape {public double volume();}public class Circle implements TwoDShape {....}public class Cone implements ThreeDShape {....}public class Rectangle implements TwoDShape {....}public class Sphere implements ThreeDShape {....}Suppose we have three different implementations of a total_area method to find the total area ofsome shapes.public static double total_area1(ArrayList<TwoDShape> shapes){double sum = 0;for(TwoDShape shape : shapes) {sum = sum + shape.area();}return sum;}5public static double total_area2(ArrayList<? extends GeometricShape> shapes){double sum = 0;for(GeometricShape shape : shapes) {sum = sum + ((TwoDShape) shape).area();}return sum;}public static double total_area3(ArrayList<? extends TwoDShape> shapes){double sum = 0;for(TwoDShape shape : shapes) {sum = sum + shape.area();}return sum;}Now suppose that we have four variables declared as follows:ArrayList<Rectangle> rects;ArrayList<TwoDShape> two_d_shapes;ArrayList<GeometricShape> geo_shapes;double area;We then assign various shapes to the different arraylists. (So rects will only contain instancesof Rectangle, while geo shapes might contain instances of Circle, Cone, Rectangle, orSphere.)Consider the following code that uses these methods and variables. Write “compiles” next to thefollowing statements that compile correctly, and write “doesn’t compile” next to the ones that don’tcompile. Finally, if there is a possibility that the statement will cause a cast exception to be raised,write “possible exception” next to it.area = total_area1(rects);area = total_area1(two_d_shapes);area = total_area1(geo_shapes);area = total_area2(rects);area = total_area2(two_d_shapes);area = total_area2(geo_shapes);area = total_area3(rects);area = total_area3(two_d_shapes);area = total_area3(geo_shapes);67. (6 points) What is something that Ruby mixins can do that Java interfaces can’t?8. (6 points) Consider the following Java code fragments. In each case, does the code compile correctly?If so, does it execute without error, or is there an exception?(a) Object[] a = new String[10];a[0] = "hi there";(b) ArrayList<Object> a = new ArrayList<String>();a.add("hi there");(c) Object[] a = new String[10];a[0] = new Point(10,20);9. (10 points) What is duck typing? What is an advantage of duck typing over a more standard approach,and what is a potential problem? Finally, could you program in Java in a way that follows the ducktyping philosophy? Why or why not?710. (10 points) In Java, a class can be covariant in the return type of an inherited method. It turns out thatthis feature is new in J2SE 5.0; before that, the return type had to be invariant (i.e., exactly the sameas the return type in the method in the superclass or interface).For a widely used language like Java, the language designers must concern themselves with compata-bility issues. What compatability issues were raised by this change? Are there programs that used tocompile without error in pre-5.0 Java that now get a compile error? Are there programs that formerlywouldn’t compile that now do compile? What about runtime behavior? Are there programs that com-piled in old and new Java but that can behave differently? If there are such programs with differentcompile time or run time behavior, give an example, and explain why there is a


View Full Document

UW CSE 341 - Study Notes

Documents in this Course
Macros

Macros

6 pages

Macros

Macros

6 pages

Macros

Macros

3 pages

Mutation

Mutation

10 pages

Macros

Macros

17 pages

Racket

Racket

25 pages

Scheme

Scheme

9 pages

Macros

Macros

6 pages

Load more
Download Study Notes
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 Study Notes 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 Study Notes 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?