All featured quizzes

Ruby - Basics

Boolean evaluation of nil, bundler gem installation, puts output, blocks, and instance variables.

Sample Questions

A preview of what's in this quiz. Answer them interactively for instant scoring and full explanations.

What is the prefix character for a Class Variable in Ruby?

1. What is the prefix character for a Class Variable in Ruby?

  • A.@
  • B.@@
  • C.$
  • D.No prefix
What is the prefix character for an Instance Variable in Ruby?

2. What is the prefix character for an Instance Variable in Ruby?

  • A.@@ (class variables)
  • B.@
  • C.$ (global variables)
  • D.No prefix (local variables)
How do you declare a Local Variable in Ruby?

3. How do you declare a Local Variable in Ruby?

  • A.var x = 5
  • B.x = 5
  • C.let x = 5
  • D.local x = 5
What is the prefix character for a Global Variable in Ruby?

4. What is the prefix character for a Global Variable in Ruby?

  • A.@
  • B.$
  • C.@@
  • D.No prefix
What is a key philosophy of the Ruby programming language regarding data values?

5. What is a key philosophy of the Ruby programming language regarding data values?

  • A.Numbers and strings are primitives, not objects.
  • B.Everything is an object, including numbers and booleans.
  • C.Objects only exist inside modules.
  • D.Ruby does not support object-oriented programming.
What is the default return value of a Ruby method if no `return` keyword is explicitly used?

6. What is the default return value of a Ruby method if no `return` keyword is explicitly used?

  • A.nil
  • B.The value of the last evaluated expression inside the method
  • C.void
  • D.true
What is a Symbol in Ruby?

7. What is a Symbol in Ruby?

  • A.A local variable starting with capital letters.
  • B.An immutable, unique name represented with a colon prefix, e.g., `:my_symbol`.
  • C.A database connection string alias.
  • D.A syntax tag used for comments.
What is the name of the interactive command-line shell shipped with Ruby?

8. What is the name of the interactive command-line shell shipped with Ruby?

  • A.ruby-shell
  • B.irb
  • C.pry (a popular third-party REPL, but not the default shipped with Ruby)
  • D.rsh
How do you define a method in Ruby?

9. How do you define a method in Ruby?

  • A.function my_method; end
  • B.def my_method; end
  • C.fn my_method; end
  • D.method my_method; end
How do you create an array in Ruby?

10. How do you create an array in Ruby?

  • A.my_array = array(1, 2, 3)
  • B.my_array = [1, 2, 3]
  • C.my_array = {1, 2, 3}
  • D.my_array = Array.new{1, 2, 3}
What is a Hash in Ruby?

11. What is a Hash in Ruby?

  • A.An ordered array of strings.
  • B.A collection of unique key-value pairs, e.g. `{ key: value }`.
  • C.A cryptographic security class.
  • D.A file parsing module.
What is a key difference between a Symbol and a String in Ruby?

12. What is a key difference between a Symbol and a String in Ruby?

  • A.Strings are immutable; Symbols are mutable.
  • B.Symbols are immutable and reuse the same memory address; Strings are mutable and create new objects.
  • C.Symbols can only hold numeric values; Strings hold text.
  • D.There is no difference in memory or mutability.
How do you append an item to the end of an array using Ruby's shovel operator?

13. How do you append an item to the end of an array using Ruby's shovel operator?

  • A.arr.push(4) (while valid, `<<` is the specific shovel operator)
  • B.arr << 4
  • C.arr += 4
  • D.arr.append(4)
Which loop structure executes its block code UNTIL a condition becomes TRUE?

14. Which loop structure executes its block code UNTIL a condition becomes TRUE?

  • A.while
  • B.until
  • C.for
  • D.loop
What is a Proc in Ruby?

15. What is a Proc in Ruby?

  • A.A running process inside OS thread queues.
  • B.An object wrapper around a block of code, created via `Proc.new`.
  • C.A helper class managing database queries.
  • D.A compiler validation check.
How do you check the number of elements in an array or hash in Ruby?

16. How do you check the number of elements in an array or hash in Ruby?

  • A.arr.count() (count exists but iterates, length/size are preferred for simple sizing)
  • B.arr.length or arr.size
  • C.len(arr)
  • D.arr.count_keys
What is the result of accessing a non-existent key in a Hash by default?

17. What is the result of accessing a non-existent key in a Hash by default?

  • A.An exception is thrown.
  • B.nil
  • C.0
  • D.false
What is a Block in Ruby?

18. What is a Block in Ruby?

  • A.A class definition file.
  • B.A anonymous chunk of code enclosed in `do...end` or `{}` passed to methods.
  • C.A database table record block.
  • D.A syntax tag separating classes.
Which keyword is used inside a method to execute a block passed to that method?

19. Which keyword is used inside a method to execute a block passed to that method?

  • A.call
  • B.yield
  • C.block.run
  • D.execute
What is the difference between `do ... end` and curly braces `{ ... }` for defining blocks?

20. What is the difference between `do ... end` and curly braces `{ ... }` for defining blocks?

  • A.Braces create Procs; `do ... end` creates lambdas.
  • B.Braces have higher precedence; braces are preferred for single-line blocks.
  • C.They are parsed by different compiler threads.
  • D.There is no difference in precedence.

Looking for a different topic? Browse all subjects