Midterm Exam

Mar 9, 2011

This is a online "take-home" exam. Your answers are due in blackboard by 11:59PM on Saturday, March 12. It contains three sections (Architecture, Ruby and Rails), a total of 25 questions and is worth 1000 points (40 points per question).

If something is unclear on the exam and you have a question, simply post your question to the Midterm Exam Questions thread on Ore.

Architecture

  1. What is the difference between an HTTP GET request and a POST request?

You see a link in an interface whose markup is as follows:

<a href="images/new?request_type=PUT" method="POST">create a new image</a>
  1. Is the target URL relative or absolute?
  2. What is the difference between an absolute and relative URL?
  3. If you clicked on this link, what kind of request would your browser generate? (which HTTP method?) Assume no JavaScript modifies the behavior of the link.
  4. Is there a querystring? What is it?
  5. What is lacking from the link declaration that would otherwise enhance accessibility?
  1. What are the roles of the model, view and controller in most typical Web applications? (One sentence for each.)

Given the following HTTP response header:

HTTP/1.1 304 Not Modified
Date: Wed, 09 Mar 2011 16:43:33 GMT
Server: Apache
Connection: Keep-Alive
Keep-Alive: timeout=2, max=100
Etag: "110e412f-7df-49e0f6a106500"
Vary: Accept-Encoding
  1. Would an HTTP response that begins like that usually contain a body? Why or why not?

Ruby

  1. Write a Ruby class definition that meets the following criteria:
  2. Imagine an Engine instance hemi, which, when the following method is called:
    hemi.respond_to?("to_s")
    returns true. What is missing from your class definition in order for this example to be accurate?
  3. What object-oriented feature does the respond_to?() method illustrate? (eg, "inheritance, composition, ...")
  4. According to Ruby conventions, what character is usually at the end of methods that return boolean values?
  5. According to Ruby conventions, what is the difference between pairs of methods like do_this and do_this! (notice the bang) ?
  6. Briefly explain Ruby's type system. What is it (by name)? What does it mean?
  7. What type of Ruby object does the following expression yield? %w( master rails and then try another framework you'll never go back)
  8. Given an array of strings called @happy_places, would these two snippets of code do the same thing?
    @happy_places.each do |happy_place|
      puts happy_place
    end
    and
    @happy_places.each {|hp| puts hp}
  9. Given a function that needs to return a value to its caller, does the function need an explicit return statement? If so, explain why. If not, then what can you always expect a Ruby function to return?

Rails

  1. Which component of the MVC stack does ActiveRecord embody?
  2. The Rails convention maps HTTP methods to certain controller methods, and those methods usually involve specific CRUD operations on models. Given the following CRUD database methods:
    create, read, update, and delete
    and the following HTTP methods:
    GET, PUT, POST, DELETE
    and the following controller actions:
    index, new, create, edit, update, destroy Complete the following table.
    HTTP method controller action CRUD operation
    index
    new
    create
    edit
    update
    delete

  3. Since browsers don't natively support real PUT or DELETE requests, how does Rails get around this to simulate RESTful behavior?
  4. What are the three standard Rails environments? Name them, and explain their purpose.
  5. Usually, Rails controllers incorporate plural nouns, such as ProtestsController and RevolutionsController. In what case should a controller have a singular name like GeocodingController?
  6. What is a Rails "helper method" and when should they be defined and used by you, the developer?

Assume you have a RockStar AR class that has_many :fans, and a Fan class that belongs_to :rock_star.

  1. What attribute must be present on which table, in order for AR to infer the proper foreign key / relationship?

Assume a GET request is sent to the RockStarsController#index action, which contains a finder method call @rockstars = RockStar.all. Assume the view app/views/rock_stars/index.html.haml displays the name of each RockStar and each RockStar instance's fans like so:

- @rockstars.each do |r|
  %h1= r.name
    - r.fans.each do |f|
      %p= f.name
If you were tailing the log of your application during the rendering of the response, you would notice tons of database queries.

  1. Are all of those queries ok? If so, explain why. If not, explain how you would reduce the number of database queries (without hand-rolling your own SQL query).