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

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

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

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

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

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

CMSC330 Spring 2016 Final Exam

CMSC330 Spring 2017 Midterm #1 Solution

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

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

CMSC330 Spring 2017 Midterm 2

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

CMSC330 Fall 2017 Final Exam

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

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

CMSC330 Fall 2017 Final Exam Solution

CMSC 330, Fall 2018 Midterm 1

CMSC 330, Fall 2018 Midterm 1

CMSC330 Fall 2016 Final Exam

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

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

CMSC330 Fall 2014 Midterm 1 Grading

CMSC330 Spring 2014 Midterm 2 Solutions

CMSC330 Fall 2014 Midterm 1 Solution

CMSC330 Spring 2008 Final Exam

Problem Score Max Score 1 Syntax directed translation & type

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

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

CMSC330 Fall 2009 Final Exam

CMSC330 Fall 2014 Midterm 2 Solutions

CMSC 330 Exam #2 Fall 2008

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

CMSC 330, Fall 2013, Practice Problem 3 Solutions

Functional Programming. Introduction To Cool

Cunning Plan. One-Slide Summary. Functional Programming. Functional Programming. Introduction to COOL #1. Classroom Object-Oriented Language

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

CSCI-GA Scripting Languages

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

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

CMSC 330, Fall 2018 Midterm 2

CMSC 330, Fall 2018 Midterm 2

CMSC 330: Organization of Programming Languages. Rust Basics

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

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

Structure and Interpretation of Computer Programs

CMSC 330, Fall 2013, Practice Problems 3

CMSC 330: Organization of Programming Languages

CSE341 Spring 2017, Midterm Examination April 28, 2017

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

Name: CIS 120e Midterm I Review

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type.

Functional Programming. Introduction To Cool

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

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

Structure and Interpretation of Computer Programs Spring 2017 Mock Midterm 1

Lecture #23: Conversion and Type Inference

CMSC330 Fall 2014 Final Exam

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

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

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

Anonymous Functions CMSC 330: Organization of Programming Languages

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

Programming Languages and Techniques (CIS120)

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

Qualifying Exam Languages

Functional Programming Mid-term exam Tuesday 3/10/2017

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

Introduction to Algorithms March 11, 2009 Massachusetts Institute of Technology Spring 2009 Professors Sivan Toledo and Alan Edelman Quiz 1

2. You are required to enter a password of up to 100 characters. The characters must be lower ASCII, printing characters.

CSCI-GA Final Exam

Lecture: Functional Programming

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

CMSC330 Fall 2010 Final Exam Solutions

To figure this out we need a more precise understanding of how ML works

CSE 130, Winter 2011: Final Examination

CS Lecture 6: Map and Fold. Prof. Clarkson Fall Today s music: Selections from the soundtrack to 2001: A Space Odyssey

List Functions, and Higher-Order Functions

CS 2316 Exam 1 Spring 2013

CMSC 330: Organization of Programming Languages

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

Programming Paradigms Written Exam (6 CPs)

Background. CMSC 330: Organization of Programming Languages. Useful Information on OCaml language. Dialects of ML. ML (Meta Language) Standard ML

CMSC330 Fall 2016 Final Exam Solution

CS150 Sample Final. Name: Section: A / B

First Midterm Exam CS164, Fall 2007 Oct 2, 2007

CS 340 Spring 2019 Midterm Exam

CS150 Sample Final Solution

