quiz1-example-fall11

quiz1-example-fall11 - # Output = a[foo] = 2 puts a[1] d....

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

View Full Document Right Arrow Icon
CMSC330 Fall 2011 Example Quiz #1 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) Describe a benefit of using implicit declarations in Ruby. 2. (12 pts) What is the output (if any) of the following Ruby programs? Write FAIL if code does not execute. a. (3 pts) puts “Win” if 1 < 0 # Output = puts “The” if 0 < 1 if 0 then puts “Future” end b. (3 pts) a = [5.”foo”,2.1] # Output = puts “Found #{x[1]}” c. (3 pts) a = { }
Background image of page 1

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

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: # Output = a[foo] = 2 puts a[1] d. (3 pts) a = [ ] # Output = a[2] = 5 a.each { |x| puts x } 3. (14 pts) Given an array of strings x , write a Ruby method printRepeats( x ) using Hash and code blocks to print out all strings in S followed by the number of occurrences, with each string on a separate line. Some helpful functions (not all need to be used): a = h.keys // returns keys in hash h as an array a a = h.values // returns values in hash h as an array a b = a.sort // b = new array similar to a, but in sorted order a.sort! // sorts elements of array a in place a.size // number of elements in the array a.each { } // apply code block to each element in array a.push / a.pop // treat array as stack Example Input (value of x) Example Output x = [c, b, a, b, a, d, b] a 2 b 3 c 1 d 1 Answer: def printRepeats( x )...
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.

Page1 / 2

quiz1-example-fall11 - # Output = a[foo] = 2 puts a[1] d....

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

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