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

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

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

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

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

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

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

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

CMSC330 Spring 2017 Midterm 2

CMSC330 Fall 2014 Midterm 1 Solution

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

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

CMSC330 Fall 2014 Midterm 1 Grading

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

CMSC330 Spring 2017 Midterm #1 Solution

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

CMSC330 Fall 2017 Final Exam

Midterm I - Solution CS164, Spring 2014

Homework 1 Due Tuesday, January 30, 2018 at 8pm

CS/ECE 374 Fall Homework 1. Due Tuesday, September 6, 2016 at 8pm

CMSC330 Fall 2009 Final Exam

CMSC330 Fall 2017 Final Exam Solution

Problem Score Max Score 1 Syntax directed translation & type

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

CMSC330 Spring 2008 Final Exam

Last lecture CMSC330. This lecture. Finite Automata: States. Finite Automata. Implementing Regular Expressions. Languages. Regular expressions

CMSC 330, Fall 2018 Midterm 2

Theory of Computations Spring 2016 Practice Final Exam Solutions

Midterm I (Solutions) CS164, Spring 2002

Theory of Computation Dr. Weiss Extra Practice Exam Solutions

CS 374 Fall 2014 Homework 2 Due Tuesday, September 16, 2014 at noon

CMSC330 Spring 2016 Final Exam

CMSC330 Fall 2016 Final Exam

CMSC330 Summer 2009 Midterm 1: Ruby and Re~lu[ar Lan~ua~les

Lexical Analysis. Implementation: Finite Automata

CS164: Midterm I. Fall 2003

UMBC CMSC 331 Final Exam

Final Exam. COMP Summer I June 26, points

CMSC 330, Fall 2018 Midterm 2

CMSC 330, Fall 2018 Midterm 1

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

I have read and understand all of the instructions below, and I will obey the Academic Honor Code.

Lexical Analysis. Lecture 3-4

CMSC 330, Fall 2018 Midterm 1

CS402 Theory of Automata Solved Subjective From Midterm Papers. MIDTERM SPRING 2012 CS402 Theory of Automata

Lexical Analysis. Chapter 2

Automating Construction of Lexers

Implementation of Lexical Analysis. Lecture 4

CS164: Midterm Exam 2

Implementation of Lexical Analysis

Lexical Analysis. Lecture 2-4

Lexical Analysis. Lecture 3. January 10, 2018

CSE 105 THEORY OF COMPUTATION

1. (10 points) Draw the state diagram of the DFA that recognizes the language over Σ = {0, 1}

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)

CSE wi Midterm Exam 2/8/18. Name UW ID #

Implementation of Lexical Analysis

Introduction to Lexical Analysis

Theory Bridge Exam Example Questions Version of June 6, 2008

CS143 Final Spring 2016

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

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

CSE450. Translation of Programming Languages. Lecture 20: Automata and Regular Expressions

UMBC CMSC 331 Final Exam

R10 SET a) Construct a DFA that accepts an identifier of a C programming language. b) Differentiate between NFA and DFA?

CS 303E Fall 2011 Exam 2 Solutions and Criteria November 2, Good Luck!

CMSC330 Fall 2014 Midterm 2 Solutions

CS Lecture 2. The Front End. Lecture 2 Lexical Analysis

Implementation of Lexical Analysis

