CMSC 330, Fall 2018 Midterm 1

Similar documents
CMSC 330, Fall 2018 Midterm 1

CMSC 330, Fall 2018 Midterm 2

CMSC 330, Fall 2018 Midterm 2

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

CMSC330 Fall 2017 Final Exam

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

CMSC330 Spring 2017 Midterm #1 Solution

CMSC330 Fall 2017 Final Exam Solution

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

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

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

Part 1 (80 points) Multiple Choice Questions (20 questions * 4 points per question = 80 points)

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

Problem Score Max Score 1 Syntax directed translation & type

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

CMSC330 Fall 2014 Midterm 1 Solution

CMSC330 Fall 2014 Midterm 1 Grading

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

CMSC330 Spring 2017 Midterm 2

CMSC 330 Exam #2 Fall 2008

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

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

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

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

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

CMSC330 Fall 2014 Midterm 2 Solutions

CS 1301 Exam 1 Fall 2010

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

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

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

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

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

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.

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

CSE 131 Introduction to Computer Science Fall 2016 Exam I. Print clearly the following information:

Programming Paradigms Written Exam (6 CPs)

CS 321 Programming Languages

CSE 130, Fall 2006: Final Examination

CMSC330 Fall 2014 Final Exam

CMSC330 Fall 2009 Final Exam

CS 1301 Exam 1 Fall 2010

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

CSE 130, Fall 2005: Final Examination

CMSC330 Spring 2016 Final Exam

Computer Science CS221 Test 2 Name. 1. Give a definition of the following terms, and include a brief example. a) Big Oh

CMSC 330: Organization of Programming Languages

CIS 110 Introduction to Computer Programming 8 October 2013 Midterm

CSE 131 Introduction to Computer Science Fall Final Exam

CMSC 330: Organization of Programming Languages. Rust Basics

CS 1301 Exam 1 Fall 2014

CS 1301 Exam 1 Fall 2014

In addition to the correct answer, you MUST show all your work in order to receive full credit.

FUNCTIONAL PROGRAMMING

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

CS 2316 Exam 4 Fall 2011

CS 2150 Final Exam, Spring 2018 Page 1 of 10 UVa userid:

CSE341 Autumn 2017, Final Examination December 12, 2017

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

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

School of Computer Science

Structure and Interpretation of Computer Programs

CS112 Spring 2012 Dr. Kinga Dobolyi. Exam 2. Do not open this exam until you are told. Read these instructions:

# true;; - : bool = true. # false;; - : bool = false 9/10/ // = {s (5, "hi", 3.2), c 4, a 1, b 5} 9/10/2017 4

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

Case by Case. Chapter 3

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

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

CMSC 330: Organization of Programming Languages

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Booleans (aka Truth Values) Programming Languages and Compilers (CS 421) Booleans and Short-Circuit Evaluation. Tuples as Values.

COS 126 General Computer Science Spring Written Exam 1

CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

COMP 401 Midterm. Tuesday, Oct 18, pm-3:15pm. Instructions

CSE 131 Introduction to Computer Science Fall Exam I

CSE341 Spring 2016, Midterm Examination April 29, 2016

CIS 120 Midterm II March 29, 2013 SOLUTIONS

CPSC 211, Sections : Data Structures and Implementations, Honors Final Exam May 4, 2001

CSE341 Spring 2016, Midterm Examination April 29, 2016

CS 111X - Spring Final Exam - KEY

CSE Programming Languages Final exam - Spring Answer Key

Programming Paradigms Written Exam

COMP 524 Spring 2018 Midterm Thursday, March 1

CMSC330 Fall 2016 Final Exam

CSCI-GA Scripting Languages

CS 1044 Programming in C++ Test 1 READ THIS NOW! Failure to read and follow the instructions below may result in severe penalties.

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

CS 11 Ocaml track: lecture 2

CSE 247 Data Structures and Algorithms Fall Exam I

CS150 Sample Final. Name: Section: A / B

CMSC 313 Fall2009 Midterm Exam 1 Section 01 October 12, 2009

Forward recursion. CS 321 Programming Languages. Functions calls and the stack. Functions calls and the stack

Section 1: True / False (2 points each, 30 pts total)

CSE341 Spring 2016, Final Examination June 6, 2016

Structure and Interpretation of Computer Programs

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

CS 1301 Exam 3 Fall 2014

CS 2316 Exam 1 Spring 2013

CSE341 Autumn 2017, Midterm Examination October 30, 2017

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

CS 3330 Exam 1 Fall 2017 Computing ID:

Transcription:

CMSC 330, Fall 2018 Midterm 1 Name Teaching Assistant Kameron Aaron Danny Chris Michael P. Justin Cameron B. Derek Kyle Hasan Shriraj Cameron M. Alex Michael S. Pei-Jo Instructions Do not start this exam until you are told to do so. You have 75 minutes for this exam. This is a closed book exam. No notes or other aids are allowed. For partial credit, show all your work and clearly indicate your answers. Honor Pledge Please copy and sign the honor pledge: I pledge on my honor that I have not given or received any unauthorized assistance on this examination. Section Points Programming Language Concepts 10 Ruby Regular Expressions 10 Ruby Execution 12 Ruby Programming 18 OCaml Typing 17 OCaml Execution 15 OCaml Programming 18 Total 100 1

Programming Language Concepts 1. [1 pts] (T / F) In every programming language, code must be compiled before it is run. False. 2. [1 pts] (T / F) Static typing occurs during program execution and dynamic typing occurs before the program is run. False. In the following questions, circle all answers that apply. 3. [2 pts] In Ruby, which of the following are objects? (a) true (b) Hash.new (c) 2 (d) [1] (a), (b), (c), (d) 4. [2 pts] In OCaml, which of the following are true about functions? (a) They can take other functions as arguments. (b) They have to be given a name to be used. (c) They will throw an error if not given enough arguments. (d) They can return another function as an output. (a), (d) 5. [2 pts] Which of the following is stored in a closure? (a) the execution stack (b) the function s output (c) the environment (d) the function s code (c), (d) 6. [2 pts] Which of the following fit the functional programming paradigm? (a) loops (b) recursion (c) higher-order functions (d) mutable variables (b), (c) 2

Ruby Regular Expressions 1. [3 pts] Give the output of the following code snippet. secret = "<a href='umd.edu' user='cmsc330' />" result = secret.scan(/([a-z]+)(=)/) puts(result) [["href", "="], ["user", "="]] 2. [2 pts] Given the regular expression /Oh. What's that?b*/ circle all of the strings that are matched entirely. (Hint:? means zero or one.) (a) Oh. What s thatb (b) Oh. What s that? (c) Oh! What s that (d) Oh. What s thabb (a), (c), (d) 3. [2 pts] What is the output of the following? "href=https://www.ign.com/ps4" =~ /.{3}\/([a-z]+\d)/ puts($1) ps4 4. [3 pts] Write a Ruby regular expression to match room numbers. Room numbers are a building code (three capital letters), followed by a space, followed by a room code (four digits, possibly with a B in front). Examples are given below. ESJ 0202 CSI 1115 MTH B0421 /[A-Z]{3} B?\d{4}/ 3

Ruby Execution Next to each Ruby snippet, write the output after executing it. If there is an error, then write error. 1. [2 pts] x = {"CMSC330" => 1, "CMSC351" => 2, "CMSC320" => 3} x["cmsc216"] = 4 if x.key?("cmsc216") then x["cmsc330"] = x["cmsc216"] puts(x["cmsc330"]) 4 2. [2 pts] i = 0 x = Hash.new(1) 3.times do x[i] += 1 i += 1 puts(x) {0 => 2, 1 => 2, 2 => 2} 3. [2 pts] x = [1, 2, 3] x << "four" puts(x) 4

[1, 2, 3, four ] 4. [3 pts] def fun(y) i = 1 while i <= y yield(i * i) i += 1 fun(4) { x } puts(x) 1 4 9 16 5. [3 pts] class Foo @@y = [] def initialize(ele) @@y.push(ele) def add(ele) @@y.push(ele) @@y 5

f = Foo.new(12) g = Foo.new("a") h = g.add("e") h[3] = "i" puts(g.add("o")) [12, "a", "e", "i", "o"] 6

Ruby Programming You re in charge of building a Ruby class for a bank that deals exclusively in the tech world s hottest new currency: UMDCash. You need to implement the Bank class in order to support this hot new economy and make the teaching staff very rich. The Bank class must store the account names and each account s current tally of UMDCash. You can assume that (a) each account name is distinct, and (b) there are no negative amounts ever provided to your code. 1. [2 pts] Implement initialize. You will have to decide the proper data structure(s) for storing the data needed for the rest of the outlined functions. 2. [8 pts] Implement importaccounts that takes the name of a file and loads accounts and their existing UMDCash tally. The file has one account per line, in the format name:amount where the name consists of (upper and lowercase) letters and the amount is an integer or float. If the same account appears more than once in the file, store only the highest value. If any line doesn t follow this format, the line is invalid and should be skipped. Multiple calls to importaccounts will update existing balances. Names are NOT case sensitive (i.e. bob and Bob will be treated as the same name). Here is an example. b = Bank.new b.importaccounts("file.txt") Here is the contents of file.txt. Bob:10 Alice:48.50 McKenzie:33 Samantha:28.01 Kyle:Fifty # Invalid Line You may find it useful to use the IO.foreach method, that reads files. IO.foreach("myfile.txt") { line puts line } 3. [4 pts] Implement transfer that takes in an account name from, account name to, and a transfer amount. It returns true if the transaction succeeds, or false if either of the accounts do not exist or the from account doesn t have enough UMDCash to complete the transfer. If false is returned, neither account s balance should change. 4. [4 pts] Implement whoistherichest that returns the name of the account in your bank who currently has the highest UMDCash total. Return nil if there are currently no accounts in your bank. 7

