Intermediate Ruby test (Intermediate)
30 questions · 35 minutes · Free
Modules, mixins, Rails basics and testing.
Take the Intermediate Ruby test
What it covers
- Classes, modules and mixins
- Procs and lambdas
- Rails MVC
- ActiveRecord basics
- RSpec
Interview format
- Questions: 30 questions — 18 multiple choice, 9 multi-answer, 3 predict-the-output
- Time: 35 minutes, timed from the first question
- Difficulty: Intermediate
- Skill area: Backend
- Price: Free
Sample questions
5 sample questions from the Intermediate Ruby test, with the answers. Samples are taken out of the live test, so you will not meet these when you sit it.
A Rails application is encountering a routing error when trying to access a specific controller action. Which of the following is the most likely cause of this issue?
- The controller action is not defined with the correct HTTP verb (e.g., get, post, etc.).
- The controller action is not using the correct helper method.
- The controller action is using an incorrect model association.
- The controller action is rendering the wrong view template.
Answer: A. The controller action is not defined with the correct HTTP verb (e.g., get, post, etc.).
In Rails, controller actions are mapped to specific HTTP verbs using routes. If the action is not defined with the correct verb, the request will not be handled properly, leading to a 404 or other routing error. The other options describe unrelated issues, such as incorrect view rendering, incorrect model associations, or incorrect use of helpers, which do not directly relate to the HTTP verb mismatch in the controller action.
What is the behavior of the RSpec describe block when it is executed?
- It runs the block for each example in the spec suite.
- It returns the number of examples defined in the block.
- It returns an empty object and does not execute the block.
- It runs the block once and returns the result of the last expression in the block.
Answer: D. It runs the block once and returns the result of the last expression in the block.
In RSpec, the describe block is used to group related tests. It runs the block once and returns the result of the last expression in the block, which is typically the result of the tests defined within it. The other options are incorrect because they describe behaviors of other constructs such as it blocks, before/after hooks, or the outcome of test execution, not the describe block itself.
Which of the following is the correct RSpec syntax to check that a User object has a name attribute with the value 'Ria'?
- expect(User.new).to eq(name: 'Ria')
- expect(User.new).to be_truthy(name: 'Ria')
- expect(User.new).to have_attributes(name: 'Ria')
- expect(User.new).to be(name: 'Ria')
Answer: C. expect(User.new).to have_attributes(name: 'Ria')
In RSpec, the correct syntax to check attributes of an object is to use have_attributes with a hash of expected values. Option A is incorrect because it uses be instead of have_attributes. Option C is incorrect because it uses eq, which is for direct equality, not attribute matching. Option D is incorrect because it uses be_truthy, which checks for truthiness, not attribute values.
Which of the following is the correct approach when deciding between using `where` and `find_by` in ActiveRecord query methods?
- Use `where` when you need a single record and don't need to chain further methods and `find_by` when you need to chain additional query methods.
- Use `where` when you need to chain additional query methods and `find_by` when you need a single record and don't need to chain further methods.
- Use `where` and `find_by` interchangeably because they both return the same type of object.
- Use `where` when you need to chain additional query methods and `find_by` when you need to chain additional query methods.
Answer: B. Use `where` when you need to chain additional query methods and `find_by` when you need a single record and don't need to chain further methods.
The `where` method returns an ActiveRecord::Relation object, which allows for further query method chaining. On the other hand, `find_by` returns a single record (or nil) and is not chainable. Therefore, the correct choice is to use `where` when chaining is needed and `find_by` for single record retrieval without further chaining.
What is the difference between a lambda and a proc in Ruby when it comes to returning control?
- A lambda returns from the enclosing method, while a proc returns immediately from the proc.
- A lambda returns immediately from the lambda, while a proc returns from the enclosing method.
- A lambda and a proc behave identically when it comes to returning control.
- A lambda is a type of proc, and they both return from the enclosing method.
Answer: B. A lambda returns immediately from the lambda, while a proc returns from the enclosing method.
Lambdas and procs both capture blocks of code, but they differ in how they handle returns. A lambda behaves like a method, returning control to the caller of the lambda. A proc, on the other hand, returns from the enclosing method, which can lead to unexpected behavior if not handled carefully.
Frequently asked questions
- How many questions are in the Intermediate Ruby test?
- 30 questions (18 multiple choice, 9 multi-answer, 3 predict-the-output), with 35 minutes to answer them.
- What does the Intermediate Ruby test cover?
- Classes, modules and mixins, Procs and lambdas, Rails MVC, ActiveRecord basics, RSpec.
- Is the Intermediate Ruby test free?
- Yes. It is free to take with a readyforhires account.
- What level is the Intermediate Ruby test?
- Intermediate, in Backend.