CSE Programming Languages Final exam - Winter Answer Key

Similar documents
CSE Programming Languages Final exam - Autumn 2012

CSE Programming Languages Final exam - Winter Answer Key

CSE Programming Languages Final exam - Spring Answer Key

CSE Programming Languages Midterm - Winter 2010

CSE341 Autumn 2017, Final Examination December 12, 2017

CSE341 Spring 2016, Final Examination June 6, 2016

Computer Science & Engineering 505 Final December 10, 1999 Open book & notes 110 minutes 10 points per question 90 points total

CSE 341, Spring 2011, Final Examination 9 June Please do not turn the page until everyone is ready.

CSE341 Spring 2017, Final Examination June 8, 2017

CSE341 Spring 2017, Final Examination June 8, 2017

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

CSCI-GA Final Exam

STREAMS 10. Basics of Streams. Practice with Streams COMPUTER SCIENCE 61AS. 1. What is a stream? 2. How does memoization work?

Typed Racket: Racket with Static Types

CSE Programming Languages Final Exam - Autumn Answer Key

Programming Languages. Streams Wrapup, Memoization, Type Systems, and Some Monty Python

Outline. More optimizations for our interpreter. Types for objects

CSE Lecture 7: Polymorphism and generics 16 September Nate Nystrom UTA

CS450 - Structure of Higher Level Languages

Programming Languages Fall 2013

The Typed Racket Guide

Box-and-arrow Diagrams

Programming Paradigms Written Exam (6 CPs)

CSE341, Spring 2013, Final Examination June 13, 2013

UMBC CMSC 331 Final Exam

Lecture 19: Functions, Types and Data Structures in Haskell

CMSC 331 Final Exam Section 0201 December 18, 2000

CSE413 Midterm. Question Max Points Total 100

