Summer 2006 CMSC330 Pseudo-Midterm 1 - Prof. Jeffrey S. Foster, Exams of Programming Languages

A pseudo-midterm exam for the cmsc330 course during the summer 2006 semester. The exam covers various topics in ruby programming, including conditional statements, arrays, sorting, hashes, and regular expressions. It also includes questions related to writing ruby programs, understanding formal regular expressions, and converting nfas to dfas.

Typology: Exams

Pre 2010

Uploaded on 02/13/2009

koofers-user-rw1
koofers-user-rw1 🇺🇸

4.5

(2)

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMSC330 - SUMMER 2006 - PSEUDO-MIDTERM 1
INSTRUCTOR: GUILHERME FONSECA
(1) (20 points) What is the output of each Ruby program below? Ignore any possible
warning message. All programs are syntatically valid.
(a) if 0
puts "ab"
elsif ""
puts "cd"
else
puts "ef"
end
(b) a = b = [3, 1, 2]
a.sort!
puts b
(c) a = [[1,2], [1,2,1], [-1], [1,3]]
puts a.sort.inspect
(d) h = Hash.new([])
h["a"] = 3
h["b"] = h["b"] + [5]
h["c"] = h["c"] + [7]
puts "#{h["a"]} #{h["b"][0]} #{h["c"][-1]}"
(2) (10 points) Write a ruby program that reads several whole numbers from the standard
input, and prints them sorted by the absolute value. In Ruby, if xis a Fixnum, x.abs
returns the absolute value of x. The input numbers have no leading zeroes or spaces,
and are presented as one number per line. Output them as one number per line too.
(3) (21 points) Write a formal regular expression for each of the languages below. The
alphabet is Σ = {a, b}. The only operators allowed are and |(do not write a Ruby
regular expression).
(a) {w|the number of a’s before the first bin wis at least 3 }
wis trivially in the language if there is no b.
(b) {w|the number of a’s before the first bin wis at most 5 }
wis trivially in the language if there is no b.
(c) The intersection of the two languages above.
(4) (8 points) Write a formal regular expression for each of the languages below. The
alphabet is Σ = {a, b, c}. The only operators allowed are and |(do not write a
Ruby regular expression).
(a) {w|there is no aadjacent to b}
1
pf2

Partial preview of the text

Download Summer 2006 CMSC330 Pseudo-Midterm 1 - Prof. Jeffrey S. Foster and more Exams Programming Languages in PDF only on Docsity!

CMSC330 - SUMMER 2006 - PSEUDO-MIDTERM 1

INSTRUCTOR: GUILHERME FONSECA

(1) (20 points) What is the output of each Ruby program below? Ignore any possible warning message. All programs are syntatically valid. (a) if 0 puts "ab" elsif "" puts "cd" else puts "ef" end (b) a = b = [3, 1, 2] a.sort! puts b (c) a = [[1,2], [1,2,1], [-1], [1,3]] puts a.sort.inspect (d) h = Hash.new([]) h["a"] = 3 h["b"] = h["b"] + [5] h["c"] = h["c"] + [7] puts "#{h["a"]} #{h["b"][0]} #{h["c"][-1]}"

(2) (10 points) Write a ruby program that reads several whole numbers from the standard input, and prints them sorted by the absolute value. In Ruby, if x is a Fixnum, x.abs returns the absolute value of x. The input numbers have no leading zeroes or spaces, and are presented as one number per line. Output them as one number per line too.

(3) (21 points) Write a formal regular expression for each of the languages below. The alphabet is Σ = {a, b}. The only operators allowed are ∗ and | (do not write a Ruby regular expression). (a) {w | the number of a’s before the first b in w is at least 3 } w is trivially in the language if there is no b. (b) {w | the number of a’s before the first b in w is at most 5 } w is trivially in the language if there is no b. (c) The intersection of the two languages above.

(4) (8 points) Write a formal regular expression for each of the languages below. The alphabet is Σ = {a, b, c}. The only operators allowed are ∗ and | (do not write a Ruby regular expression). (a) {w | there is no a adjacent to b} 1

(5) (21 points) Write a DFA for each of the languages below. The alphabet is Σ = {a, b}. (a) {w | w has at least one a, at least one b, and all the a’s occur before all the b’s } (b) {w | #a(w) = 1 (mod 3) and #b(w) = 0 (mod 4)} (c) {w | w ends with aba}

(6) (10 points) Convert the following NFA to a NFA without ε transitions. a a b

S T U V ε (^) ε ε

(7) (10 points) Convert the following NFA to a DFA.

S T

V U

a

a

a b

b

a

a

(8) (10 points) Convert the following formal regular expression to a NFA:

b(a|aba)∗(bb)∗|bab

2