CMSC330 Spring 2008 Final Exam

Similar documents
CMSC330 Fall 2009 Final Exam

CMSC330 Fall 2013 Practice Problems 6 Solutions

CMSC330 Spring 2012 Final Exam Solutions

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

CMSC330 Fall 2010 Final Exam Solutions

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

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

CMSC330 Spring 2017 Midterm 2

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 2 Solutions

Problem Score Max Score 1 Syntax directed translation & type

CMSC 330: Organization of Programming Languages

CMSC330 Spring 2014 Midterm 2 Solutions

CMSC330 Fall 2014 Final Exam

CMSC330 Fall 2014 Midterm 1 Solution

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

CMSC 330, Fall 2018 Midterm 2

CMSC 330, Fall 2013, Practice Problems 3

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

CMSC330 Fall 2017 Final Exam

CMSC330 Fall 2014 Midterm 1 Grading

Semantic Analysis. Lecture 9. February 7, 2018

CS 415 Midterm Exam Spring 2002

CMSC 330, Fall 2018 Midterm 2

CMSC 330, Fall 2013, Practice Problem 3 Solutions

CMSC330 Spring 2016 Final Exam

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

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

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

CMSC330 Fall 2016 Final Exam

CSE3322 Programming Languages and Implementation

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

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

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

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

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

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

Midterm I - Solution CS164, Spring 2014

CSCI-GA Scripting Languages

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

CMSC 330: Organization of Programming Languages. Operational Semantics

CSE 401 Midterm Exam Sample Solution 2/11/15

Comp 311: Sample Midterm Examination

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

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

CMSC330 Fall 2017 Final Exam Solution

Midterm 1. CMSC 430 Introduction to Compilers Spring Instructions Total 100. Name: March 14, 2012

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

COP4020 Spring 2011 Midterm Exam

CS 415 Midterm Exam Spring SOLUTION

MIDTERM EXAMINATION - CS130 - Spring 2005

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

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Midterm I (Solutions) CS164, Spring 2002

Compiler Design Aug 1996

CSE341, Spring 2013, Final Examination June 13, 2013

First Midterm Exam CS164, Fall 2007 Oct 2, 2007

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

CMSC 330 Exam #2 Fall 2008

Theory of Computation Dr. Weiss Extra Practice Exam Solutions

NAME: c. (true or false) The median is always stored at the root of a binary search tree.

ML Built-in Functions

UMBC CMSC 331 Final Exam

2. Reachability in garbage collection is just an approximation of garbage.

CSE 582 Autumn 2002 Exam 11/26/02

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

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

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

CMSC411 Fall 2013 Midterm 1

Computer Science Foundation Exam

CMSC330 Spring 2017 Midterm #1 Solution

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

Examples of attributes: values of evaluated subtrees, type information, source file coordinates,

CS164: Midterm I. Fall 2003

CMSC330 Fall 2016 Final Exam Solution

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Review. Pat Morin COMP 3002

Talen en Compilers. Jurriaan Hage , period 2. November 13, Department of Information and Computing Sciences Utrecht University

MASSACHUSETTS INSTITUTE OF TECHNOLOGY Fall Test I

CSE341 Spring 2017, Final Examination June 8, 2017

CSE3322 Programming Languages and Implementation

Torben./Egidius Mogensen. Introduction. to Compiler Design. ^ Springer

CIS 120 Midterm I February 16, 2015 SOLUTIONS

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

CSE 5317 Midterm Examination 4 March Solutions

Type Inference Systems. Type Judgments. Deriving a Type Judgment. Deriving a Judgment. Hypothetical Type Judgments CS412/CS413

CSE 130, Fall 2005: Final Examination

CMSC 330: Organization of Programming Languages. Objects and Abstract Data Types

CMSC330. Objects, Functional Programming, and lambda calculus

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

Compiler Construction Lent Term 2015 Lectures 10, 11 (of 16)

CSE341 Spring 2017, Final Examination June 8, 2017

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

Principles of Programming Languages (2016F)

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

Question Bank. 10CS63:Compiler Design

UNIVERSITY OF CALIFORNIA

Part I: Multiple Choice Questions (40 points)

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

Transcription:

CMSC33 Spring 28 Final Exam Do not start this exam until you are told to do so! Instructions You have 2 minutes to take this midterm. This exam has a total of 24 points, so allocate 3 seconds for each point. There is also a 6 point extra credit problem if you finish early. This is a closed book exam. No notes or other aids are allowed. If you have a question, please raise your hand and wait for the instructor. Answer essay questions concisely using sentence, or at most 2 sentences. Longer answers are not necessary and a penalty may be applied. In order to be eligible for partial credit, show all of your work and clearly indicate your answers. Write neatly. Credit will not be given for illegible answers. Good luck!

