quiz1-soln-fall10

quiz1-soln-fall10 - 2. (12 pts) What is the output (if any)...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
CMSC330 Fall 2010 Quiz #1 Solution Name Discussion Time (circle one): 9am 10am 11am 12pm 1pm 2pm Instructions Do not start this test until you are told to do so! You have 15 minutes for this quiz. 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. 1. (4 pts) Name an important difference between Ruby’s nil and Java’s null . nil is an object, while null is not. I.e., nil.to_s returns “nil” nil can be treated as false, while null cannot.
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 2. (12 pts) What is the output (if any) of the following Ruby programs? Write FAIL if code does not execute. a. (4 pts) a = { } # Output = nil a[foo] = 1 puts a[1] b. (4 pts) if Run DMC =~ /([a-z]+)/ then # Output = Found un puts Found #{$1} else puts Not found end c. (4 pts) a = [1,2,3] # Output = 3 b = a.collect { |x| x+2 } 4 puts b 5 3. (4 pts) Give a regular expression (formal REs or in Ruby) for all even 3-digit numbers less than 200. Leading zeros are allowed (i.e., allow 000 to 198, but not 001 or 199). Formal RE (0|1)(0|1|2|3|4|5|6|7|8|9)(0|2|4|6|8) Ruby RE /(0|1)(0|1|2|3|4|5|6|7|8|9)(0|2|4|6|8)/ /[01][0-9][02468]/...
View Full Document

This note was uploaded on 01/13/2012 for the course CMSC 330 taught by Professor Staff during the Fall '08 term at Maryland.

Ask a homework question - tutors are online