CMSC330 Spring 2017 Midterm #1 9:30am/11:00am/12:30pm/5:00pm

Similar documents
CMSC330 Spring 2017 Midterm #1 Solution

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

CMSC330 Spring 2017 Midterm 2

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

CMSC330 Spring 2016 Midterm #1 9:30am/12:30pm/3:30pm

CMSC330 Spring 2014 Midterm #2. Score 1 OCaml types & type

CMSC330 Fall 2017 Final Exam

CMSC330 Spring 2016 Midterm #1 9:30am/12:30pm/3:30pm Solution

CMSC330 Spring 2016 Midterm #2 9:30am/12:30pm/3:30pm

CMSC330 Fall 2015 Midterm #1 12:30pm/2:00pm/5:00pm

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

CMSC330 Fall 2016 Final Exam

CMSC330 Spring 2015 Midterm #1 9:30am/11:00am/12:30pm

CMSC330 Spring 2015 Midterm #1 9:30am/11:00am/12:30pm

CMSC 330, Fall 2018 Midterm 1

CMSC330 Fall 2017 Final Exam Solution

Problem Score Max Score 1 Syntax directed translation & type

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

CMSC330 Spring 2016 Final Exam

CMSC 330, Fall 2018 Midterm 1

CMSC330 Spring 2018 Midterm 2 9:30am/ 11:00am/ 3:30pm

CIS 110 Introduction to Computer Programming Spring 2016 Midterm

CMSC330 Fall 2014 Final Exam

CMSC330 Fall 2009 Final Exam

CMSC 430 Introduction to Compilers. Fall Everything (else) you always wanted to know about OCaml (but were afraid to ask)

15-110: Principles of Computing, Spring 2018

CMSC330 Fall 2014 Midterm 2 Solutions

CSE115 Introduction to Computer Science for Majors I Spring 2008 Final Exam. First/Given Name (PRINT) May 6, 2008

CMSC 330, Fall 2018 Midterm 2

CMSC411 Fall 2013 Midterm 1

CMSC330 Spring 2008 Final Exam

CMSC 330, Fall 2018 Midterm 2

CMSC330 Spring 2014 Midterm 2 Solutions

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

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

CSE341 Spring 2017, Final Examination June 8, 2017

Algebraic data types. The case against null

Review Questions II KEY

CS 11 Ocaml track: lecture 2

CSE341 Spring 2017, Final Examination June 8, 2017

CSCE121: Introduction to Program Design and Concepts Practice Questions for Midterm 1

CMSC 330 Exam #2 Fall 2008

CMSC 330, Fall 2013, Practice Problems 3

CMSC330 Fall 2014 Midterm 1 Solution

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

Computer Science Foundation Exam. December 15, Section I A. No Calculators! Name: SSN:

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

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

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

CIS 110 Introduction to Computer Programming Summer 2018 Final. Recitation # (e.g., 201):

Tuples. CMSC 330: Organization of Programming Languages. Examples With Tuples. Another Example

CMSC330 Fall 2014 Midterm 1 Grading

CS Lecture 5: Pattern Matching. Prof. Clarkson Fall Today s music: Puff, the Magic Dragon by Peter, Paul & Mary

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

CMSC330 Fall 2016 Final Exam Solution

OCaml Data CMSC 330: Organization of Programming Languages. User Defined Types. Variation: Shapes in Java

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

CMSC 330, Fall 2013, Practice Problem 3 Solutions

CMSC 330: Organization of Programming Languages. Closures (Implementing Higher Order Functions)

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

UVa ID: NAME (print): CS 4501 LDI Midterm 1

Sample file. Practice Exam One COMPUTER SCIENCE A SECTION I. Directions: Determine the answer to each of the following questions or incomplete

CS421 Spring 2014 Midterm 1

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

Relationship between Pointers and Arrays

Midterm 2. CMSC 430 Introduction to Compilers Fall 2018

CIS 110 Introduction to Computer Programming Summer 2018 Final. Recitation # (e.g., 201):

Name: CIS 120e Midterm I Review

Programming Paradigms

CSE505, Fall 2012, Midterm Examination October 30, 2012

OCaml Style Guide Fall 2017

COMP 524 Spring 2018 Midterm Thursday, March 1

CMSC430 Spring 2009 Midterm 2 (Solutions)

CMSC330 Fall 2010 Final Exam Solutions

CIS 110 Fall 2014 Introduction to Computer Programming 8 Oct 2014 Midterm Exam Name:

CS Exam 2 Name: Your Grading TA: This exam has 7 pages including the title page. Please check to make sure all pages are included.

Chapter 6 - Pointers

University of Maryland College Park Dept of Computer Science CMSC106 Fall 2016 Midterm I

