To ExamInfo

Final Exam Review Questions

  1. What are some ways in which HTML5 is different than older versions of HTML? Ans:
    1. Uses the simpler document header <!DOCTYPE html>.
    2. Has more meaningful content tags that can replace the div tag:
      <article> <section> <header> <aside> <nav>
    3. The <canvas> tag allows JavaScript to draw graphical pictures.
    4. Depreciated elements dropped: <center>  <font>  <strike>.
  2. Explain the difference between a view and an action.
    Ans: A view is an .html.erb file that contains HTML and ERB code. An action is a controller method that executes on behalf of a view. The view is displayed immediately after the action executes.
  3. Give some examples of how to be DRY in Rails. Ans: DRY means don't repeat yourself.
    1. Can use CSS code to apply to multiple views.
    2. A layout page can contain multiple views.
    3. A partial can be rendered by multiple views.
    4. Ruby code that used by multiple controllers can be placed in application.html.erb.
    5. Methods that are called by multiple actions can be placed in the private section at the bottom and called by before_action.
  4. What does CoC mean?
    Ans: Convention over configuration. This means that if you accept Rails conventions on where to put everything in the project files, you shouldn't have to do any configuration.
  5. What does ERB mean?
    Ans: Embedded Ruby, which is Ruby code placed within ERB delimiters <%= ... %> or <% ... %>.
  6. How much logic should you put in your ERB files?
    Ans: As little as possible. Put most of your logic in the controller methods.
  7. List some Rails scaffold datatypes?
    Ans: string, text, integer, float, boolean, date, datetime
  8. Give the hex color codes for these standard HTML colors:
    aqua black blue fuschia gray green lime maroon 
    navy olive purple red silver teal yellow white
    
    Ans: Look at this color wheel.
  9. What is the difference between an instance variable and a local variable.
    Ans: A local variable is only valid within the method that it is used; an instance variable is value anywhere in the class where it is used. For a Rails project, an instance variable is value anywhere in the controller class.
  10. How can regular expressions be used to validate form input?
    Example: validates :phone, with: { format: /\A\d{3}\/\d{3}-\d{4}/ }
  11. What is a cryptographic hash?
    Ans: It is a one-way encryption algorithm that can be used to encrypt passworks. An example of such an algorithm is MD5. Another name for a cryptographic hash is a digest.
  12. What is the meaning of each of these symbols in Ruby and/or Rails?
    +   -   *   <   &   !  .  $
    
    + addition, string concatenation, array concatenation, += assignment operator
    - subtraction, array difference, -= assignment operator
    * multiplication, string repetition, *= assignment operator, /* ... */ CSS comment
    < less than, <= less than or equal, << array append, <!-- HTML comment, <=> spaceship operator
    & bitwise and, && logical and, &= assignment operator, &lt; HTML special symbol
    ! not, != not equal, <!-- HTML comment
    . member selection operator, .. inclusive range, ... exclusive range
    $ jQuery selector, Ruby prefix for global variable
  13. Find the errors:
  14. <% if Kid.first.gender = 'F' %>
      <% @f %>
    <% else if Kid.first.gender = 'M' =%>
      <% @f %>
    end
    
    --- Corrected Version --------------------
    
    <% if Kid.first.gender == 'F' %>
      <%= @f %>
    <% elsif Kid.first.gender == 'M' %>
      <%= @m %>
    <% end %>
    
  15. What is a migration?
    Ans: It is a specification for creating or modifying a database table. Migration files are stored in db/migrations. To run all pending migrations, use rake db:migrate
  16. How can you correct errors in a migration before running rake db:migrate?
    Ans: Edit the migration file.
  17. True or False: all Ruby methods are public.
    Ans: In general, yes. However, the initialize method is always private. Also methods can be declared to be private with the private keyword. See the bottom of a scaffold-based controller.
  18. How are JavaScript and jQuery related?
    Ans: jQuery is a JavaScript library. It is based on selectors, for example $("p"), $("#btn"), $(".top").
  19. How are jQuery and CoffeeScript related?
    Ans: CoffeeScript is a less wordy version of jQuery, inspired by Ruby, Python, and Haskell. CoffeeScript uses -> to define a function and uses indentation to indicate the body of a function rather than curley braces.
  20. Name 5 methods from the Time class.
    Ans: now year mon day hour min sec dst
  21. Name 5 methods from the Array class.
    Ans: length empty? sort insert << [ ]
  22. Name 3 methods from the Range class.
    Ans: begin end include?
  23. What is WEBrick?
    Ans: It is the web server that ships with Ruby on Rails. It has a reputation of being slow.
  24. In a Rails scaffold-based project, where do you put the CSS statements?
    Ans: In the .scss files located in app/assets/stylesheets.
  25. How does Rails know which layout page to use for a specific view?
    Ans: Rails looks for a layout page in app/views/layouts named controller_name.html.erb (replace controller_name by the name of the controller to which the view belongs). If no such file exists, use the layout page application.html.erb.
  26. What is a partial?
    Ans: It is a piece of HTML and ERB that can be embedded into a view with the render method. The filename of a partial starts with the _ prefix.
  27. What is AJAX. For what is it used?
    Ans: AJAX means Asynchronout JavaScript and XML. It is a set of technologies that allow for partial page refreshes.
  28. How can you retrieve all of the rows in a database table?
    Ans: ModelName.all, where you replace ModelName by the actual name of the ActiveRecord model.
  29. How can you obtain all rows where the salary field is greater than 90000?
    Ans: ModelName.where("salary > 90000").all
  30. How do the HTTP verbs GET, POST, PUT, and DELETE map to the CRUD operations?
    Ans: GET --> Read, POST --> Create, PUT --> Update, DELETE --> Destroy.
  31. Name seven ways of running Ruby code?
    1. IRB
    2. Ruby batch file
    3. ERB page
    4. Controller
    5. Ruby Console
    6. Load script into Ruby console
    7. Seed file
  32. What is the difference between form_tag and form_for for defining a form?
    Ans: In a form_tag form, the control helper methods end in _tag. For FormTagHelper controls, the value from each control must be obtained individually by the controller code using a params statement. With FormHelper methods, a single record of information is obtained by the server, which can be inserted directly into the database.
  33. Where do you put validations?
    Ans: In the model files.
  34. What are some common validation options for the validates method?
    Ans: :format :inclusion :length :numericality :presence
  35. If there is more than one validation error, when attempting to insert a new row into a database, what does Rails do?
    Ans: It shows all of the validation errors.
  36. How do you specify a many-to-many relationship?
    Ans: You put a belongs_to method call in the secondary model and a has_many method call in the primary model.
  37. What does the Array method collect do?
    Ans: It collects together each piece of information returned by the code block and assembles all these pieces into an array. Example:
    a = [2, 3, 4, 5, 6]
    b = a.collect do |n|
      n ** 2
    end
    # Now the value of b is [4, 9, 16, 25, 36] 
  38. What does before_action do?
    Ans: It executes a (usually private) method before executing an action.
  39. What is the session object?
    Ans: A Ruby Hash object that stores information by key. This information is stored for the entire user session (until all browsers are closed).
  40. How do you store information in a session object?
    session[:key] = "piece of data"