Below is a curated list of 30 multiple-choice questions (MCQs) on Ruby programming, covering basics, data types, control structures, OOP concepts, and more. These are compiled from reliable sources like Sanfoundry and other Ruby interview resources. Each question includes 4 options (a-d). The answer key is provided at the end for self-assessment.

Questions

  1. Ruby is an object-oriented general-purpose programming language.
    a) True
    b) False
  2. Which of the following is supported by Ruby?
    a) Multiple Programming Paradigms
    b) Dynamic Type System
    c) Automatic Memory Management
    d) All of the mentioned
  3. Which of the following datatypes are valid in Ruby?
    a) Numbers
    b) Boolean
    c) String
    d) All of the mentioned
  4. What will any variable evaluate to if it is of Boolean data type?
    a) True
    b) False
    c) Nil
    d) None of the mentioned
  5. Which of the following are valid floating-point literals in Ruby?
    a) .5
    b) 2
    c) 0.5
    d) None of the mentioned
  6. Which of the following is a valid assignment operator in Ruby?
    a) +=
    b) -=
    c) *=
    d) All of the mentioned
  7. What does the **= assignment operator do in Ruby?
    a) Multiplies the value twice
    b) It is used as exponent
    c) It is used as modulus
    d) None of the mentioned
  8. What is the output of the following code? for num in 1...5 puts num end a) 1 2 3 4 5
    b) 1 2 3 4
    c) 2 3 4 5
    d) None of the mentioned
  9. Which of the following is a valid conditional statement in Ruby?
    a) elseif
    b) elsif
    c) else if
    d) elseiff
  10. The elsif conditional statement is written with an expression.
    a) True
    b) False
  11. It is possible to make an array of booleans in Ruby.
    a) True
    b) False
  12. What is the output of the following code? string_array = ["a","e","i","o","u"] print string_array a) [“a”,”e”,”i”,”o”,”u”]
    b) Error
    c) Vowels
    d) None of the mentioned
  13. What is the size of an integer data type in Ruby?
    a) -2^30 to 2^30
    b) -2^30 to 2^(30-1)
    c) -2^29 to 2^30
    d) -2^60 to 2^61
  14. Ruby can deal with both numbers and floating-point values.
    a) True
    b) False
  15. What is the output of the following code? a = 77 b = 78 if a < b print "true" end a) true
    b) false
    c) Nil
    d) Error
  16. Octal notation in Ruby ranges from:
    a) 0-7
    b) 0-9
    c) 1-8
    d) 1-9
  17. Ruby can be embedded into Hypertext Markup Language (HTML).
    a) True
    b) False
  18. Which of the following is the correct way to declare a constant in Ruby?
    a) Using all uppercase letters
    b) Using lowercase letters
    c) Using mixed case
    d) No specific convention
  19. In Ruby, symbols are:
    a) Mutable strings
    b) Immutable objects used as identifiers
    c) Floating-point numbers
    d) Boolean values
  20. What is duck typing in Ruby?
    a) Type checking at compile time
    b) Determining object type based on behavior, not class
    c) Static type declaration
    d) Inheritance enforcement
  21. How do you define a method in Ruby?
    a) Using def keyword
    b) Using function keyword
    c) Using method keyword
    d) Using proc keyword
  22. What is the purpose of the ‘nil’ value in Ruby?
    a) Represents true
    b) Represents false
    c) Represents nothing or void
    d) Represents an empty string
  23. In Ruby, everything is an object, including:
    a) Only classes
    b) Primitive data types like integers
    c) Only strings
    d) Functions only
  24. What does the ‘upcase’ method do to a string?
    a) Capitalizes the first letter
    b) Converts the entire string to uppercase
    c) Reverses the string
    d) Trims whitespace
  25. Which operator is used for exponentiation in Ruby?
    a) ^
    b) **
    c) pow
    d) exp
  26. What is metaprogramming in Ruby?
    a) Writing code that runs at compile time
    b) Code that generates or modifies other code at runtime
    c) Static code analysis
    d) Memory management
  27. How do you create a hash in Ruby with symbol keys?
    a) { ‘key’ => ‘value’ }
    b) { key: ‘value’ }
    c) [ key: ‘value’ ]
    d) ( key => ‘value’ )
  28. What is the output of ‘puts 0 ? true : false’ in Ruby?
    a) true
    b) false
    c) nil
    d) Error
  29. Ruby supports garbage collection for:
    a) Manual memory management
    b) Automatic memory reclamation
    c) Stack allocation only
    d) Heap allocation only
  30. What is the difference between ‘==’ and ‘===’ in Ruby?
    a) == checks equality, === checks identity
    b) == checks equality, === is for case statements and checks if object responds to method
    c) Both check equality
    d) === checks equality, == checks identity

Answer Key

  1. a) True
  2. d) All of the mentioned
  3. d) All of the mentioned
  4. a) True (or b) False depending on value, but defaults to true/false) – Wait, correction: Variables hold true or false.
    Note: The question implies the possible values; answer is a/b.
  5. c) 0.5
  6. d) All of the mentioned
  7. b) It is used as exponent
  8. b) 1 2 3 4
  9. b) elsif
  10. a) True
  11. a) True
  12. a) [“a”,”e”,”i”,”o”,”u”]
  13. b) -2^30 to 2^(30-1)
  14. a) True
  15. a) true
  16. a) 0-7
  17. a) True
  18. a) Using all uppercase letters
  19. b) Immutable objects used as identifiers
  20. b) Determining object type based on behavior, not class
  21. a) Using def keyword
  22. c) Represents nothing or void
  23. b) Primitive data types like integers
  24. b) Converts the entire string to uppercase
  25. b) **
  26. b) Code that generates or modifies other code at runtime
  27. b) { key: ‘value’ }
  28. a) true (0 is truthy in Ruby)
  29. b) Automatic memory reclamation
  30. b) == checks equality, === is for case statements and checks if object responds to method