Name EID. (calc (parse '{+ {with {x {+ 5 5}} {with {y {- x 3}} {+ y y} } } z } ) )

cs61amt2_4 CS 61A Midterm #2 ver March 2, 1998 Exam version: A Your name login: cs61a- Discussion section number TA's name

CSC324 Principles of Programming Languages

CSC324 Principles of Programming Languages

Lecture #23: Conversion and Type Inference

CSE 431S Type Checking. Washington University Spring 2013

STREAMS 10. Basics of Streams. Practice with Streams COMPUTER SCIENCE 61AS. 1. What is a stream?

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

CS 415 Midterm Exam Fall 2003

Intro. Scheme Basics. scm> 5 5. scm>

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

Haskell Types, Classes, and Functions, Currying, and Polymorphism

Introduction to Programming Using Java (98-388)

CISC 3115 Modern Programming Techniques Spring 2018 Section TY3 Exam 2 Solutions

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

Prelim One Solution. CS211 Fall Name. NetID

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

CS 61A Midterm #2 ver March 2, 1998 Exam version: A. Your name. login: cs61a- Discussion section number. TA's name

CS 360: Programming Languages Lecture 10: Introduction to Haskell

CS 376b Computer Vision

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

Implementing objects as in Javascript, Lua, 164 is expensive because object attributes are implemented as hashtable accesses:

Java Object Oriented Design. CSC207 Fall 2014

CS 415 Midterm Exam Spring 2002

Overloading, Type Classes, and Algebraic Datatypes

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

Discussion. Type 08/12/2016. Language and Type. Type Checking Subtypes Type and Polymorphism Inheritance and Polymorphism

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

Conversions and Overloading : Overloading

Advanced features of Functional Programming (Haskell)

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Exercise 8 Parametric polymorphism November 18, 2016

Principles of Programming Languages 2017W, Functional Programming

CSE 113 A. Announcements - Lab

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

APCS Semester #1 Final Exam Practice Problems

CS558 Programming Languages

UNIVERSITY OF TORONTO Faculty of Arts and Science. Midterm Sample Solutions CSC324H1 Duration: 50 minutes Instructor(s): David Liu.

CSE 8B Intro to CS: Java

CSE 331 Final Exam 3/16/15 Sample Solution

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

Comp 311: Sample Midterm Examination

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

CS 275 Name Final Exam Solutions December 16, 2016

Module 8: Local and functional abstraction

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Lecture Overview. [Scott, chapter 7] [Sebesta, chapter 6]

CS 320: Concepts of Programming Languages

Introduction to Haskell

A LISP Interpreter in ML

2. [20] Suppose we start declaring a Rectangle class as follows:

Advances in Programming Languages

CS 314 Principles of Programming Languages. Lecture 16

Ruby: Introduction, Basics

COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

Functional Logic Programming Language Curry

4.2 Variations on a Scheme -- Lazy Evaluation

Programming Paradigms Written Exam

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

Exercise 6 Multiple Inheritance, Multiple Dispatch and Linearization November 4, 2016

Course year Typeclasses and their instances

YOUR NAME PLEASE: *** SOLUTIONS ***

Type-indexed functions in Generic Haskell

CS107 Handout 37 Spring 2007 May 25, 2007 Introduction to Inheritance

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Transcription:

CSE 341 - Programming Languages Final exam - Winter 2015 - Answer Key 1. (10 points) Write a Prolog rule doubles. It should succeed if its argument is a list of numbers, such that the n + 1 th number is two times the n th number. Use the clpr library. (Hint: remember that to write a constraint that uses the clpr library write it in curly brackets, e.g., {X=Y.) Here are some examples of goals that should succeed: doubles([]). doubles([3]). doubles([a,b,12,d,e]). /* should succeed with A=3.0, B=6.0, D=24.0, E=48.0 */ :- use_module(library(clpr)). doubles([]). doubles([_]). doubles([x,y Ys]) :- {Y=2*X, doubles([y Ys]). 2. (10 points) Write an analogous doubles? function in Racket. It should succeed if its argument is a list of integers, such that the n + 1 th integer is two times the n th integer. The Racket version doesn t need to allow for logic variables in the list, of course it just takes a list of integers. You don t need to do any error checking. In analogy with the Prolog rule, (doubles? ()) and (doubles? (10)) should both evaluate to #t. (define (doubles? s) (cond ((null? s) #t) ((null? (cdr s)) #t) ((= (* 2 (car s)) (cadr s)) (doubles? (cdr s))) (else #f))) 3. (15 points) The parser provided for the OCTOPUS interpreter represents Racket lists using the constructor OctoList followed by a Haskell list of OctoValues: data OctoValue = OctoInt Int OctoBool Bool OctoSymbol String OctoList [OctoValue]... Using this representation, parse "()" evaluates to OctoList [], and parse "(1 2 3)" evaluates to OctoList [OctoInt 1, OctoInt 2, OctoInt 3]. Suppose instead that we represent OCTOPUS lists using explicit cons cells: data OctoValue = OctoInt Int OctoBool Bool OctoSymbol String OctoConsCell OctoValue OctoValue OctoNil... 1

(a) Using the new representation, what does parse "()" evaluate to? OctoNil What does parse "(1 2 3)" evaluate to? OctoConsCell (OctoInt 1) (OctoConsCell (OctoInt 2) (OctoConsCell (OctoInt 3) OctoNil)) (b) Write primitives for octocons, octocar, and octocdr (i.e., Haskell functions to implement the primitives for cons, car, and cdr), using the new representation for lists. Hint: here is the code for octoplus from the starter file. As with all of the Haskell functions to implement primitives, it takes a Haskell list of the arguments to the primitive. getint (OctoInt i) = i -- The octoplus function takes a list of OctoInts and adds them. octoplus ints = OctoInt $ sum $ map getint ints octocons [x,y] = OctoConsCell x y octocar [OctoConsCell x y] = x octocdr [OctoConsCell x y] = y (c) A benefit of the new representation is that OCTOPUS could then handle improper lists. Draw a box-andarrow representation of the improper list (1 2. 3). o ---- -----> o o 1 2 3 (d) What should parse "(1 2. 3)" evaluate to? (In other words, what gets printed if you type parse "(1 2. 3)" at the ghci prompt after loading the OCTOPUS interpreter?) OctoConsCell (OctoInt 1) (OctoConsCell (OctoInt 2) (OctoInt 3)) 4. (8 points) Consider the following Prolog rule last, which succeeds if the first argument is a list, and the second argument is the last element of that list. last([x],x). last([_ Xs],Y) :- last(xs,y). What are all the answers returned to the following goals? (If there are an infinite number, give at least the first three.) last([1,2,3],a). A = 3 last(as,10). As = [10] ; 2

As = [_G1777, 10] ; As = [_G1777, _G1780, 10] ; As = [_G1777, _G1780, _G1783, 10] ; As = [_G1777, _G1780, _G1783, _G1786, 10] ; last([],a). fail last([1,2,3],5). fail Now suppose that we add a cut: last_cut([x],x) :-!. last_cut([_ Xs],Y) :- last_cut(xs,y). What are all the answers in this case to the same goals? last_cut([1,2,3],a). A=3 last_cut(as,10). As = [10]. last_cut([],a). fail last_cut([1,2,3],5). fail 5. (5 points) Which of the following lists represent valid difference lists? For valid difference lists, what list do they represent? [a,b,c]\[b,c] represents [a] [b,c]\[a,b,c] not a valid difference list T\T represents [] [X T]\T represents [X] [[1,2],[3,4],[5,6]]\[5,6] not a valid difference list 6. (10 points) The PosRational class, which we used as an introductory Ruby example, includes a + method to add two positive rational numbers. However, this method doesn t interoperate correctly with integers. Write 3

a modified version of the + method for PosRational, and also any additional new methods that you need, to make r+3 and 3+r work correctly for a postive rational instance r. Hint: here is the code for PosRational for initialize and +. class PosRational def initialize(num,den=1) if num < 0 den <= 0 raise "PosRational received an inappropriate argument" @num = num @den = den reduce def + r ans = PosRational.new(@num,@den) ans.add(r) ans New code: class PosRational def + r ans = PosRational.new(@num,@den) ans.add(r.asposrational) ans def asposrational self def coerce(n) # coerce gets called when we try to add or multiply a number and # a posrational return [n.asposrational, self] /* Note that we put this method in Integer. Then this is available for both Fixnum and Bignum. We do not want to put it to Numeric, though, since we want trying to add a float and a positive rational to fail. */ class Integer def asposrational return PosRational.new(self) 4

7. (10 points) Define a Ruby class MyRange that represents a range of integers, with an optional step. (Note that the MyRange class in the example notes didn t include a step.) Include the Enumerable mixin in your class. MyRange should include two methods: initialize and each. initialize should have three parameters: first, last, and step. step should be optional and default to 1. It can be negative, but you should raise an exception if it is 0. Here are some examples. r = MyRange.new(1,5) /* r.each should yield the values 1,2,3,4,5 */ r = MyRange.new(6,0,-2) /* r.each should yield the values 6,4,2,0 */ r = MyRange.new(10,10) /* r.each should yield the value 10 */ r = MyRange.new(6,10,-1) /* r.each should yield no values */ r = MyRange.new(6,10,0) /* this should raise an exception */ class MyRange include Enumerable def initialize(first,last,step=1) if step==0 raise "step cannot be 0" @first = first @last = last @step = step def each i=@first while (i <= @last && @step>0) (i>=@last && @step<0) yield i i=i+@step 8. (10 points) Suppose that Java didn t overload method names, and allowed method typing to be contravariant in the argument types. Then suppose we have the following interface: interface Octopus { public void test1(rectangularshape s); public void test2(arraylist<point> as); 5

For each of the method declarations in the following programs, say whether or not it results in a compile time error. You should have a total of 4 answers. (Hint: Rectangle2D is a subclass of RectangularShape.) public class BabyOctopus1 implements Octopus { public void test1(object s) { System.out.println("calling test1 in BabyOctopus1"); // no error, since RectangularShape is a subtype of Object public void test2(arraylist<object> s) { System.out.println("calling test2 in BabyOctopus1"); // compile time error, since ArrayList<Point> is NOT a subtype // of ArrayList<Object> public class BabyOctopus2 implements Octopus { public void test1(rectangle2d s) { System.out.println("calling test1 in BabyOctopus2"); // compile time error, since Object is NOT a subtype of Rectangle2D public void test2(arraylist<?> s) { System.out.println("calling test2 in BabyOctopus2"); // no error, since ArrayList<Object> is a subtype of ArrayList<?> Finally, is this version of Java s type system, with contravariant typing for method arguments, sound? If it is sound, give an example method call that passes this version of the type system but that would result in a compile time error in standard Java. If it is not sound, give an example of a method call that passes type checking and that results in a runtime type error. Yes, it is sound. Both BabyOctopus1 test1 and BabyOctopus2 test2 pass this version of the type system but get a compile-time error in Java. 9. (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? String[] a = new String[10]; Object[] b; b = a; b[0] = new Point(10,20); // compiles but gets a runtime exception Object[] a = new Object[10]; String[] b; b = a; b[0] = "squid!"; // compile-time error String[] a = new String[10]; 6

Object[] b; b = a; b[0] = "squid!"; // compiles and executes without errror 10. (16 points) Consider the following Ruby classes and mixins. (Feel free to tear this page out of the exam and not hand it in, if you don t have anything on it you want graded and if you want to have it side-by-side when looking at the expressions on the following page.) class Class1 def seacreatures ["octopus"] + others def others ["squid"] module M1 def seacreatures ["clam"] + super module M2 def others ["oyster"] class Class2 < Class1 include M1 class Class3 < Class1 include M1, M2 Suppose we define the following variables: c1 = Class1.new c2 = Class2.new c3 = Class3.new What is the result of evaluating the following expressions? c1.seacreatures ["octopus", "squid"] c2.seacreatures ["clam", "octopus", "squid"] 7

c3.seacreatures ["clam", "octopus", "oyster"] Class2.superclass Class1 Class2.ancestors [Class2, M1, Class1, Object, Kernel, BasicObject] Class3.ancestors [Class3, M1, M2, Class1, Object, Kernel, BasicObject] Class3.class Class Class3.class.class Class (Hint: Object.ancestors evaluates to [Object, Kernel, BasicObject].) 11. (0 points) equal? is Ruby s idea of what the identity test should be called. Is this merely misguided, or in fact a sinister plot by the Ruby implementors to confuse generations of programmers as to the difference between object identity or object equality? Def your answer. (Continue on the backs of the pages as needed.) 12. (10 points) True or false? (a) Suppose we had a dynamically typed version of Haskell called D-Haskell. Any program in normal Haskell that successfully compiles and executes would also successfully compile and execute in D-Haskell. True (b) Any program in D-Haskell that successfully compiles and executes would also successfully compile and execute in normal Haskell. False (we could have an expression that doesn t type check but that is never evaluated) (c) In Ruby, if x==y evaluates to true, x.equal?(y) must evaluate to true as well; this is enforced by the language implementation. False (it ought to, but this isn t enforced) (d) Ruby blocks are not first-class citizens. True, alas (e) Java methods can be covariant in the return type. True 8