FORM 2 (Please put your name and form # on the scantron!!!!)

CSE341 Spring 2016, Midterm Examination April 29, 2016

University of Washington CSE 140 Data Programming Winter Final exam. March 11, 2013

CMSC 330: Organization of Programming Languages

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

CIS 120 Midterm I October 2, Name (printed): Username (login id):

Read and fill in this page now. Your instructional login (e.g., cs3-ab): Your lab section days and time: Name of the person sitting to your left:

Midterm #2a Fall minutes

CSCI 111 First Midterm Exam Fall Solutions 09.00am 09.50am, Wednesday, October 18, 2017

CSC/MAT-220: Lab 6. Due: 11/26/2018

CSE341 Spring 2016, Final Examination June 6, 2016

Gain familiarity and practice with folding and mapping. The following supplementary materials may be helpful in completing this assignment:

CSE341 Spring 2017, Midterm Examination April 28, 2017

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

Final Exam CS 152, Computer Programming Fundamentals December 9, 2016

Introduction to OCaml

Version B Final Exam

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Metaprogramming assignment 3

Name: Pennkey: CIS 120 Midterm II November 18, 2011

Mini-ML. CS 502 Lecture 2 8/28/08

CSE 341 Section 5. Winter 2018

Transcription:

CMSC330 Spring 2017 Midterm #1 9:30am/11:00am/12:30pm/5:00pm Name (PRINT YOUR NAME as it appears on gradescope ): Discussion Time (circle one) 10am 11am 12pm 1pm 2pm 3pm Discussion TA (circle one) Aaron Alex Austin Ayman Daniel Eric Greg Jake JT Sam Tal Tim Vitung Instructions Do not start this test until you are told to do so! You have 75 minutes to take this midterm. This exam has a total of 100 points, so allocate 45 seconds for each point. This is a closed book exam. No notes or other aids are allowed. Answer essay questions concisely in 2-3 sentences. Longer answers are not needed. For partial credit, show all of your work and clearly indicate your answers. Write neatly. Credit cannot be given for illegible answers. Problem Score 1 Programming Language Concepts /12 2 Regular Expression s /12 3 Ruby execution /10 4 Ruby Programming /18 5 OCaml Typing /12 6 OCaml Execution /18 7 OCaml Programming /18 Total /100 1

1) Programming Language Concepts (12 pts) a) (3 pts) Which of the following statements are true of higher-order functions? Circle all that apply. i) They are functions that run in higher than O(n) time ii) They are functions that can return other functions iii) They are necessary for object-oriented programming iv) They are functions that receive other functions as arguments v) map and fold are examples of higher order functions b) (3 pts) Show the contents of the closure for g after executing the following code let f = (fun x-> (fun y-> (fun z -> x * z)));; let g = f 10 20;; Code Environment c) (6 pts) Circle T (true) or F (false) for the following statements (1pt each) i) T / F OCaml tuples are homogeneous. ii) T / F With immutable state, aliasing is irrelevant. iii) T / F Methods can be overloaded in Ruby. iv) T / F Ruby is a compiled language. v) T / F For OCaml, type inference happens at compile-time. vi) T / F Ruby has static typing. 2

2. Regular Expression s (12 pts) a) (3 pts) Circle all of the strings that match the Ruby regular expression /^\d{3}.\d{4}$/ i) 732.7444 ii) 908-5490 iii) 201-901 iv) ddd.dddd b) (3 pts) What is the output of the following Ruby code? Why was 6 afraid of 7? =~ /\d\s(\w+).*(\d)/ puts $1 puts $2 c) (3 pts) Write the output of the following code. (Recall that a.inspect gives the representation of a as it would appear in source code, e.g. [1,2,3].inspect is "[1,2,3]".) s = To be, or not to be! a = s.scan(/(\s+) (\S+)/) puts a.inspect d) (3 pts) A phone company needs to match serial numbers. The format is either 2 or 4 alphabetic characters followed by 1 to 3 numeric characters. Write the a Ruby regular expression which matches these serial numbers. Example valid Inputs at7 op115 uaqt345 Example invalid Inputs 76n k997 abcde123 3

3. Ruby Execution (10 pts) Write the output of the following Ruby code. If there is an error, then write FAIL. a) (3 pts) arr = [true,nil,false,0] output: arr.each do x if x then puts Ruby else puts Crystal b) (4 pts) class JellyBean def initialize @@flavors = ["blue", "red", "purple", "yellow"] def foo(f) @@flavors.push f def flavors @@flavors j = JellyBean.new() k = JellyBean.new() j.foo("orange") k.foo("rainbow") puts j.flavors.inspect c) (3 pts) h = { "a"=>4, "b"=>3, "c"=>2, "d"=>1 } r = h.values.select { x x % 2 == 0 } r.sort! puts r.inspect 4

4. Ruby Programming (18 pts) For this question, you implement a memory game in class MemoryGame. In the game, a player picks two cards (each an object of class Card ) from a deck (an object of class Deck ). If the two cards have the same face value (regardless of suit), they are marked as matched. Players may not match cards that have been matched previously. Players can play until every card is matched. We provide starter code for the Card and Deck classes below. Notice that: Suits are strings :"Spades, "Clubs", "Hearts", "Diamonds" Values are strings or integers : "Ace", "King", "Queen", "Jack", 2, 3, 4, 5, 6, 7, 8, 9, 10 the deck is shuffled when it is created. class Card attr_accessor :suit, :value def initialize(s, v) @suit = s @value = v... # you will add a method here class Deck def initialize @cards = [] suits = ["Spades", "Clubs", "Hearts", "Diamonds"] values = ["Ace", "King", "Queen", "Jack", 2, 3, 4, 5, 6, 7, 8, 9, 10] values.each { v suits.each { s c = Card.new(s,v) @cards.push(c) } } @cards.shuffle! # randomly permutes the cards... # you will add two methods here 5