Dialects of ML. CMSC 330: Organization of Programming Languages. Dialects of ML (cont.) Features of ML. Functional Languages. Features of ML (cont.

CS 2505 Computer Organization I

Homework 1. Notes. What To Turn In. Unix Accounts. Reading. Handout 3 CSCI 334: Spring, 2017

CS 101 Fall 2006 Midterm 1 Name: ID:

CS 1301 Exam 1 Fall 2010

Recap. Recap. If-then-else expressions. If-then-else expressions. If-then-else expressions. If-then-else expressions

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

CS 11 Haskell track: lecture 1

CIS 120 Midterm II November 8, 2013 SOLUTIONS

Introduction to OCaml

Inductive Data Types

CSE341 Autumn 2017, Final Examination December 12, 2017

CMSC 330: Organization of Programming Languages. Lets, Tuples, Records

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

Transcription:

CMSC330 Spring 2016 Midterm #1 9:30am/12:30pm/3:30pm Solution Name: Discussion Time: 10am 11am 12pm 1pm 2pm 3pm TA Name (Circle): Adam Anshul Austin Ayman Damien Daniel Jason Michael Patrick William 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 /15 2 Regular Expressions /10 3 Ruby Execution /10 4 Ruby Programming /15 5 OCaml Typing /16 6 OCaml Execution /18 7 OCaml Programming /16 Total /100

1. Programming Language Concepts (15 pts) a) (2 pts ) True / False The = operator in OCaml performs structural comparison b) (3 pts) In each part, circle the validity of the statement given the expression: let comb x lst = (x + 5)::lst ;; True / False: OCaml uses type inference to recognize that x must be an int. True / False: comb 2 [8.0;9.0] is valid. Implicit type conversion will cast x + 5 to a float True / False: comb 2 will produce a closure that contains x -> 2. c) (2 pts) Which of the following is NOT true about OCaml (circle all that apply)? i) It is statically typed ii) types are inferred iii) it doesn t have garbage collection iv) it evaluates x before it evaluates y in let x = <code> in y d) (2 pts) Which of these is true about Ruby (circle all that apply)? i) Ruby is a compiled scripting language. ii) Ruby uses dynamic typing. iii) Ruby is a functional programming language. iv) Ruby uses explicit declarations. e) (2 pts) Which of these is a difference between Ruby and Java (circle all that apply)?? i) Ruby is statically typed while Java is not. ii) Java is object-oriented while Ruby is not. iii) Java is a scripting language while Ruby is not. iv) Ruby supports implicit declarations while Java doesq not. f) (2 pts) Which of the following features help OCaml reduce the probability of errors occurring at runtime (circle all that apply)? i) Static Typing ii) Currying iii) Modules iv) Type Inference

g) (2 pts) What is a typical advantage of a compiled language over an interpreted language (circle all that apply)? i) Improved performance ii) Shorter development time iii) Manual Memory Management iv) Write Once, Run Anywhere (ie. The same code will just work on most platforms) v) Improved type safety 2. Regular Expressions (10 pts) 1. (3 pts) Describe the strings matched by the following regex: /^(0(0 1)*0) (1(0 1)*1)$/ Solution: Binary strings that start and with 0 or start and with 1 +1 - Binary strings or mention that strings only contain 0,1 +1 - Strings can start and with 0 +1 - Strings can start and with 1 2. (3 pts) What is the output of the following Ruby code: if Email: eggplant_lover512@veggies.com =~ /\w+(@\w+)\.com/ then puts $1 else puts No match Solution: @veggies 3. (4 pts) Write a regular expression that matches with types of OCaml tuples that only consist of ints and floats. Note that the * symbol needs to be escaped in regexes. You may assume that the input strings will contain no whitespace, and have at least two types. Example Matches: int*float int*int*int float*int*float*int Solution: /^(int float)(\*(int float))+$/ or /^((int float)\*)+(int float)$/

3. Ruby Execution (10 pts) 1) (4 pts) What is the output (if any) of the following Ruby programs? If the code does not execute, write FAIL. h ={"hello" => "salut", "bye" => "au revoir"} y = h.keys.collect { k k + "->" + h[k]} y.sort! puts y[0] Answer bye->au revoir 2) (3 pts) class Animal def initialize(height, weight) @@height = height @weight = weight def measure() return @@height + @weight giraffe = Animal.new(1,2) elephant = Animal.new(3,4) puts giraffe.measure() puts elephant.measure() Answer 5 7 3) (3 pts) a = [] a[1] = "Strawberry" a[3] = "Tomato" a.each { x if x puts "Fruit" else puts "Veggie" }

Answer Veggie Fruit Veggie Fruit 4. Ruby Programming (15 pts) When applications or websites require password authentication, they must store the passwords associated with each user. However, storing the actual passwords without any encryption is a critical security hazard. Instead, the passwords are securely hashed (encrypted), so that even if the database is compromised, attackers cannot read the passwords. A password hash (encryption) is valid according to our (bad) algorithm if: It is 10-character long It includes no whitespace The first character is h The last two characters are == It contains at least one uppercase letter It contains at least one number You are writing functions for a class named HashReader. It will read from text file, consisting of username and password_hash pairs in the form of <username>;<password_hash>. You can assume all the username/password_hash pairs are in valid form each line contains exactly one pair all the usernames are distinct Implement the following functions (5 pts each): initialize(filename): read all the user/password_hash data from text file, and store it in an appropriate data structure. In Ruby IO.foreach opens a file calls the given block for each line it reads and closes the file afterwards. You don't have to worry about closing the file. For example: IO.foreach("file") { line puts line } is_valid(password_hash): Given a password_hash, return true if the hash_value meets the requirements for a valid hash given above. Otherwise, return false. You have to use regex for validation. iter(): Takes in a code block and passes each username / password_hash pair to the block. Iterate over each username / password. users.txt: alice;h9lnffmk== bob;h9lnffmk== cathy;h12345fh== dinosaurav;h867f309== Example usage:

