Practice Problem 1 with Solutions - Organization Programming Language | CMSC 330, Assignments of Programming Languages

Material Type: Assignment; Class: ORGNZTN PROGM LANG; Subject: Computer Science; University: University of Maryland; Term: Spring 2009;

Typology: Assignments

Pre 2010

Uploaded on 07/30/2009

koofers-user-hte
koofers-user-hte ๐Ÿ‡บ๐Ÿ‡ธ

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMSC 330, Practice Problems 1 (SOLUTIONS)
1. Programming languages
a. Explain how goals for programming languages have changed since the 1960โ€™s.
Shifted from efficiency to ease-of-programming
b. List 2 desirable attributes for a programming language where Ruby is better
than C. Explain why.
Naturalness of application โ€“ Text processing is easier in Ruby
Cost of use โ€“ Small Ruby programs are simpler/quicker to write
c. List 2 methods for executing a program. Which method is used by Ruby?
Interpretation & compilation. Ruby is interpreted.
d. Explain why Ruby fits the definition of a scripting language.
Rich text processing (Regexp) and easy to use (implicit declarations)
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.
def foo(x) โ€ฆ end ; foo(2) ; // x = formal param, 2 = actual parameter
b. Using different Ruby control statements, write 4 code fragments that iterate
from i=1 to i=10.
1.upto(10) {|i| puts i; }
(1..10).each {|i| puts i; }
for i in (1..10) do puts i; end
i=1; while i<=10 do puts i; i+=1; end
i=1; do puts i; break if (i+=1)>10 end
c. Explain the difference between explicit and implicit variable declarations.
Explicit โ€“ declaration statements declare type of each variable used
Implicit โ€“ first use of a variable declares it and determines its type
d. List two advantages of static types.
Helps prevent subtle errors, catches more type errors at compile time
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.
class Teacher
@@totalStudents = 0
def initialize
@students = 0
@@totalStudents += @students
end
end
f. Give an example of reference copy in Ruby.
x = โ€œaโ€ ; y = x
g. Give an example of testing for structural equality in Ruby.
x == y
pf3
pf4

Partial preview of the text

Download Practice Problem 1 with Solutions - Organization Programming Language | CMSC 330 and more Assignments Programming Languages in PDF only on Docsity!

CMSC 330, Practice Problems 1 (SOLUTIONS)

  1. Programming languages a. Explain how goals for programming languages have changed since the 1960โ€™s. Shifted from efficiency to ease-of-programming b. List 2 desirable attributes for a programming language where Ruby is better than C. Explain why. Naturalness of application โ€“ Text processing is easier in Ruby Cost of use โ€“ Small Ruby programs are simpler/quicker to write c. List 2 methods for executing a program. Which method is used by Ruby? Interpretation & compilation. Ruby is interpreted. d. Explain why Ruby fits the definition of a scripting language. Rich text processing (Regexp) and easy to use (implicit declarations)
  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. def foo(x) โ€ฆ end ; foo(2) ; // x = formal param, 2 = actual parameter b. Using different Ruby control statements, write 4 code fragments that iterate from i=1 to i=10. 1.upto(10) {|i| puts i; } (1..10).each {|i| puts i; } for i in (1..10) do puts i; end i=1; while i<=10 do puts i; i+=1; end i=1; do puts i; break if (i+=1)>10 end c. Explain the difference between explicit and implicit variable declarations. Explicit โ€“ declaration statements declare type of each variable used Implicit โ€“ first use of a variable declares it and determines its type d. List two advantages of static types. Helps prevent subtle errors, catches more type errors at compile time 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. class Teacher @@totalStudents = 0 def initialize @students = 0 @@totalStudents += @students end end f. Give an example of reference copy in Ruby. x = โ€œaโ€ ; y = x g. Give an example of testing for structural equality in Ruby. x == y
  1. Ruby advanced features a. Describe the string matched by the Ruby regular expression /(3{2})/? $1 = exactly 2 3โ€™s, i.e., โ€œ33โ€ b. Describe the string matched by the Ruby regular expression /([A-Z])/? $1 = any single uppercase letter c. Describe the string matched by the Ruby regular expression /([A-Z]*[0-9])/? $1 = 0 or more uppercase letters followed by a single digit d. Describe the string matched by the Ruby regular expression /(0$)/? $1 = a 0 at the end of the line e. Describe the string matched by the Ruby regular expression /(.)/? $1 = a single (literal) period f. What is the output of the following Ruby program? โ€œCMSC 330โ€ =~ /([0-9]+)/ puts $1 // 330 puts $2 // nil 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 // 4 6 nil nil 7 1 puts a.pop // 1 h. What is the output of the following Ruby program? if โ€œCMSC 330โ€ =~ /1/ then puts โ€œtโ€ elsif โ€œCMSC 330โ€ !~ /1/ then puts โ€œfโ€ // f else puts โ€œnโ€ end i. What is the output of the following Ruby program? a = [โ€œcโ€, โ€œbโ€, โ€œaโ€] puts a // c b a b = a a.sort! puts b // a b c j. What is the output of the following Ruby program? a = โ€œCMSC 330 CMSC 351โ€ b = a.scan(/[A-Z]+/) puts b // CMSC CMSC a.scan(/[0-9]+ [A-Z]+/) { |x| puts x } // 330 CMSC k. What is the output of the following Ruby program? a = {4 => 6, 5 => 7} puts a[4] // 6 puts a[6] // nil puts a.values // 6 7 or 7 6

// Alternate version that reads file one line at a time file = File.new(ARGV[0], "r") until file.eof? do line = file.readline line.chomp! if line !~ /[^A-Za-z0-9_]+/ then puts line end end