DOC PREVIEW
UMD CMSC 330 - Practice Problems 1

This preview shows page 1 out of 2 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 2 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 2 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CMSC 330, Practice Problems 1 1. Programming languages a. Explain how goals for programming languages have changed since the 1960’s. b. List 2 desirable attributes for a programming language where Ruby is better than C. Explain why. c. List 2 methods for executing a program. Which method is used by Ruby? d. Explain why Ruby fits the definition of a scripting language. 2. Ruby basics a. Write a Ruby method foo that takes an integer as a parameter. Call foo with 2 as its argument. Circle & label the formal and actual parameters in your code. b. Using different Ruby control statements, write 4 code fragments that iterate from i=1 to i=10. c. Explain the difference between explicit and implicit variable declarations. d. List two advantages of static types. e. Using Ruby, write a class Teacher that contains an integer field students and an integer field totalStudents that is shared across all objects of class Teacher. f. Give an example of reference copy in Ruby. g. Give an example of testing for structural equality in Ruby. 3. Ruby advanced features a. Describe the strings accepted by the Ruby regular expression /3{2}/ ? b. Describe the strings accepted by the Ruby regular expression /[A-Z]/ ? c. Describe the strings accepted by the Ruby regular expression /[A-Z]*[0-9]/ ? d. Describe the strings accepted by the Ruby regular expression /0$/ ? e. Describe the strings accepted by the Ruby regular expression /\./ ? f. What is the output of the following Ruby program? “CMSC 330” =~ /([0-9]+)/ puts $1 puts $2 g. What is the output of the following Ruby program? a = [4,5,6] a[5] = 7 a.delete_at(1) a.push(1) puts a puts a.pop h. What is the output of the following Ruby program? if “CMSC 330” =~ /1/ then puts “t” elseif “CMSC 330” !~ /1/ then puts “f” else puts “n” endi. What is the output of the following Ruby program? a = [“c”, “b”, “a”] puts a b = a a.sort! puts b j. What is the output of the following Ruby program? a = CMSC 330 CMSC 351” b = a.scan(/[A-Z]+/) puts b a.scan(/[0-9]+ [A-Z]+/) { |x| puts x } k. What is the output of the following Ruby program? a = {4 => 6, 5 => 7} puts a[4] puts a[6] puts a.values l. What is the output of the following Ruby program? h = Hash.new(0) h["a"] = h["b"] h["b"] = 7 h["c"] += 2 puts "#{h["a"]} #{h["b"]} #{h["c"]}" m. What is returned by “file = File.new(filename, "r"); lines = file.readlines( );”? n. What is returned by “x = ARGV[0];”? o. Write a Ruby function foo that takes a code block and executes it twice. 4. Ruby programming a. Write a Ruby program that reads in lines of input from $stdin and remembers all integers (consecutive digits) encountered. The program should stop and print out the list of integers in sorted order (from smallest to largest) when the word “Done!” is encountered. b. Write a Ruby program that reads the name of a text file from the command line, opens the file, reads every line of text in the file, and prints only the lines that contain exclusively the following characters: uppercase and lowercase letters, digits, and underscore. For example, lines that contain space or punctuation should not be


View Full Document

UMD CMSC 330 - Practice Problems 1

Documents in this Course
Exam #1

Exam #1

6 pages

Quiz #1

Quiz #1

2 pages

Midterm 2

Midterm 2

12 pages

Exam #2

Exam #2

7 pages

Ocaml

Ocaml

7 pages

Parsing

Parsing

38 pages

Threads

Threads

12 pages

Ruby

Ruby

7 pages

Quiz #3

Quiz #3

2 pages

Threads

Threads

7 pages

Quiz #4

Quiz #4

2 pages

Exam #2

Exam #2

6 pages

Exam #1

Exam #1

6 pages

Threads

Threads

34 pages

Quiz #4

Quiz #4

2 pages

Threads

Threads

26 pages

Exam #2

Exam #2

9 pages

Exam #2

Exam #2

6 pages

Load more
Download Practice Problems 1
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Practice Problems 1 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Practice Problems 1 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?