. (9 pts) Programming languages a. (3 pts) Give an example of a language feature (not including library functions) in Ruby & OCaml whose syntax is different but semantics are the same. b. (3 pts) Give an example of a language feature (not including library functions) in Ruby & Java whose syntax is the same but semantics are different. c. (3 pts) Describe a feature of an old programming language which proved vexing for programmers. 2. (2 pts) Ruby features What is the output (if any) of the following Ruby programs? Write FAIL if code does not execute. a. a = [2,3] a[3] = c a.push( b ) puts a b. if CMSC 33 =~ /([-9]+)/ then puts $ else puts Error end c. s = "CMSC 33 is too HARD!" s.scan(/[a-z]+/) { x puts x } d. h = {2 => 3, => 2, =>} puts "#{h[]} #{h[2]}" 3. (2 pts) Ruby programming Consider the following programming problem. We need to read from a text file a list of cars and the years they were produced. The file contains a number of lines of the form <car> <year>, where <car> is composed of letters, <year> is composed of digits, and the two are separated by a single space. Write a Ruby program to read in the list of cars and their model years, and output the list of cars and years in sorted order, with each car on a separate line. Any lines not in the correct format should be ignored. Skeleton Code file = File.new(ARGV[], "r") until file.eof? do line = file.readline end // a = Array.new ; a.sort! // print ; puts 2 Example Input Example Output yaris 28 camry 983 prius 24 prius 24 26 27 prius 27 yaris 26 28 rav4 998 yaris 26 prius 26 camry 983

