CSE Programming Languages Final exam - Spring Answer Key

Similar documents
CSE Programming Languages Final exam - Winter Answer Key

CSE Programming Languages Final exam - Autumn 2012

CSE Programming Languages Midterm - Winter 2010

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

CSE Programming Languages Final exam - Winter Answer Key

An introduction introduction to functional functional programming programming using usin Haskell

CS 61B Discussion 5: Inheritance II Fall 2014

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

Programming Paradigms Written Exam (6 CPs)

Programming Languages Fall 2013

CSE341, Spring 2013, Final Examination June 13, 2013

CSE341 Spring 2017, Final Examination June 8, 2017

Java Object Oriented Design. CSC207 Fall 2014

Lecture 19: Functions, Types and Data Structures in Haskell

Advanced features of Functional Programming (Haskell)

CSE 401 Final Exam. March 14, 2017 Happy π Day! (3/14) This exam is closed book, closed notes, closed electronics, closed neighbors, open mind,...

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

CS 320 Midterm Exam. Fall 2018

Programming Paradigms Written Exam

CS 320: Concepts of Programming Languages

Overloading, Type Classes, and Algebraic Datatypes

Chapter 5 Object-Oriented Programming

CSE341 Spring 2016, Final Examination June 6, 2016

CSE Lecture 3: Objects 2 September Nate Nystrom University of Texas at Arlington

CSE au Final Exam Sample Solution

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

Programming Paradigms Written Exam (6 CPs)

CSE 341, Autumn 2015, Ruby Introduction Summary

CSE P 501 Exam 8/5/04

CSCI-GA Final Exam

A Second Look At ML. Chapter Seven Modern Programming Languages, 2nd ed. 1

CSE341 Spring 2017, Final Examination June 8, 2017

Shell CSCE 314 TAMU. Haskell Functions

User-Defined Algebraic Data Types

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

CS558 Programming Languages

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

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

CMSC330 Spring 2015 Final Exam 9:30am/11:00am/12:30pm

CSE341 Autumn 2017, Final Examination December 12, 2017

Functional Logic Programming Language Curry

Semantics of programming languages

CSc 372 Comparative Programming Languages

Outline. More optimizations for our interpreter. Types for objects

An introduction to functional programming. July 23, 2010

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

An Introduction to Subtyping

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

CS-202 Introduction to Object Oriented Programming

CMSC 330: Organization of Programming Languages. OCaml Expressions and Functions

CS558 Programming Languages

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

Week 7. Statically-typed OO languages: C++ Closer look at subtyping

CMSC330 Fall 2016 Midterm #1 2:00pm/3:30pm

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

CSE341 Spring 2017, Midterm Examination April 28, 2017

CMSC330 Spring 2017 Midterm 2

CSE331 Spring 2015, Final Examination June 8, 2015

Total Score /1 /20 /41 /15 /23 Grader

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

CSE413 Midterm. Question Max Points Total 100

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17

Functional Programming in Haskell Part I : Basics

CSE 341 : Programming Languages Midterm, Spring 2015

CSc 372 Comparative Programming Languages

Project 6 Due 11:59:59pm Thu, Dec 10, 2015

CS 415 Midterm Exam Spring 2002

Programming Paradigms Written Exam (6 CPs)

CSc 372. Comparative Programming Languages. 8 : Haskell Function Examples. Department of Computer Science University of Arizona

COP4020 Programming Assignment 1 - Spring 2011

Example: Haskell algebraic data types (1)

Prelim 1. CS2110, October 2, 2014, 5:30 PM Extra Total Question TrueFalse Multiple Object Oriented

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

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

More fun with recursion

CS 340 Spring 2019 Midterm Exam

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

CPSC 311, 2010W1 Midterm Exam #2

CSci 450: Org. of Programming Languages Overloading and Type Classes

Programming Paradigms Written Exam (6 CPs)

Example Scheme Function: equal

XC Total Max Score Grader

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

Big Bang. Designing a Statically Typed Scripting Language. Pottayil Harisanker Menon, Zachary Palmer, Scott F. Smith, Alexander Rozenshteyn

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

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

CS4215 Programming Language Implementation

A general introduction to Functional Programming using Haskell

CSE Programming Languages Final Exam - Autumn Answer Key

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

CSE 130, Fall 2005: Final Examination

Programming Paradigms Written Exam (6 CPs)

CMSC 331 Final Exam Section 0201 December 18, 2000

Principles of Programming Languages 2017W, Functional Programming

A Short Summary of Javali

Introduction to Programming Using Java (98-388)

Haskell: From Basic to Advanced. Part 2 Type Classes, Laziness, IO, Modules

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Type-indexed functions in Generic Haskell

Haskell Overloading (1) LiU-FP2016: Lecture 8 Type Classes. Haskell Overloading (3) Haskell Overloading (2)

Transcription:

CSE 341 - Programming Languages Final exam - Spring 2018 - Answer Key 1. (8 points) Write a Racket function multicons that takes an item x, a non-negative integer n, and a list xs; and returns a new list with n occurrences of x followed by xs. You don t need to handle bad inputs. Examples: (multicons 'z 3 '(a b c)) => (z z z a b c) (multicons '(a b) 3 '(x y z)) => ((a b) (a b) (a b) x y z) (multicons 'z 0 '(a b c)) => (a b c) (define (multicons x n xs) (if (zero? n) xs (cons x (multicons x (- n 1) xs)))) Or here is a nice tail-recursive solution. (Both would receive full credit though.) (define (multicons x n xs) (if (zero? n) xs (multicons x (- n 1) (cons x xs)))) 1

2. (10 points) Suppose we have the following class in Ruby: class Toy def initialize(size, type) @size = size @type = type # The goodness method returns a number indicating how good # the toy is. The default is that bigger is better! def goodness size (a) Add code to Toy to define public getters for size and type (but not setters), and to mix in the Comparable module. Write your extra code in the blank spaces in the Toy definition above. To compare two toys, compare their goodness. You should define in Toy the method needed by Comparable; this should then automatically let you compare two toys t1 and t2 using t1>t2, t1<t2, t1==t2, and so on, without needing to define <, >, and so on in Toy. Hint: 3<=>10 evaluates to -1. class Toy include Comparable attr_reader :size, :type def initialize(size, type) @size = size @type = type # The goodness method returns a number indicating how good # the toy is. The default is that bigger is better! def goodness size def <=> other goodness <=> other.goodness (b) Write a subclass of Toy called StuffedAnimal. StuffedAnimal has the same fields as Toy plus numhugsgiven. numhugsgiven should be 0 on initialization. 2

The new initialize method for StuffedAnimal should still take 2 arguments: size and type. For full credit, when possible reuse relevant methods inherited from Toy. Redefine goodness for StuffedAnimal to be the product of its size and the number of hugs given. Add a public method hug that increments numhugsgiven by 1 when called. Add a public getter for numhugsgiven. class StuffedAnimal < Toy attr_reader :numhugsgiven def initialize(size, type) super(size, type) @numhugsgiven = 0 def goodness size*numhugsgiven def hug @numhugsgiven = numhugsgiven+1 3

3. (10 points) Write a Haskell function indices that takes a item and a list of that same type of item, and returns a list of the positions of that item in the list. You can use a helper function if needed. Also give the most general type of the indices function. Examples: indices 'b' "ababb" => [1,3,4] indices true [false,false,true] => [2] indices 'x' "abc" => [] indices x xs = helper x 0 xs helper x n [] = [] helper x n (y:ys) = if x==y then n : helper x (n+1) ys else helper x (n+1) ys indices :: (Num a, Eq t) => t -> [t] -> [a] Or here is a version that doesn t use a helper function: indices x [] = [] indices x (y:ys) = if x==y then 0 : rest else rest where rest = map (+1) (indices x ys) 4

4. (6 points) What is the output from the following Ruby program? Write the output on the numbered lines. Hint: puts for a hash prints like this: {"x"=>100}. def test1(a,b) a["x"] = "squid" b["x"] = "clam" def test2(a,b) a["x"] = "tuna" b = {"x"=>"starfish"} a = Hash.new test1(a,a) puts a test2(a,a) puts a b = Hash.new c = Hash.new test1(b,c) puts b puts c test2(b,c) puts b puts c {"x"=>"clam"} {"x"=>"tuna"} {"x"=>"squid"} {"x"=>"clam"} {"x"=>"tuna"} {"x"=>"clam"} 5

5. (6 points) Suppose that Ruby passed parameters by reference. What would the output be in that case for the program in Question 4? {"x"=>"clam"} {"x"=>"starfish"} {"x"=>"squid"} {"x"=>"clam"} {"x"=>"tuna"} {"x"=>"starfish"} 6

