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

Similar documents
CMSC330 Spring 2014 Midterm 2 Solutions

CMSC330 Fall 2014 Midterm 2 Solutions

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

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

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

CMSC330 Spring 2017 Midterm 2

Problem Score Max Score 1 Syntax directed translation & type

CMSC330 Fall 2014 Final Exam

CMSC 330, Fall 2018 Midterm 2

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

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

CMSC 330, Fall 2018 Midterm 2

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 2015 Final Exam 9:30am/11:00am/12:30pm

CMSC330 Spring 2017 Midterm #1 Solution

CMSC330 Spring 2008 Final Exam

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

CMSC330 Fall 2017 Final Exam

CMSC330 Spring 2012 Final Exam Solutions

CMSC 330, Fall 2013, Practice Problems 3

Midterm I (Solutions) CS164, Spring 2002

CMSC330 Fall 2017 Final Exam Solution

CMSC330 Fall 2010 Final Exam Solutions

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

CMSC330 Fall 2009 Final Exam

CMSC 330, Fall 2013, Practice Problem 3 Solutions

CMSC330 Spring 2016 Final Exam

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

CMSC330 Fall 2016 Final Exam

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

Question Points Score

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

CMSC 330: Organization of Programming Languages

MCS-378 Intraterm Exam 1 Serial #:

CS143 Midterm Fall 2008

CMSC 330 Exam #2 Fall 2008

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

First Midterm Exam CS164, Fall 2007 Oct 2, 2007

Question Marks 1 /12 2 /6 3 /14 4 /8 5 /5 6 /16 7 /34 8 /25 Total /120

CMSC 330: Organization of Programming Languages

1. Explain the input buffer scheme for scanning the source program. How the use of sentinels can improve its performance? Describe in detail.

CMSC 330, Fall 2018 Midterm 1

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

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

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

CMSC 330 Practice Problem 4 Solutions

CSE341 Spring 2017, Midterm Examination April 28, 2017

CMSC 330, Fall 2009, Practice Problem 3 Solutions

CMSC 330, Fall 2018 Midterm 1

CSE 130, Fall 2005: Final Examination

Midterm I - Solution CS164, Spring 2014

UMBC CMSC 331 Final Exam

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

CSE341 Spring 2016, Midterm Examination April 29, 2016

CMSC330 Fall 2016 Final Exam Solution

MIDTERM EXAMINATION - CS130 - Spring 2003

CSE341 Spring 2016, Midterm Examination April 29, 2016

University of Maryland College Park Dept of Computer Science

CSE3322 Programming Languages and Implementation

Question Marks 1 /20 2 /16 3 /7 4 /10 5 /16 6 /7 7 /24 Total /100

Computer Science Spring 98 Midterm Examination Professor L. Rowe

ECE251 Midterm practice questions, Fall 2010

CMSC 330: Organization of Programming Languages

1 Parsing (25 pts, 5 each)

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

Question: Total Points: Score:

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

Top down vs. bottom up parsing

Outline. Top Down Parsing. SLL(1) Parsing. Where We Are 1/24/2013

Comp 311: Sample Midterm Examination

CS 101 Fall 2006 Midterm 1 Name: ID:

CSE 131S Introduction to Computer Science Summer SON Exam I

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

Building a Parser III. CS164 3:30-5:00 TT 10 Evans. Prof. Bodik CS 164 Lecture 6 1

CS 415 Midterm Exam Spring 2002

Final Examination: Topics and Sample Problems

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

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

Question Marks 1 /11 2 /4 3 /13 4 /5 5 /11 6 /14 7 /32 8 /6 9 /11 10 /10 Total /117

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

11. a b c d e. 12. a b c d e. 13. a b c d e. 14. a b c d e. 15. a b c d e

CMSC330 Fall 2014 Midterm 1 Solution

CMSC411 Fall 2013 Midterm 1

1. Consider the following program in a PCAT-like language.

