final-soln-spr08

final-soln-spr08 - CMSC330 Spring 2008 Final Exam Solutions...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CMSC330 Spring 2008 Final Exam Solutions 1. (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. Semantics Ruby Syntax OCaml Syntax Conditional statement foo( ) if true if true then foo( ) Nested if statement if … then … elsif … then … else … end if … then … else if … then … else Assign value to x x = 1; x := 1 Structural equality of strings a == b a = b Floating point operations 1.0 + 1.1 1.0 +. 1.1 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. Statement Ruby Semantics Java Semantics x = 1; Declare new variable x & assign value 1 to x Assign 1 to closest x in static scope 3 + 4 Method invocation: 3.+(4) Add 3 & 4 if (x) Check x == nil Check x == (boolean) true “#{x}” String for x.to_s String “#{x}” x == “str” Structural equality (contents) Physical equality (address) c. (3 pts) Describe a feature of an old programming language which proved vexing for programmers. Spaces in identifiers, no reserved keywords, dynamic scoping, etc… 2. (12 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] // (3 pts) 2 3 nil c b a[3] = “c” a.push(“b”) puts a b. if “CMSC 330” =~ /([0-9]+)/ then // (3 pts) 330 puts $1 else puts “Error” end c. s = "CMSC 330 is too HARD!" // (3 pts) CMSC HARD s.scan(/[A-Z]+/) { |x| puts x } d. h = {2 => 3, 1 => 2, 0=>1} puts "#{h[1]} #{h[2]}" // (3 pts) 2 3
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
3. (20 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 Example Input Example Output file = File.new(ARGV[0], "r") until file.eof? do line = file.readline end // a = Array.new ; a.sort! // print 1 ; puts 2 yaris 2008 prius 2004 prius 2007 rav4 1998 yaris 2006 prius 2006 camry 1983 camry 1983 prius 2004 2006 2007 yaris 2006 2008 cars = { } // (2 pt) Allocate Hash file = File.new(ARGV[0], "r") // (2 pt) Open & read file until file.eof? do line = file.readline if line =~ /([a-zA-Z]+) ([0-9]+)/ // (4 pt) RE for entry if ! cars[$1] // lowercase only is fine cars[$1] = Array.new // (2 pt) init new Hash entry end cars[$1].push($2) // (2 pt) store model year end end labels = cars.keys // (2 pt) get & sort list of cars labels.sort! labels.each { |x|
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 13

final-soln-spr08 - CMSC330 Spring 2008 Final Exam Solutions...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online