4. (3 pts) Regular expressions finite automata a. (6 pts) Reduce the regular expression ()* to an NFA, using the algorithm shown in class. b. (2 pts) Reduce the NFA to a DFA using the subset algorithm. Show the NFA states represented by each DFA state. c. (2 pts) Minimize the following DFA using Moore s algorithm. Show the entire sequence of partitions created (list the DFA states in each partition), and explain why each partition was split. 2 5 3 4 5. ( pts) Grammars and parsing a. (4 pts) Write a grammar for a x b y a z, where x = y z, for x,y,z b. (6 pts) Rewrite the following grammar S S and S not S true false so that and is right associative & has higher precedence than not. 6. (24 pts) OCaml Types & Type Inference Give the type of the following OCaml expressions: a. (3 pts) let f x = match x with [] -> [] (h::_) -> h b. (3 pts) let f x y = y (x+) Write an OCaml expression with the following types: c. (3 pts) int -> (int -> 'a) -> 'a d. (3 pts) (int -> 'a) -> int -> 'a What are the values of the following OCaml expressions? If an error exists, describe the error. e. (3 pts) let x = x in f. (3 pts) let x = in if x > then x else print_string Error g. (3 pts) (fun x -> if x > then x else ( x)) 2 h. (3 pts) Are type inference and strong typing orthogonal language features? Explain.

7. (4 pts) OCaml Polymorphic Types Consider a OCaml module Bst that implements a binary search tree: module Bst = struct type bst = Empty Node of int * bst * bst let empty = Empty (* empty binary search tree *) let is_empty = function (* return true for empty bst *) Empty -> true Node (_, _, _) -> false (* Implement the following functions val height : bst -> int val post_fold : ('a -> int -> 'a) -> 'a -> bst -> 'a *) let rec height = (* height of bst *) let rec post_fold f a t = (* apply f to nodes of t in postorder *) end a. (6 pts) Implement height. You may use Pervasives.max (a -> 'a -> 'a), which returns the greater of its two arguments. b. (8 pts) Implement post_fold as a version of fold which performs a postorder traversal of the tree (visits in order : left subtree, 2: right subtree, 3: node). Example: let s = Node(9,Node(5,Node(3,empty,empty),empty),Node(2,empty,empty)) ;; height s ;; (* returns 3 *) post_fold (fun a m -> m::a) [] s (* returns [3;5;2;9] *)

8. (2 pts) Recursive Descent Parser in OCaml The example OCaml recursive descent parser 5-parseArith_fact.ml employs a number of shortcuts. For instance, the function parset handles the grammar rules for T F * T F directly instead of rewriting the grammar, creating the following productions: T? C? You must identify where code corresponding to parsec was inserted directly in the code for parset, in the comments below: let rec parset lr = (* parset *) let x = parsef lr in (* :?? *) match!lr with (* parsec *) ('*'::t) -> (* 2: if lookahead = First(? ) *) lr := t; (* 3:?? *) Prod (x,parset lr) _ -> x (* 4:?? *) a. (2 pts) What rule should have been applied to the productions for T? b. (2 pts) What productions for T & C would be created by applying the rule? c. (2 pts) What production should appear in place of? in comment? d. (2 pts) What sentential form should appear in place of? in comment 2? e. (2 pts) What production should appear in place of? in comment 3? f. (2 pts) What production should appear in place of? in comment 4? 9. (2 pts) Function arguments, scoping, & FP vs. OOP a. (4 pts) Explain why upwards funargs are more difficult to implement than downwards funargs. b. (4 pts) Explain why dynamic scoping can be more confusing than static scoping c. (4 pts) Describe how OOP may be used to simulate functional programming.

. (2 pts) Parameter passing a. (2 pts) Describe lazy evaluation. b. (3 pts) Explain how to implement lazy evaluation in a language using call-byvalue. Consider the following C code. void swap(int f, int g) { int tmp = f; f = g; g = tmp; } int main( ) { int i = 2; int a[] = {2,, }; swap(i, a[i]); printf("%d %d %d %d\n", i, a[], a[], a[2]); } c. (2 pts) Give the output if C uses call-by-value d. (2 pts) Give the output if C uses call-by-reference e. (3 pts) Give the output if C uses call-by-name. (8 pts) Polymorphism a. (3 pts) Explain the difference between subtype polymorphism & overloading. b. (3 pts) Explain why subtype polymorphism causes problems for container classes (e.g., Set, Map, Stack). c. (4 pts) Generic programming is used in object-oriented programming languages to solve these problems. Name and describe the technique used to implement generic programming in Java without changing the JVM (Java virtual machine). Consider the following Java classes: class A { } class B extends A { } class C extends A { } HashSet<E> is a class supporting generics in the Java.5 class library. Explain why the following code is or is not legal d. (2 pts) int count(hashset<?> s) { } count(new HashSet<C>( )); e. (3 pts) int count(hashset<? extends A> s) { } count(new HashSet<C>( )); f. (3 pts) int count(hashset<? super B> s) { } count(new HashSet<C>( ));

2. (6 pts) Java multithreading a. Using Java locks & conditions, you must implement a synchronization construct called Partner. A Partner object is created with a value. When a thread calls the method next( ), it waits until a 2 nd thread also calls next( ). The 2 nd thread returns the current Partner value and continues without blocking, waking up the st thread. The next thread to invoke next( ) and acquire the lock for Partner will increment the Partner value and return its previous (non-incremented value). Be careful, since a 3 rd thread may invoke next( ) before the st thread can wake up and be partnered with the 2 nd thread, forcing the st thread to wait for a new partner! class Partner { public int next( ) { } } Partner p = new Partner( ); thread.run( ) { p.next( ); } // blocks & returns after thread 2 calls next( ) thread2.run( ) { p.next( ); } // next( ) returns immediately thread3.run( ) { p.next( ); } // blocks & returns after thread 4 calls next( ) thread4.run( ) { p.next( ); } // next( ) returns immediately 3. (6 pts) Memory allocation & garbage collection Consider the following Java code. public foo(object a[ ] ) { a[] = new Object( ); // object a[] = new Object( ); // object 2 a[2] = new Object( ); // object 3 a[] = a[2]; } a. (3 pts) What object(s) are garbage when foo( ) returns? Explain why. b. (3 pts) Describe why garbage collection may be preferred over manual memory allocation. 4. (8 pts) Markup & query languages a. (5 pts) Creating your own XML tags, write an XML document that organizes the following information: Obama and Hillary are Democrats, McCain is a Republican. b. (3 pts) Explain how query languages are different from programming languages.

5. (2 pts) Lambda calculus & encodings Evaluate the following -expressions as much as possible a. (3 pts) (x.y.x) a b b. (3 pts) (z.y.z y) y z Using the appropriate -calculus encodings c. (6 pts) Given: or = x.y.((x true) y) true = x.y.x false = x.y.y Prove : false or true = true d. (8 pts) Given: = f.y.y = f.y.f y 2 = f.y.f (f y) M + N = x.y.(m x)((n x) y) Prove : (+ 2) = 2 6. (7 pts) Operational Semantics a. (3 pts) Describe the purpose of analyzing the semantics of a program using techniques such as operational semantics. b. (4 pts) Write a proof in operational semantics that (+ a b) has the value 3, if the value of a is and the value of b is 2. 7. (6 pts) Extra credit a. ( pts) Given: Z = f. (x. f (y. x x y)) (x. f (y. x x y)) fact = f. n.if n = then else n * (f (n-)) What happens when you try to evaluate (Z fact)? You do not need to expand if. b. (6 pts) Given Y = f.(x.f (x x)) (x.f (x x)) What is the relationship between Y and Z?