a = HashReader.new("users.txt") puts a.is_valid("this is a password==") a.iter { username, hash puts "#{username};#{hash}" } Output: h9lnffmk== false alice;h9lnffmk== bob;h9lnffmk== cathy;h12345fh== dinosaurav;h867f309== class HashReader def initialize(file) @hash = Hash.new() IO.foreach(file) { line if line =~ /^(.*);(.*)$/ #will always match @hash[$1] = $2 } def get_record(user) return @hash[user] def is_valid(pass) if pass.length == 10 && pass =~ /[A-Z]/ && pass =~ /\d/ && pass =~ /^h[^ ]*==$/ return true return false def iter() @hash.each { k,v yield k,v } Grading Breakdown:

5. OCaml Typing (16 pts) 1) (6 pts) Without using type annotations, write an expression for each of the following types: (No partial credit) i) int list -> int ii) iii) 'a -> 'a -> bool (int -> int) -> (int -> int) -> int 2) What is the type of the following function given the following data type? a) (4 pts) type 'a atree = Leaf Node of 'a * 'a atree * 'a atree ;; let rec apply t f= match t with Leaf -> t Node(x, l, r) -> Node(f x, apply l f, apply r f) ;; a atree -> ( a -> a) -> a atree 2 points off for a atree -> ( a -> b) -> b atree 0 points for anything else (unless it seems fairly logical, since I can t think of what else they might put) b) (3 pts) let mult_and_div x y = (x *. y, x /. y) float -> float -> float * float c) (3 pts) [(4, Hello );(5, World )] int * string list

6. OCaml Execution (18 pts) 1) (4 pts) What is r bound to at the of the following code? If there is an error, mark the line and write ERROR with a brief explanation. let v = 2;; let f a b = a * b;; let f = f v;; let v = 5;; let r = (f 10, f v 10);; Answer: (20,50) 2) (4 pts) What does the following statement evaluate to? Refer to the definition of fold provided. let rec fold f a l = match l with [] -> a h::t -> fold f (f a h) t ;; fold (fun (a1,a2) v -> (a1 + v*2, a2 + v/2)) (0,0) [2;4;7];; Answer: (26,6) 3) (4 pts) What is r bound to at the of the following expression? let rec apply_two d f1 f2 = f2 (f1 d);; let data = [1;2;3];; let r = apply_two data (map (fun x->10*x)) (fold (fun a x->a+x) 100);; Answer: 160 4) (4 pts) We define a function inc as follows: let inc = let x = ref 0 in fun y -> x :=!x + y;!x What is the value of the following expression? If there is an error, write ERROR and give a brief description of the problem.(use the map given in Question 7, next page) map inc [1; 2; 3; 4] Answer: [1; 3; 6; 10]

7. OCaml programming (16 pts) Implement any two of the following three problems. (If you do all three, all will be graded, and the result scaled to be out of 16 points) 1) Use fold and map to write a function, f, that takes one argument, a list of lists of ints, and outputs a list of ints, replacing each of the list of ints inside the original list with their respective sums. Eg: f [[1;2];[];[4,5,3]] evaluates to [3;0;12]. Ensure that order of sums conforms to the order of int list in the original list of list of ints. Solution: let f list = map (fold (fun a h -> a + h) 0) list;; Alternate solution: let f list = fold (fun a h -> a@(fold (fun a1 h1 -> a1 + h1) 0 h)) [] list;; 2) Partition: Write a recursive OCaml function which takes a list and a predicate function and returns a tuple of 2 lists. The first list is all of the elements of the argument list which satisfy the predicate, while the second list is all of the elements which do not satisfy the predicate. The type of partition is the following: a list -> ( a -> bool) -> a list * a list e.x. partition [-1;-5;2;-6;5] (fun x -> if x > 0 then true else false) ([2;5], [-1;-5;-6]) also acceptable: ([5;2], [-6;-5;-1]) Solution: let partition list pred = let rec p_helper l p (a,b) = match l with [] -> (a,b) h::t -> if p h then (h::a, b) else (a,h::b) in p_helper list pred ([],[]) ;; Alternate solution: let partition list pred = fold (fun (a,b) h -> if p h then (h::a,b) else (a,h::b)) ([],[]) list;;

3) Define a recursive function merge that takes two sorted lists as arguments and returns a sorted list that is the result of merging the two lists. let rec merge xs ys = match xs, ys with [],_ -> ys _,[] -> xs (h1::tl1), (h2::tl2) -> let rest = merge tl1 tl2 in if (h1 > h2) then h2::h1::rest else h1::h2::rest