6. (10 points) Write a Prolog rule index_of(x,xs,n) that finds the element at a given position in a list. You can assume that N is an integer in the goal. However, either X or Xs or both could be variables. Use is for arithmetic. Examples: index_of(x,[a,b,c,d],2) should succeed with X=c index_of(x,[a,b,c,d],10) should fail index_of(x,[],0) should fail index_of(x, [X _], 0). index_of(x, [_ Xs], I) :- I > 0, I2 is I-1, index_of(x, Xs, I2). 7. (6 points) Using your rule from Question 6, what are all the answers returned for the following goals? If there are infinitely many, give the first three. Write false if the derivation fails. If your answer involves variables generated by Prolog, make up names like this: _42 (the exact number you use in the name doesn t matter). (a) index_of(b,[a,b,c,d],3) => false (b) index_of(a,xs,0) => Xs = [a _1] (c) index_of(a,xs,2) => Xs = [_2, _3, a _4 7

8. (10 points) Rewrite your Prolog rule from Question 6 to use constraints on integers, using the clpfd library. Hint: to remind you of the syntax for constraints in clpfd, here are examples of constraints on K: K#>5, K#=J+4, K#>=0. index_of(x, [X _], 0). index_of(x, [_ Xs], I) :- I#>0, J#=I-1, index_of(x, Xs, J). 9. (6 points) Using your improved rule from Question 8, what are all the answers returned for the following goals? If there are infinitely many, give the first three. Write false if the derivation fails. If your answer involves variables generated by Prolog, make up names like this: _42 (the exact number you use in the name doesn t matter). (a) index_of(b,[a,b,c,d,a,b,c,d],n) => N=1 N=5 (b) index_of(x,[a,b,c],n) => X=a, N=0 X=b, N=1 X=c, N=2 (c) index_of(a,xs,n) => Xs=[a _1], N=0 Xs=[_2,a _3], N=1 Xs=[_4,_5,a _6], N=2... 8

10. (10 points) Here are some groups of statements about Java types. Circle all statements that are correct as far as the Java compiler is concerned. In addition, write an E on the line next to each statement if that statement is correct as far as the Java compiler is concerned, but that could result in a runtime exception due to a type error. For example, suppose that one group of statements is Rectangle2D is a subtype of RectangularShape RectangularShape is a subtype of Rectangle2D You would circle Rectangle2D is a subtype of RectangularShape because that statement is correct as far as the Java compiler is concerned. You would not write an E next to it, since this could never result in a runtime exception due to a type error. You would not circle the other two statements. Hint: note that any type T is a subtype of itself. (a) Integer is a subtype of Object Object is a subtype of Integer (b) Integer[] is a subtype of Object[] E Object[] is a subtype of Integer[] (c) LinkedList<Integer> is a subtype of LinkedList<Object> LinkedList<Object> is a subtype of LinkedList<Integer> (d) LinkedList<?> is a subtype of LinkedList<? exts RectangularShape> LinkedList<? exts RectangularShape> is a subtype of LinkedList<?> (e) LinkedList<?> is a subtype of LinkedList<? exts Object> LinkedList<? exts Object> is a subtype of LinkedList<?> 9

11. (10 points) True or false? Write T or F on the line in front of the question. (a) F Racket s eq? function could be added to OCTOPUS as a new primitive function. Object identity is not meaningful for ordinary Haskell data, so adding eq? to OCTOPUS would require changing how OCTOPUS data is represented. For example, (equal? (cons 1 '()) (cons 1 '())) evaluates to #t in both Racket and OCTOPUS, as it should. What about (eq? (cons 1 '()) (cons 1 '()))? In Racket this evaluates to #f getting this to also evaluate to #f in OCTOPUS would require changing the representation of OctoValue. (b) T Adding support for floating-point numbers to OCTOPUS would require changes to the lexer and/or parser, in addition to changes to the interpreter. (c) F The class Class in Ruby is a subclass of itself. (d) T The class Class in Ruby is an instance of itself. (e) F Any two Haskell lists can be tested for equality, since the list type is in the Eq type class. (Consider a list of functions, for example [sin,cos]==[sin,cos]. Two lists can be tested for equality only if the elements are in the Eq type class.) (f) T Any let* expression in Racket can be rewritten as a set of nested let expressions. (g) F Adding a cut to a Prolog program may change the number of answers that are returned, but will never result in different answers. We ed up throwing out this question. The inted answer was F (consider removing the cut from the definition of not in the lecture notes, since with the cut you can get a fail and if no cut some answer) but you could argue that is still just changing the number of answers. (h) F Java methods can be contravariant in the return type. (But they can be covariant in the return type.) (i) T Java methods can be overloaded based on the declared types of the method arguments. (j) F In Ruby, a singleton class has only one superclass, but other classes may have multiple superclasses. 12. (12 points) Consider the following Ruby class definitions. class Book attr_reader :author, :title def initialize(author, title) @author = author @title = title def description title + " by " + author + "." class Textbook < Book attr_reader :subject def initialize(author, title, subject) super(author,title) @subject = subject def description return super + " A textbook about " + subject + "." 10

class AnonymouslyWrittenBook < Book def initialize(title) @title = title def author "anonymous" Suppose we make three objects b, t, and a by evaluating these statements: b = Book.new("J.K. Rowling", "Harry Potter and the Deathly Hallows") t = Textbook.new("James Stewart", "Calculus", "mathematics") a = AnonymouslyWrittenBook.new("Haskell Good") Then what is the result of evaluating each of these expressions? Hint: the instance_variables method returns an array of instance variable names, like this: [:@x, :@y]. b.description Harry Potter and the Deathly Hallows by J.K. Rowling. t.description Calculus by James Stewart. A textbook about mathematics. a.description Haskell Good by anonymous. b.instance_variables [:@author, :@title] t.instance_variables [:@author, :@title, :@subject] a.instance_variables [:@title] Note that since it doesn t call the inherited initialize method, a doesn t have an author instance variable. 11