class Bank def initialize ledger = Hash.new(0) def importaccounts(file) IO.foreach(file) do line if line =~ /^([A-Za-z]+):(\d+(\.\d+)?)/ then ledger[$1] = [ledger[$1], $2.to_f].max def transfer(from, to, amount) if not (ledger.key?(from) and ledger.key?(to) and ledger[from] >= amount) then return false ledger[from] -= amount ledger[to] += amount true def whoistherichest() pair = ledger.max_by { _, amt amt } pair[0] if pair 8

OCaml Typing Recall the definition of 'a option. type 'a option = Some of 'a None Determine the type of the following expressions. If there is an error, write error. 1. [3 pts] fun a -> if a then None else (Some a) bool -> bool option 2. [3 pts] fun a b c -> (a b) +. c > 0.0 ('a -> float) -> 'a -> float -> bool Write an expression that has the following type, without using type annotations. 3. [3 pts] int -> int option -> int let f a b = match b with None -> a Some x -> a + x 4. [3 pts] ('a -> 'b -> 'a * 'b) -> 'a -> 'b -> ('a * 'b) list let f a b c = (a b c) :: [(b, c)] Define functions f that when used in the following expressions will not produce any type errors. 5. [3 pts] fun g t -> [g t; f g t] fun g t -> g t 6. [2 pts] fun x -> (f x) :: [x] fun x -> x 9

OCaml Execution Recall the definitions of map, fold_left, and fold_right. let rec map f xs = match xs with [] -> [] x :: xt -> (f x) :: (map f xt) let rec fold_left f a xs = match xs with [] -> a x :: xt -> fold_left f (f a x) xt let rec fold_right f xs a = match xs with [] -> a x :: xt -> f x (fold_right f xt a) Write the final value of the following OCaml expressions next to each snippet. If there is an error, write error. 1. [2 pts] let y = 3 in let x = y + 1 in let y = 5 in x 4 2. [2 pts] let f x y = if x > y then x - y else (if x < y then y - x) f 3 2 Error. 3. [3 pts] let f a = match a with (a1, a2) -> a1 * a2 map f [(2, 2); (1, 4); (3, 2)] [4; 4; 6] 10

4. [4 pts] fold_left (fun a x -> if x mod 3 = 0 then x :: a else a) [] [1; 3; 11; 27] [27; 3] 5. [4 pts] let rec f l = match l with [] -> [] h :: t -> (fold_left (fun a x -> a * x) 1 t) :: (f t) in f [1; 2; 3; 4] [24; 12; 4; 1] 11

OCaml Programming For the following questions you may use map, fold_left, and fold_right. 1. [8 pts] Write sum_thresh : int list -> int -> (int * int) that returns a tuple where the first component is the sum of all elements in xs strictly less than thresh, and the second component is the sum of all the elements in xs greater than or equal to thresh. Examples: sum_thresh [5; 2; 7; 3] 4 = (5, 12) sum_thresh [1; 2; 10; 5] 5 = (3, 15) let sum_thresh xs thresh = fold_left (fun (a1, a2) x -> if x < thresh then (a1 + x, a2) else (a1, a2 + x)) (0, 0) xs 2. [10 pts] Write compress: 'a list -> ('a * int) list that packs in consecutive duplicate elements together in a tuple (a, b), where a is the element and b is the number of consecutive occurrences. Examples: compress [] = [] compress ["a"; "a"] = [("a", 2)] compress ["a"; "a"; "b"; "c"; "b"; "b"] = [("a", 2); ("b", 1); ("c", 1); ("b", 2)] let compress xs = fold_left (fun a x -> match a with [] -> [(x, 1)] (ch, cnt) :: xt -> if ch = x then (ch, cnt + 1) :: xt else (x, 1) :: (ch, cnt) :: xt) [] xs 12