Elance Ruby on Rails Test Answers
·
What command do you run to undo the last 5 migrations
database_undo
rake db:rollback
rake
db:rollback STEP=5
rake db:drop
Which of the following controller methods is NOT properly paired with its HTTP counterpart?
None of these
SHOW
for PUT
CREATE for POST
DESTROY for DELETE
INDEX for GET
How do you render a variable in ERB template?
<% variable_name %>
<%= "variable_name" =%>
<%= '#{variable_name}' =%>
<%=
variable_name =>
A correct example of class inheritance from "Exception" into "Bomber"
class
Bomber < Exception def initialize # Instance end end
class Bomber(Exception) def initialize # Instance end end
class Bomber << Exception def initialize # Instance end end
Which of the following associations does NOT declare a many-to-many relationship?
has_many :through
has_and_belongs_to_many
has_many
In a Rails migration, what's the syntax for creating a table?
sudo gem install make_table
create_table => :table_name
rake db:create TableName
create_table
:table_name
How would you check to see if an array named COLORS contains the the value 'red'?
COLORS.has?('red')
COLORS.include?('red')
'red'.includedIn?(COLORS)
COLORS.contains?('red')
Which is NOT a reserved Ruby logic flow word?
else
break
if
elsif
elseif
How do you remove a column during a migration?
remove_column => :column_name
drop_column :column_name
remove_column
:table_name, :column_name
remove_column :column_name
which one is class method?
class A def a end end
class
A def self.a end end
Which of these javascript frameworks became the default with the release of Rails 3.1?
jQuery
rails-script
rails.js
Prototype
Which is NOT a model validation method?
validates_presence_of
validates_numericality_of
validates_uniqueness_of
validates_length_of
validates_form_of
REST stands for...
Request Example Synchronized Test
Related Entity Simple Technology
REpresentational
State Transfer
REquired STate
How can you create a new Rails project?
'rails create /path/to/new/app'
'rails /path/to/new/app'
'rails
new /path/to/new/app'
What is the preferred method of validating that the name has been set?
validates
:name, :presence => true
validates :name => not_null
before_save if name != nil return true end return false end
save!
Which of these does NOT correctly return the version of Ruby?
-VERSION
--version
-v
If you want the /person url to map to your dog controller and show method, what do you add to your routes?
map.resources :person => :dog
match
'/person', :to => 'dog#show'
map.resources :person
map.resources :dog
Which of the following prints the "Hello WORLD!" output with a new line?
disp( "Hello WORLD!" )
puts
"Hello WORLD!"
print "Hello WORLD!"
Which of these statements will utilize the gem named 'mygem'?
import 'mygem'
require
'mygem'
sudo 'mygem'
utilize 'mygem'
instantiate 'mygem'
An instance variable has a name beginning with___________.
@@
@
%
$
The recommended directory in which to place your app's javascript files is:
assets/javascripts
app/assets/javascripts
public/javascripts
app/javascripts
Which of the following will interpolate within a string with the variable named 'monster'?
{monster}
"{monster}"
"/monster"
"#{monster}"
Which of these is not a standard directory in a Rails application?
app
lib
config
All
of these are standard directories
db
What ORM does Ruby on Rails use by default?
ActiveRails
MySQL
SQL
ActiveRecord
ActiveSupport
Which is NOT a default Rails environment?
development
test
sandbox
production
Which extension is default for views html templates?
.haml
.rabl
.erb
.slim
What command do you run to update your database?
rake db:rollback
rails update
rake
db:migrate
database_update
Migrations...
are considered poor programming and are not used significantly in Rails.
modify
a database by adding or removing columns or tables.
back up a project by copying it to another location.
can never be reversed.
generally affect only views and sometimes controllers.
What command do you run to create your database?
rake db:migrate
rake
db:create
rails create
database_create
In the MVC pattern:
MVC are the initials of the creator, Michael Vincent Clantor.
mySQL is the database, vi is the text editor, and C++ is the language.
manipulators handle data, validators ensure data integrity, and
communicators transfer information between them.
models
represent the data, views display the data, and controllers respond to user
interactions
What is the command to install Rails?
gem
install rails
gem install ruby_on_rails
gem install ror
What command do you run to drop your database?
rake db:migrate
rails destroy
database_drop
rake
db:drop
Ruby uses:
null
nil
What is the output of the following ? s="foo" * 2 puts s
foo
foofoo
Gives an Error
foo*2
What is the default value for a global variable (before initialization)?
nul
nil
null
0
What is a typical file extension found in the app/controllers directory
.rb
.html.erb
.yml
.erb
What is the use of the 'defined?' method?
To find whether it is a constant or variable
To find the memory allocated to that variable
To
determine if the variable is defined at the current scope
To find the value of a variable
Determine the value of the variable x after the execution of the following code. x = [1,2,3] x.pop until x.empty? x.push(4) while x.empty?
[4]
[1,2,3,4]
[1,2,3]
[1]
Which of the following is a working ruby while-loop?
while i < 100: # DO THINGS i += 1 end
while i < 100 do # DO THINGS i += 1
while
i < 100 do # DO THINGS i += 1 end
Which of the following files is used to specify any default data that should be loaded into the application's database, when it is first setup?
db/migrate/inital_load.rb
db/default_data.rb
db/migrate/default_data.rb
db/seeds.rb
Which of the following will delete the key-value 1 from array: "big_data"?
big_data.delete(1)
big_data.del(1)
big_data.del[1]
Imagine that you have two models: "User" and "Book", and a user can have only one book. How should the association look like in the "User" model?
belongs_to :book
has_one :books
has_one
:book
has_many: books
How do you create a new user object with the name david and save it to the database?
User.make(:name => 'david')
User.build(:name => 'david')
User.new(:name => 'david')
User.create(:name
=> 'david')
If you want to append an item to an array, what is the standard method?
arr.append(item)
arr
<< item
arr.map(item.clone)
arr.length = arr.length+1 arr[length-1] = item
What part of a migration runs during a rollback?
self.up
The entire migration
nothing
self.down
Which is NOT a Ruby operator?
&&
||
<=
=!
!=
What is the most elegant way to include a javascript file needed for only one page?
<script type='text/javascript' src='FILE NEEDED'></script>
Application.layout.Javascripts << "FILE NEEDED"
:render => {:javascript => 'FILE NEEDED'}
<%=
javascript_include_tag "FILE NEEDED" %>
If you have posts and each post has_many comments, how do you structure your routes?
resources :posts, :comments
resources
:posts do resources :comments end
resources :comments do resources :posts end
If you want the standard restful routes for your person controller, how do you add that?
resources :person do member do post :short end collection do get :long
end end
response.map{|person| => :restful}
map.person => {:get, :post, :put}
resources
:people
Of the following, which would create a new project without unit test?
$
rails new example_app -T
$ rails new example_app -O
$ rails new example_app --database=postgresql
$ rails new example_app --skip-bundle
If you create PostsController, where will Rails look for its templates by default?
app/views
app/templates/posts
app/views/templates
app/templates
app/views/posts
Where will routes.rb file appear?
config/routes.rb
app/config/routes.rb
app/routes.rb
db/routes.rb
Which is NOT an ActiveRecord query method?
group
has
where
order
select
Which command, when run, will compile all assets in the 'app/assets' directory and copy compiled versions to a configured target directory ('public/assets' by default)?
rake assets:compileanddeploy
rake assets:deploy
rake
assets:precompile
rake assets:compile
Which is an incorrectly defined method?
def
method_name() # method operations
def method_name # method operations end
def method_name() # method operations end
Functional tests are used to test what part of a Rails application?
Views
Controllers
Javascript
Models
Which of the following will check the array named "big_data" for the presence of a key "icecream"?
big_data.has_key?("icecream")
big_data.has_key["icecream"]
big_data.has_key?["icecream"]
Where do you add named scopes?
Models
ApplicationController
Views
Migrations
If you want to display the price of an item, with a :time_span tag, where would you place this code?
view or model
model or migration
migration or helper_function
helper_function
or view
What is not a RESTful controller action?
update
insert
index
edit
Which command would generate a DogController with a 'bark' action?
rails generate Dog:controller bark:action
rails g c Dog bark
rails
g controller Dog bark
rails create controller Dog bark:action
rails g controller Dog
When rendering a partial on a collection, what is the recommended method?
<% @collection.each do |item| %> <%= render :partial =>
'partial_to_render', item %> <% end %>
<%=
render :partial => 'partial_to_render', :collection => @collection %>
<%= render :partial => 'partial_to_render', :source =>
@collection.each %>
Unit tests are used to test what part of a Rails application?
Helpers
Views
Controllers
Models
Which of these is NOT a default subdirectory of the app/ directory?
All of these are subdirectories of the app/ directory
controllers
models
helpers
environments
A class variable (not class level instance variable) has a name beginning with ___________.
@
%
@@
$
As of Rails 3 and later, where is the default location for code that does environment-specific configuration?
/config/environments/[environment_name].rb
/config/locales/[environment_name].rb
/config/environments.rb
/config/application.rb
/config/environment.rb
The session is, by default, accessible to:
controllers
and views
controllers and models
models and views
controllers only
models only
What is the main step to add ajax call in Ruby on Rails?
remote:
true
remote => true
:remote = true
:remote :true
In the context of unit testing Rails applications, "fixture" refers to which of the following?
Predefined
data for populating the testing database
The result expected from a passing unit test
The required action to get a unit test to pass
Fields which remain fixed across all records in the testing database
Ruby 1.9, what new method of building key-value pairs is supported?
=>
:
:=
Which of the following is a correctly formatted multi-line comment?
/# COMMENT #/
""" COMMENT """"
### COMMENT ###
=begin
COMMENT =end
/ COMMENT
Which does NOT append "nine" to the array: "big_data"?
big_data.push("nine")
big_data.append("nine")
big_data << "nine"
Which of these is not a valid form helper?
password_field
text_field
hidden_field
radio_button
text_box
What is the output of the following? @@x = 10 puts defined? @@x
class
variable
global-variable
instance-variable
local-variable
What is the output of the following? puts 'a\nb'
anb
a b
a/b
a\nb
String objects are _______.
Immutable
Mutable
Neither of these
Ruby supports single inheritance, multiple inheritance, or both?
It does not support inheritance
Single
Inheritance
Both
Multiple Inheritance
What is not a proper REST verb?
update
put
delete
get
post
Which of these is NOT a valid way of associating models?
has_and_belongs_to_many
belongs_to
has_and_belongs_to
has_one
has_many
When you've got a form, and when the user submits invalid data, you redirect back to the edit form. If you want to show a general error message (not tied to validation errors) to the user, you'd do which of the following?
In controller: flash.error = message
At the top of page: <%= message %>
In controller: flash.now.error = message
In
controller: flash[:error] = message
In controller: flash.now[:error] = message
Which is NOT an ActiveRecord migration method?
add_index
add_column
add_key
change_table
drop_table
Which column type is NOT supported by Active Record?
float
binary
blob
datetime
decimal
A global variable has a name beginning with:
@@
$
%
@
What is the output of the following? $x = 10 puts defined? $X
nil
local-variable
instance-variable
class variable
How does the Asset Pipeline (Rails 3.1 +) deal with different precompiled versions of an asset?
Append
MD5 digest to filename at precompile
Append version number to filename at precompile
Append random 32 character string at precompile
Append datetime to filename at precompile
To randomize the order of entries in an Array, use the _______ method.
shuffle
sample
scramble
random
sort
For your app to route the "http://myapp.com/" to the home controller, index action; what could you add to your routes.rb file?
root_to "home", "index"
root => "home/index"
root
:to => "home#index"
root => "home#index"
Which expression will not return a sum of array elements in Ruby on Rails?
array.inject { |sum, e| sum + e }
None
of them. All will return the sum.
array.inject(:+)
array.sum
sum = 0; array.each { |e| sum += e }; sum
What kind of variables are Author and AUTHOR?
global
local
class
constant
Which HTTP method is used by default when clicking a button defined using the ActionView helper method 'button_to'?
put
post
delete
get
Which of these code blocks cannot be right?
= form_for current_user do |f|
= form_for @user do |f|
= form_for :user do |f|
= simple_form_for @user do |f|
=
form_for User do |f|
What is the proper way to subclass a Module?
module SuperModule < SubModule; end
module SuperModule < SubModule;
module SuperModule << SubModule; end
You
cannot subclass a Module.
module SuperModule << SubModule;
If a method is protected:
It may be called only by the instance of its subclasses
It may not be called by any instances.
It may be called only by the instance of defining class
It
may be called by any instance of the defining class or its subclasses
What is the ActionView form helper tag for <input type="text" name="foo" id="foo"/>
<%=
text_field_tag "foo" %>
<%= text_input "foo" %>
<%= input_tag :type=>"text", :name=>"foo",
:id=>"foo" %>
<%= text_tag "foo" %>
If the class User has a belongs_to :role, which table has the foreign key?
Both
Role
Neither
User
What does Model.reset_column_information do?
Resets the column names of the table.
Resets the data type of the column.
Resets the existing records with the provided value.
Resets the index of the corresponding table.
Resets
all the cached information about columns.
Which of the following are valid objects of class Integer or one of its subclasses? 1) -123 2) 0xFF 3) 123_456_789 4) 123456789123456789123456789123456789
1
1 and 4
All
of the above
4
link_to('link text', url, :remote => true) does which of the following?
Creates an HTML link that will be inactive if the user's browser has
javascript disabled
Creates a link to url with an "onclick" HTML attribute that
results in an AJAX call when user clicks
Creates
a link to url with HTML attribute data-remote="true"
Creates an HTML link with 'link text' as text and url as the href
In development mode (config.assets.digest = false), if a file exists in app/assets/javascript/hello.js, which link will show that file ?
localhost:3000/hello.js
localhost:3000/assets/javascript/hello.js
localhost:3000/assets/hello.js
localhost:3000/assets/application.js
Determine the value of the variable a, b and c after the execution of the following code. a, b, c = 1, 2, 3, 4
a = 1 b = 2 c = [3, 4]
a = 1 b = [2,3] c = 4
a
= 1 b = 2 c = 3
a = [1, 2] b = 3 c = 4
What's an equivalent way of performing the following (h is a Hash): h.each { |k,v| h.delete k if k.nil? }
h.compact!
The original will result in a warning: multiple values for a block
parameter
The original will raise a NoMethodError
h.keys.compact!
h.delete_if
{ |k,v| k.nil? }
In The Author Model, how do you create a nested association between Authors and Blogs?
has_many :blog
:author has_many :blog
has_many
:blogs
has_many :blogs, :through => :author
:blog has_many :author
All Ruby number objects are instances of class _____.
BigDecimal
Fixnum
Numeric
Bignum
Which is not a valid callback?
after_save
after_validation
before_update
before_destroy
after_delete
Handling an AJAX request in controller, which of the following redirects to google?
render
js: "window.location = 'http://google.com'"
All of these
redirect_to 'http://google.com'
redirect_javascript "http://google.com"
If you're using the standard Rails RESTful routes, then which of the following actions map to the url, '/posts'?
posts#destroy_all or posts#index
posts#show or posts#index or posts#create
posts#all or posts#index
posts#create
or posts#index
posts#update or posts#index or posts#edit
Counter caches can be used for:
Caching the results of Model.count queries to bypass hitting the
database
Saving space in the database by consolidating count query results
Caching arbitrary query results (often of the form Model.count)
Caching
the counts of associations to avoid unnecessary Model.count queries
Flushing caches when models are modified
What should be the standard table name for has_and_belongs_to_many relationship between teams and users?
users_and_teams
team_users
teams_and_users
teams_users
team_user
Which of the following is a built-in feature of ActiveRecord
Event triggers
Materialized views
Stored procedures
Writeonly records
Enums
What is the output of the following ? person1 = "Tim" person2 = person1 person1[0] = 'J' puts person1 puts person2
Jim
Jim
Tim Jim
Jim Tim
Tim Tim
You've got a form, and when the user submits invalid data, you simply show them an error page (no redirect). In the controller, to show the general error on the rendered page, you would add:
At the top of page: <%= message %>
In
controller: flash.now[:error] = message
In controller: flash[:error] = message
In controller: flash.error = message
In controller: flash.now.error = message
If your controller gets an action that it will render a template for, and you need to add a flash notice to the page, you'd use:
flash.now.notice(notice_message)
flash[:notice] = notice_message
flash.now[:notice]
= notice_message
flash.notice(notice_message)
flash = notice_message
Choose the correct result p a = 1, a = 2
"12"
[1,2]
1
1
//line break// 2
2
To establish an association from a Post model to another Post model (say if one post is in response to another and there is a parent_post_id column on the posts table), you would add the following to the Post model:
belongs_to
:parent_post, :class_name => 'Post'
belongs_to :post, :foreign_key => 'parent_post_id'
has_one :parent_post, :class_name => 'Post'
has_many :parent_posts, :foreign_key => 'parent_post_id'
has_one :parent_post_id, :class_name => 'Post'
Given the following code: ; module Wheeled; end ; class Vehicle; end ; class Car < Vehicle ; include Wheeled ; end ; What is the value of this expression: Car.new.kind_of? Wheeled
nil
Vehicle
true
false
no value (raises ArgumentError)
Which one IS NOT an application server:
Ebb
Unicorn
Rainbows
Thin
Tomahawk
What is Strong Parameters in rails 4 ?
It validates attributes from end-user assignment.
It
provides an interface for protecting attributes from end-user assignment.
This makes Action Controller parameters forbidden to be used in Active
Model mass assignment without whitelisted.
Which of the following will return false?
(1..10) != 1
(1...5)
=== 5
123 == 123.0
All of these
What is the output of the following? a = (1 <=> 2) b = (1 <=> 1.0) c = (b <=> a) puts c
1
FALSE
2
0
Which of the following are NOT Ruby keywords? 1) alias 2) yield 3) defined? 4) include?
2 and 3
3
4
1 and 2
We have given: str = 'abcdef' Which of the following will return the string 'def'? 1) str[0,-3] 2) str[-3,3] 3) str[4,3] 4) str['def']
1
2
and 4
1 and 2
2
Which of the following will return true?
false.class.superclass
== Object
true.is_a? Boolean
true.class == Boolean
all of these
Initialize method is always:
protected
private
default
public
How can you get a list of all available rails generators?
rails generate --tasks
rails
generate
rake generate --tasks
rails g --list
rake generate
The index_by method is...
an ActiveRecord helper method for fetching results in groups of size
specified by the argument
an ActiveRecord migration helper method for creating database table
indexes
a
Ruby on Rails method to create a Hash from an Array with keys computed by the
block argument
a standard ruby method on Array for creating a Hash index from an Array
with keys computed by the block argument
not a method in standard Ruby on Rails
Which of the following is not a standard validates option?
:unique
:exclusion
:presence
:confirmation
What is the output of following? 1==(0||1)
false
true
Subscribe to:
Posts (Atom)