Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Page 1 of 7 IT 212 – Applied OO Programming April 17, 2014 Part A. Multiple Choice Questions. Give an optional reason for each question. If you think that none of the answers are correct, indicate your correct answer. Note that multiple Ruby statements can be placed on the same line if separated them by semicolons. 3 points each. 1. (Ruby) What is the name of the Ruby setter for @price? a. price b. price= c. price& d. set_price 2. (Ruby) What does the array a = [5, 9, 7, 4] look like after these statements: a.push 3; a.sort!; a.pop ( a. [3, 4, 5, 7] b. [4, 5, 7, 9] c. [5, 9, 7, 4] d. [5, 9, 7, 4, 3] 3. (Ruby) What is the output? x = 5; y = 3; x = y; a. 3 3 b. 3 5 y = x; c. 5 3 print "%d %d\n" % [x, y] d. 5 5 4. (Ruby) Where is the keyword super placed in the initialize method for a derived class? a. On the line after def initialize. b. On the line before the final end statement. b. In the return statement. d. super cannot be used in initialize. 5. (Ruby) Which choice is NOT true about this Ruby statement? public class executive < employee a. class should be uppercase. b. employee and executive are in the wrong order. c. employee and executive should be uppercase. d. public is not needed. 6. (Ruby) Which method returns an enumerator? a. [ ] b. each c. new d. print 7. (Ruby) What is an exception? a. Something that goes wrong in your script that causes it to crash. b. An unusual value returned by a method, such as a nil. c. An illegal value in a file. d. An empty string, for example s = "". Page 2 of 7 8. (Ruby) Which regular expression is accepts the string "W8922"? a. /B\d{5}/ b. /BW\d{4}/ c. /(B|W)\d{4}/ d. /W+\d+/ 9. (Ruby) What is the output? "bcaietwaxyzfgeouubnw".scan /[aeiou]*/ a. ["a"] b. ["aie"] c. ["aie", "a", "eouu"] d. ["uu"] 10. (Ruby) The table students contains this data: name year major gpa Alice 2 Sociology 3.78 Oliver 3 Political Science 2.91 Arthur 4 Mathematics 3.96 Heather 2 Journalism 3.98 Gary 1 English 3.59 Ulrika 1 German 3.34 What is the output from these statements? rows = $db.execute "select avg(Gpa) from students where year=2;" print rows, "\n" a. [[3.59]] b. [[3.78, 3.98]] c. [[3.88]] d. [[3.88, 2]] 11. (Ruby) If address is defined like this: address = [{:number => 3456, :street => "Maple", :city => "Streeterville", : => "IL"}, {:number => 7890, :street => "Oak", :city => "Evanston", :state => "WY"}, {:number => 1324, :street => "Walnut", :city => "Barstow", :state => "CA"}, {:number => 3456, :street => "Birch", :city => "Hershey", :state => "PA"}] Which statement produces this output? Birch a. print address["street"] b. print address["street"][3] b. print address[3]["street"] d. print address["street"][4] 12. (Ruby) What is the output? (<=> is the spaceship operator.) print "dog" <=> "cat" a. nil b. -1 c. 0 d. 1 13. (Ruby or Java) What is the output? (? and : form the tertiary conditional operator.) print "dog" < "cat" ? 'D' : 'C' a. false b. true c. C d. D Page 3 of 7 14. (Java) Which is correct? a. char gender = 'G'; c. String gender = G; b. char gender = "G"; d. String gender = 'G'; 15. (Java) Which is correct for printing to standard error? a. println(STDERR, x); b. System.err.println(x); c. STDERR.println(x); d. System.STDERR.println(x); Part B. Short Essay Questions. Only answer 1 out of 3 questions. 10 points. 1. Explain what inheritance is and why it is useful in modern object oriented programming. 2. Explain the differences between a Ruby class and a Ruby module. What is a mixin and how can a module be used to create one. 3. Compare the Ruby and Java programming languages. How are they similar and how are they different. Page 4 of 7 Part C. Find Errors in Ruby Script. Correct the errors on this sheet. There are about 15 errors. 10 points. class Person attr_reader :last, :first, gender def initialize the_last, the_first, the_gender @last = the_last @first = the_first gender = the_gender end def to_s return "%s %s %s" % [@last, @first, @gender] end end class Driver < Person attr_accessor :license_number def initalize the_last, the_first, the_gender, the_lic_num @license_number = theLicNum super the_last, the_first, the_gender end def to_string return super + ('%s\n' % @lisense_number) end end drivers = [ ] File.open("drivers.csv", "r") do /file/ while line = file.gets f = line.chomp.split(',') d = Driver.new f[0], f[1], f[2], f[4].to_f drivers += d end end drivers.each |d| do if d.license_number[0] == 'N' && d.gender = 'F' print "%s %s\n" % (d.first, d.last) end Page 5 of 7 end Here is the input file drivers.csv: Bowman,Julia,F,J230-7842-4219 Kern,Kendell,M,N212-3278-4839 Kerry,Cynthia,F,N319-3283-3289 Miller,Wayne,M,K237-2392-2139 Johnson,Carrie,F,N329-3229-3293 Chen,Maggie,F,N998-2918-5685 Part D. Predict the Output. Indicate how you obtained your answer. 5 points. require 'sqlite3' $db = SQLite3::Database.new 'pets.db' pets = ["Freddie,Dog,4", "Midnight,Cat,5", "Pee Wee,Gerbil,1", "Duke,Dog,6", "Whiskers,Cat,3", "Roxie,Cat,4"] $db.execute %Q/create table if not exists pets( name varchar(15), animaltype varchar(10), age integer);/ pets.each do |p| fields = p.split(',') $db.execute %Q*insert into pets values( '#{fields[0]}', '#{fields[1]}', #{fields[2].to_i});* end rows = $db.execute "select name from pets " + "where animaltype='Dog' and age >= 4;" rows.each do |r| print r, " " end Page 6 of 7 Part E. Find the Errors in Java Script. There are about 15 errors. 10 points. ======= Source code file Pet.java ===================================================== private class Pet { private String name; private String animalType public int age; public Pet(String theName, String theAnimalType, int theAge) { theName = name; theAnimalType = animalType; theAge = age; } public String getName( ) { return name; private String getAnimalType( ) { return animalType; } public void getAge( ) { return age; } public void setAge(int theAge) { age = theAge; } public String toString( ) { return name + animalType + " " + age; } Page 7 of 7 ======= Source code file PetArray.java ================================================ public class Main { public static void Pet[ ] array = array[0] = new array[1] = new array[2] = new array[3] = new array[4] = new array[5] = new main(String args) { new Pet[6]; Pet("Freddie", "Dog", 4); Pet("Midnight", "Cat", 5); Pet("Pee Wee", "Gerbil", 1); Pet("Duke", "Dog", 6); Pet("Whiskers", "Cat", 3); Pet("Roxie", "Cat", 4); for(int i = 0; i <= array.length; i++) { if (array(i).getAge( ) >= 5) System.in.println(array[i]); } } } }