CMSC330 Summer 2009 Quiz 1 Name: __________Answer_______________________________ 1. Compilation and Interpretation (4 points) a. What is the difference between compilation and interpretation? Compilation converts source code into machine code and attempts to optimize/process during this step, examining the entire code; interpretation executes a single instruction at a time without this analysis b. Give an instance where you would use one over the other. If we care about runtime performance we would likely use a compiled language 2. What are 4 attributes of a good programming language (list and BRIEFLY explain each in one sentence or phrase)? (4 points) Look at slides 26-28 from first day of class3. Give the output for the following Ruby programs or write FAIL if they would not execute or if they would produce an error. (10 points) a. x = "string" y = "string" puts "yes" if x == y x = 'hello' y = 'world' puts x + y b. x = "My phone number is (301)-555-5555." regex1 = /phone.*/ regex2 = '\((\d{3})\)-\d{3}-\d{4}' regex3 = '$' if x =~ regex1 puts "1" puts $1 end if x =~ Regexp.new('phone.*' + regex2) puts "2" puts $1 end if x =~ Regexp.new(regex2 + regex3) puts "3" puts $1 end 4. Complete the Ruby method below to print all integers from 0 to x that are odd. (x is a variable that is passed in). Hint: use %. (4 points) def printOdd(x) for i in (0..x) if (i % 2 != 0) then puts i end end end yes helloworld 1 nil 2
View Full Document