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
- 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>
- Is the target URL relative or absolute?
- What is the difference between an absolute and relative URL?
- 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.
- Is there a querystring? What is it?
- What is lacking from the link declaration that would otherwise enhance accessibility?
- 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
- Would an HTTP response that begins like that usually contain a body? Why or why not?
Ruby
- Write a Ruby class definition that meets the following criteria:
- class is called
Engine
- class has publicly accessible attributes
horsepower, volume, and cylinder_count
- upon instantiation, an object of this class has a member variable, a String, called
noise, whose initial value is "VROOOM" (that's three O's).
- class has an instance method called
go() that prints the value of the instance variable noise 100 times
- class has an instance method called
reverse() that prints the value of the instance variable noise backwards
- class has a static/class method called
manufacture(), which takes one string argument and returns an Engine instance whose noise attribute is "putt-putt-putt"
- 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?
- What object-oriented feature does the
respond_to?() method illustrate? (eg, "inheritance, composition, ...")
- According to Ruby conventions, what character is usually at the end of methods that return boolean values?
- According to Ruby conventions, what is the difference between pairs of methods like
do_this and do_this! (notice the bang) ?
- Briefly explain Ruby's type system. What is it (by name)? What does it mean?
- What type of Ruby object does the following expression yield?
%w( master rails and then try another framework you'll never go back)
- 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}
- 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
- Which component of the MVC stack does ActiveRecord embody?
- 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 |
|
- Since browsers don't natively support real PUT or DELETE requests, how does Rails get around this to simulate RESTful behavior?
- What are the three standard Rails environments? Name them, and explain their purpose.
- Usually, Rails controllers incorporate plural nouns, such as
ProtestsController and RevolutionsController. In what case should a controller have a singular name like GeocodingController?
- 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.
- 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.
- 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).