Ruby Exam ========= 1) Given this code: class Foo @x = 1 @@y = 2 end What is the difference between @x and @@y? 2) Given a class "Foo", list at least two ways to declare a class method called "bar". 3) True or false: strong typing is possible in (core) Ruby. 4) True or false: method overloading is possible in (core) Ruby. 5) Given this code: class Foo class Bar end end How would you create an instance of class Bar? 6) True or false: you can declare a constant within a Module 7) True or false: mixing in module Foo into class Bar makes Foo a superclass of Bar. 8) True or false: constants are inherited 9) Given the following code: class Foo @@x = "hello" def self.x=(val) @@x = val end def self.x @@x end end class Bar < Foo end Bar.x = "world" What will be returned by Foo.x? 1) "hello" 2) "world" 3) nil - class variables aren't inherited 4) This code will cause an error 10) What's the difference between String#chop and String#chomp ? 11) What's the difference between print and puts ? 12) What does an '&' in front of a variable name indicate? 13) Why is this code illegal? class Foo def bar(x=nil,y) puts "x: #{x}, y: #{y}" end end 14) What is the "splat" operator and what does it do? 15) How do you prevent an instance of a class from ever being created? In other words, how do you make Foo.new illegal? 16) What is a singleton class? 17) Given a class "Foo", how would you create a subclass of Foo called "Bar"? 18) What is the difference between super called with and without parenthesis? 19) What is a block? 20) True or false: regular expressions are objects 21) Is this code legal: "abc".length If so, what is the return value? If not, why not? 22) What is the difference between using backquotes and system() for system calls? 23) What is the difference between String#== and String#=== ? 24) True or False: 0 (zero) evaluates to false in Ruby 25) True or False: Ruby does not have an equivalent to a switch/case statement 26) What is the difference between 'extend' and 'include'? 27) What is the difference between 'clone' and 'dup'? 28) What is a marshalled object? 29) Given the following code: a = "hello" b = a.dup class << a def to_s "The value is '#{self}'" end def twoTimes self + self end end What is the return value of b.to_s? 30) True or False: END blocks are executed in the order they are encountered. 31) True or False: A BEGIN block will be executed before any load or require statements, regardless of its position in the code. 32) When you call "rescue" without specifying an exception class, which exceptions will you catch? a) All exceptions b) StandardError only c) StandardError and all of its subclasses d) None - an exception class *must* be specified 33) True or False: begin and BEGIN are synonymous. 34) What is the result of the following code? i = 0 [1,2,3].each{ |e| i += e } p i a) 6 b) 0 c) None of the above 35) True or False: Constants defined within a class or module may be accessed unadorned anywhere within the class or module