a) (6 pts) Write the self.from_string(s) static method for the Card class. This method receives a string in value suit format (for example: 10 Clubs, Queen Spades ), and creates a card with that value and suit. It returns nil if the given string is not a valid value and suit. def self.from_string(s) # of self.from_string method # of Card class... b) (2 pts) Write the method cardat(n) for the Deck class. It returns the card at position n (where indexing starts at 0). Returns nil if there is no card at that position. def cardat(n) # of Deck s cardat method c) (2 pts) Write the method numcards for the Deck class, which returns the number of cards in the deck def numcards # of Deck s numcards method # of Deck class 6

d) (8 pts) Write the MemoryGame class, which implements the memory game described above. The MemoryGame constructor creates a fresh Deck and stores it in field @d. You must implement method match(n1,n2), which checks if two cards at positions n1 and n2 in Deck @d have the same values (irrespective of the suit). You may need to add code to the constructor. match(n1,n2) returns true if the cards at positions n1 and n2 in Deck @d have the same value and have not been matched before (i.e., match(n1,n2) has not previously returned true ) match(n1,n2) returns false if n1 == n2, or n1 or n2 are not valid positions in the Deck @d, or the cards at n1 and n2 do not have the same value, or the cards at n1 and n2 were part of a previous match class MemoryGame def initialize # hint: add a data structure to keep track of matched cards @d = Deck.new # of initialize def match(n1,n2) # of match method # of MatchGame class 7

5. OCaml Typing (12 pts) a) Determine the types of the following OCaml expressions 1. (2 pts) [(1, 3.14, hello )] 2. (2 pts) fun a b c -> if a then (b a) else (c a) 3. (2 pts) let f x m = match m with [] -> x h::_ -> x + h b) Provide an OCaml expression that has the given type. (You may not use type annotations.) 1. (3 pts) int -> int list -> int list 2. (3 pts) `a * `b -> (`b -> `a) -> `a list 8

6. OCaml Execution (18 pts) Write what the following expressions evaluate to. If there is an error, write ERROR and circle the error in the code. Below are implementations of map and fold below for reference (they are equivalent to the standard List.map and List.fold_left functions). let rec fold f a l = match l with [] -> a h::t -> fold f (f a h) t ;; let rec map f l = match l with [] -> [] h::t -> l et r = f h in r::(map f t) ;; a) (3 pts.) let cmsc f x y = if (f x y = 0) then 1 else 0 in cmsc (fun a b -> a - b) 4 4 b) (3 pts.) map (fun x -> x + 2) (6::[1;4;5]) c) (4 pts.) let f l = fold (fun a x -> a x) false l in if f [false; true; false] then 1 else 0 9

d) (4 pts.) e) (4 pts.) let x = 7 in let add a b = a + b in let addto = add x in let x = 40 in (addto 10, add x 10, addto x) let f = let c = ref 0 in fun x -> c :=!c + x;!c in (f 2) + (f 4) 7. OCaml Programming (18 pts) Implement the following functions. You are allowed to add helper functions for any of these problems if you like. a) (6 points) Define a function repeat : int * `a -> `a list that takes a non-negative integer n and a value x and returns a list containing n copies of x. (You may implement this with fold or map, if you wish.) Examples: repeat (0,1) = [];; repeat (2,"hello") = ["hello"; "hello"];; b) (6 points) Define a function unpack l : (int * `a) list -> `a list that takes a list of of pairs and returns a list of the second elements of those pairs, where those elements appear as many times as indicated by the first elements. This is called run length format. (cont d next page) 10

You may not write a recursive function; instead you must use fold and/or map (whose definitions are given in problem 6, above). If you wish, you may use any function in the List or Pervasives library, or anywhere else in this exam, to help. unpack [(1,2);(3,1);(2,5)] = [2;1;1;1;5;5];; unpack [(2,"hello");(0,"fourteen")] = ["hello";"hello"];; c) (6 points) Consider the following variant type definition exp, which represents arithmetic expressions that involve integer constants, multiplication, and addition: type exp = Int of int Mult of exp * exp Plus of exp * exp Define a function eval : exp -> int that takes an expression and returns the int it evaluates to. Examples: eval (Int 3) = 3;; eval (Plus(Int 1,Int 2)) = 3;; eval (Mult(Plus(Int 1,Int 2),Int 3)) = 9;; eval (Mult(Int 2,Plus(Int 1,Int 2))) = 6;; 11

THIS PAGE INTENTIONALLY LEFT BLANK 12