ADTS, GRAMMARS, PARSING, TREE TRAVERSALS. Regrades 10/6/15. Prelim 1. Prelim 1. Expression trees. Pointers to material

CS 2505 Computer Organization I

CS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F).

CSE3322 Programming Languages and Implementation

Question: Total Points: Score:

Student Name: University of California at Berkeley College of Engineering Department of Electrical Engineering and Computer Science

University of Virginia Department of Computer Science. CS 4501: Information Retrieval Fall 2015

Midterm Exam #2 April 20, 2016 CS162 Operating Systems

CSE 130, Fall 2006: Final Examination

Computer Security Fall 2006 Joseph/Tygar Midterm 3

UNIVERSITY OF CALIFORNIA

R13 SET Discuss how producer-consumer problem and Dining philosopher s problem are solved using concurrency in ADA.

CSE341, Fall 2011, Midterm Examination October 31, 2011

MIDTERM EXAM (Solutions)

Version B Final Exam

Transcription:

CMSC330 Spring 2014 Midterm #2 Name: Discussion Time 10am 11am noon 1pm 2pm TA Name (circle): Tammy Tammy Tammy Daniel Daniel Ilse Casey Ian 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. You may not use imperative OCaml constructs unless otherwise noted. Problem Score 1 OCaml types & type /16 Inference 2 OCaml higher order & /10 anonymous functions 3 OCaml programming /18 4 OCaml polymorphic /8 datatypes 5 Context free grammars /10 6 Using context free /14 grammars 7 Parsing /15 8 Multithreading /9 Total /100 HONOR PLEDGE: I pledge on my honor that I have not given or received any unauthorized assistance on this assignment/ examination. SIGNATURE:

1. (16 pts) OCaml Types and Type Inference Give the type of the following OCaml expressions: a. (2 pts) fun b -> b + 8 Type = b. (3 pts) fun b -> b 8 Type = Write an OCaml expression with the following type: c. (2 pts) a -> 'a -> 'a list list Code = d. (3 pts) (int -> int) -> (int -> int) Code = Give the value of the following OCaml expressions. If an error exists, describe it e. (3 pts) (fun k t m -> [m; t]) 2 4 6 Value / Error = f. (3 pts) (fun (k::t::m) -> (m, t)) [2;4;6;8] Value / Error =

2. (10 pts) OCaml higher-order & anonymous functions Using either map or fold and an anonymous function, write a curried function process which when given a predicate function p, a compute function f, and a list lst, returns a list of the results of applying f to the elements of lst, but only where p is true for that element of the list. If p is false for an element of the list, it does not contribute to the final result. The relative order of the elements in the new list must the same as in lst. Your function must run in linear time. You may not use any library functions, with the exception of the List.rev function, which reverses a list in linear time. You may not use imperative OCaml (i.e., no ref variables). For example, the results of calling process will be as follows: Example: let p x = x > 2;; let f x = x+1;; process p f [ ] = [ ] process p f [ 1; 2 ] = [ ] process p f [ 1; 3; 2; 4 ] = [ 4; 5 ] let rec map f l = match l with [ ] -> [ ] (h::t) -> (f h)::(map f t) let rec fold f a l = match l with [ ] -> a (h::t) -> fold f (f a h) t

3. (18 pts) OCaml programming Write a function allpairs with type ( a list -> a list list) which given a list lst with n elements, returns a list of all n 2 pairs of elements in lst (in any order), where each pair of elements is in a list of length 2. You may not use any library functions, with the exception of the @ function (which concatenates two lists). You may use helper functions. You may use map or fold, but it is not required. Your function must execute in polynomial time. You may not use imperative OCaml constructs. Examples: allpairs [ ] ;; (* = [ ] *) allpairs [1] ;; (* = [ [1;1] ] *) allpairs [1;2] ;; (* = [ [1;1]; [1;2]; [2;1]; [2;2] ] *) allpairs [1;2;5] ;; (* = [ [1;1]; [1;2]; [1;5]; [2;1]; [2;2]; [2;5]; [5;1]; [5;2]; [5;5] ] *)