Midterm 2 Solutions Many acceptable answers; one was the following: (defparameter g1

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

CpSc 421 Final Solutions

First Midterm Exam CS164, Fall 2007 Oct 2, 2007

Midterm Exam II CIS 341: Foundations of Computer Science II Spring 2006, day section Prof. Marvin K. Nakayama

Introduction to Ruby, part I

1. Draw the state graphs for the finite automata which accept sets of strings composed of zeros and ones which:

Name: Username: I. 20. Section: II. p p p III. p p p p Total 100. CMSC 202 Section 06 Fall 2015

Structure and Interpretation of Computer Programs

CMSC330 Fall 2014 Final Exam

Structure of Programming Languages Lecture 3

Optimizing Finite Automata

University of Nevada, Las Vegas Computer Science 456/656 Fall 2016

CSE 5317 Midterm Examination 4 March Solutions

Prelim 1 Solutions. CS 2110, March 10, 2015, 5:30 PM Total Question True False. Loop Invariants Max Score Grader

Midterm Exam. CSCI 3136: Principles of Programming Languages. February 20, Group 2

CMPE 4003 Formal Languages & Automata Theory Project Due May 15, 2010.

Midterm Exam CS 251, Intermediate Programming March 6, 2015

CSE au Final Exam Sample Solution

Qualifying Exam Languages

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

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

ECE 468/573 Midterm 1 October 1, 2014

Finite automata. We have looked at using Lex to build a scanner on the basis of regular expressions.

CSE wi Midterm Exam 2/8/18 Sample Solution

CS 164 Programming Languages and Compilers Handout 9. Midterm I Solution

CSE 413 Final Exam. June 7, 2011

Final Examination: Topics and Sample Problems

CSE 105 THEORY OF COMPUTATION

University of Waterloo Midterm Examination

1. [5 points each] True or False. If the question is currently open, write O or Open.

Compiler Construction

Transcription:

CMSC330 Spring 2015 Midterm #1 9:30am/11:00am/12:30pm Name: Discussion Time (circle one): 10am 11am 12pm 1pm 2pm 3pm Discussion TA (circle one): Amelia Casey Chris Mike Elizabeth Eric Tommy 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 Regular Expressions /17 2 Finite Automata /14 3 NFA to DFA /10 4 OCaml /7 5 Programming Language Concepts /12 6 Ruby execution /20 7 Ruby programming /20 Total /100 1

1. Regular Expressions. For parts b e, below, assume the alphabet Σ = {a, b}. a. (3 points) Give an English description of the strings matched by Ruby regexp /[ct]h[a-z]*[aouei]$/ A string ing with a substring starting with ch or th, followed by a lowercase letter, and then a lowercase vowel. b. (4 points) Give a Ruby regexp that denotes the language of strings in which the number of a s is divisible by three b*(ab*ab*ab*)* c. (3 points) Give a Ruby regexp that denotes the language of strings that have exactly two bs. a*ba*ba* d. (7 points ) Draw an NFA that recognizes the language defined by regexp b*(aab(aab)*)* The full one is the left, and a minimized one is on the right 2

2. Finite Automata. For the true or false questions, no explanation is needed; if you add one for consideration of partial credit, it could help or hurt you. a. (2 points) True or false: A DFA or NFA can detect if an input (of any length) is a palindrome. (A palindrome is a word that reads the same backwards as forwards.) False. (It can detect palindromes up to a fixed length, but not of any length.) b. (2 points) True or false: Epsilon transitions make NFAs more powerful than DFAs, in terms of the languages they can express. False. NFAs and DFAs can express the same languages (the regular ones) c. (2 points) True or false: Given an NFA with N states, the corresponding DFA always have 2 N states (assuming we explicitly include dead states). False. It is at most 2N states, not always. For example a DFA is also an NFA, for that NFA the corresponding DFA has the same number of states. d. Consider the following finite automata FA1 and FA2, depicted below, and answer the following questions: i. (3 points) Is either or both of FA1 and FA2 a DFA? Which one(s), if so? Both. ii. (5 points) Do they recognize the same language? If not, give an example string that is accepted by one and not the other. Yes, they accept the same language. FA1 (final states are q1 and q4) FA2 (final state is q1) 3

3. NFA to DFA conversion a) (7 points) Convert the following NFA to a DFA using the subset construction algorithm. Be sure to label each state in the DFA with the corresponding state(s) in the NFA. Answer: b) (3 points) Give an English description of the strings matched by this NFA Any number of a s and b s, followed by ab, followed by any number of a s and b s. I.e., all strings with ab as a substring. 4

4. OCaml a. (3 points) Next to each of the following OCaml expression, give its type: a) [2; 3] int list b) [[1]; [2; 1]; [3]] int list list c) [(1, a ); (2, b )] int*string list b. (4 points) When executing the following code, what values are y and z initialized to? let x=5 in let y=x+1 in y is 6 let x=2 in (overrides previous x, does not assign to it) let z=x+y z is 8 in z;; 5. Programming Language Concepts a. (3 points) Circle each programming language attribute/feature which describes Ruby: Object-oriented Static typing Pattern matching Implicit declarations Overloading Note: Regular expression matching is not pattern matching b. (3 points) Circle each programming language attribute/feature which describes OCaml: Object-oriented Static typing Pattern matching Implicit declarations Overloading Note: Object-oriented was optional: circling it neither helped or hurt you c. (3 points) Which is true of languages that employ dynamic typing? Circle all that apply: I. Type errors will not be caught until you run the program II. Type errors are not caught at all too dynamic III. At different times, variables can contain values with different types IV. Types are inferred by the compiler, so they don t need to be written down V. Lists are required to contain elements all having the same type b. (3 points) True or false: Ruby s == method is equivalent Java s == method. Explain. False. Ruby s == method checks structural equality, while Java s checks pointer/reference equality. Another difference is that == can be overridden in Ruby, but not in Java. 5