4. (8 pts) OCaml polymorphic types Consider the OCaml type bitwiseexp implementing bitwise expressions (think of it as a binary tree where all leaves are 0 s or 1 s). For all answers, your code must work in linear time (i.e., avoid multiple passes over the tree). You are not allowed to use any OCaml library functions except +. You may use helper functions. type bitwiseexp = Zero (* 0 *) One (* 1 *) Pair of bitwiseexp * bitwiseexp (* b 1 b 2 *) bitwiseexp onesexp let x = Zero;; 0 let y = One;; 1 let p = Pair (x,y);; 1 let q = Pair (y,y);; 2 a. (2 pts) Write an OCaml expression with type bitwiseexp that is equivalent to the expression 0 (1 0) b. (6 pts) Write a function onesexp of type (bitwiseexp -> int) that takes a bitwise expression and returns the number of 1 s in the expression.

5. (10 pts) Context free grammars. Consider the following grammar (S = start symbol and terminals = a b % &): S a S%S &T T a b a. (1 pt each) Indicate whether the following strings are generated by this grammar i. %a Yes No (circle one) ii. a%&b Yes No (circle one) iii. &a&b Yes No (circle one) b. (3 pts) Draw a parse tree for the string &b%a c. (4 pts) Is the grammar is ambiguous? Provide proof if you believe it is ambiguous.

6. (14 pts) Using context free grammars. a. (6 pts) Given the following production for B, create a grammar that generates OCaml tuples of strings (for the 3 strings Joe, Laura, and Mike). Your grammar should be able to generate strings such as ( Joe ), ( Joe, Mike ), ( Joe, Laura, Laura ), etc... B Mike Laura Joe b. (8 pts) Consider the following grammar: S c d e S%S S@S &S Modify the grammar above to make the % operator left associative, the @ operator right associative, and make @ have the lowest precedence and & have the highest precedence.

7. (15 pts) Parsing (This question is irrelevant. We did not cover Parsing in this semester.) Consider the following grammar, where S, A, B are nonterminals, and a, b, c, d are terminals. S a bb Ac A SAd epsilon B c b a. (8 pts) Calculate FIRST sets for S, A, and B FIRST(S) = { } FIRST(A) = { } FIRST(B) = { } b. (7 pts) Using pseudocode, write only the parse_a function found in a recursive descent parser for the grammar. You may assume the functions parse_s, parse_b already exist. Use the following utilities: lookahead Variable holding next terminal match ( x ) Function to match next terminal to x error ( ) Reports parse error for input parse_a( ) { // your code starts here

8. (9 pts) Multithreading Consider the following multithreaded Java 1.4 code. Assume there are multiple producer and consumer threads being executed in the program, but only a single Buffer object. Questions about the last statement executed by two threads x & y refer to the most recently executed statement by those threads at some arbitrary time during the program execution. It does not mean the last statement executed by a thread before the thread exits. If a situation is possible, you need to give an example of how it is possible (e.g., thread x gets to statement a, then thread y gets to statement b). If a situation is not possible, you need to explain why. class Buffer { void produce(o) { synchronized (this) { 1. while (...) wait( ); 2.... 3. notifyall( ); 4.... } } Object consume( ) { synchronized (this) { 5. while (...) wait( ); 6.... 7. notifyall( ); 8.... } } } a. (3 pts) Is it possible given two threads x and y for the last statement executed by thread x to be statement 5, and the last statement executed by thread y to be statement 7? Explain your answer. b. (3 pts) Is it possible given two threads x and y for the last statement executed by thread x to be statement 7, and the last statement executed by thread y to be statement 6? Explain your answer. c. (3 pts) Is it possible given two threads x and y for the last statement executed by thread x to be statement 6, and the last statement executed by thread y to be statement 1? Explain your answer.