6. Ruby execution. Write the output, if any, of executing the following code. Write FAIL if an error will occur at run-time (following any output to that point). a. (4 points) x = [1,2, nil, 4] x[3] = 3 x.each { e if e then print e else print "?" } 12?3 b. (4 points) class F def initialize(x) @@g = x def foo(z) return @@g + z a = F.new(1) b = F.new(2) puts a.foo(5) 7 c. (4 points) ss = ["abc", "def", "ghij", "???", "WXYZ" ] ss.each { s if s =~ /^(a d W)..$/ then print $1 else print "X" } adxxx 6

d. (4 points) arr = [2, 1, - 3, 5] arr.sort arr2 = [6, 2, 1, 1] arr2.sort! puts arr puts arr2 2 1-3 5 1 1 2 6 (each on its own line) e. (4 points) h = { 1 => hello, 2 => bye } x = h.keys.collect { k h[k] } puts x[0] hello or bye were acceptable, since there is no guarantee of the order the keys are provided. 7

7. Ruby programming. (20 points) A sparse array is one that in which most of the elements of the array consist of the same (default) value. If the array is very large, then representing it as we normally would require unnecessary space. Implement a class SparseArr. It implements a two-dimensional, square array whose representation assumes it will be sparse. Complete the implementation of the array. Importantly, the space occupied by your array should be O(n) where n is the number of elements you have inserted into the array (note that there can be somewhat large constant factors here). You should also strive for O(1) lookups and updates, i.e., get and put calls should take roughly constant-time. Hint: you can assume that Ruby hashes have these properties, so you might want to use them in your implementation (but you can use other data structures, too, if you like). Methods to implement: the constructor takes the size of the array (the dimension), and the default value size returns the dimension of the array get(x,y) returns the value at coordinate x,y, or throws an exception if out of bounds put(x,y,e) updates the value at x,y, or throws an exception if out of bounds +(arr) returns a new array that is the element-wise sum of the current array and arr; throws an exception if arr and the current array are not the same size; the new array s default element matches the current one s Example session: irb> x = SparseArr.new(2,0) =>... irb> x.size => 2 irb> x.get(1,2) IndexError: 1,2 outside of bounds of 2D- array size 2... irb> x.get(0,0) => 0 irb> x.put(1,1,5) => 5 irb> x.get(1,1) => 5 irb> (x + x).get(1,1) => 10 Note that you should maintain the sparseness of the array, in your implementation. For example, the array produced by x+x in the above interaction should also be sparse 8

class SparseArr attr_reader :size def initialize(size,elem) @arr = Hash.new (* you could also have set the hash s default value: Hash.new(elem) *) @elem = elem @size = size def get(x,y) checkbounds(x,y,@size) (* defined on next page *) z = @arr[[x,y]] if z == nil then @elem else z def put(x,y,e) checkbounds(x,y,@size) if get(x,y)!= e then (* preserves sparseness if e is default value *) @arr[[x,y]] = e def +(arr) if arr.size!= @size then raise IndexError, "#{arr.size} of argument!= array size #{@size}" r = SparseArr.new(@size,@elem) for x in 0...@size do for y in 0...@size do v = get(x,y) w = arr.get(x,y) r.put(x,y,v+w) r 9

# you might find this useful: raises an exception if either x or y is out of the bounds def checkbounds(x,y,sz) if x < 0 y < 0 x >= sz y >= sz then raise IndexError, "#{x},#{y} outside of bounds of 2D-array size #{sz}" Standard library methods: x.push(e) adds e to the of x, an array x.unshift(e) adds e to the start of x, an array x.shift returns and removes the element at the front of x, an array x.pop returns and removes the element at the of x, an array x.each takes a code block expecting one argument. The method will invoke the code block for each element in x, an array, and will return the array, unchanged x.collect takes a code block expecting one argument. The method will invoke the code block for each element in x, an array, and collect the results in an array that s returned x.flatten is a one-dimensional flattening of x, an array, which merges the contents of any nested arrays into x, directly h.each takes a code block expecting two arguments. The method will invoke the code block for each key-value pair in h, and leave h unchanged h.collect takes a code block expecting two arguments. The method will invoke the code block for each key-value pair in h, and collect its results in a list, leaving h unchanged h.empty? returns true if h, a hash, is empty, and false otherwise 10