πŸ“Š Ruby
Q. Which of the following is a valid way to start a Ruby program?
  • (A) #!/usr/bin/ruby
  • (B) #!/bin/ruby
  • (C) #!/ruby/bin
  • (D) #!/usr/bin/python
πŸ’¬ Discuss
βœ… Correct Answer: (A) #!/usr/bin/ruby

Explanation: Shebang line #!/usr/bin/ruby is used to run Ruby scripts.

πŸ“Š Ruby
Q. Which method is used to get the length of a string in Ruby?
  • (A) size
  • (B) length
  • (C) count
  • (D) Both size and length
πŸ’¬ Discuss
βœ… Correct Answer: (D) Both size and length

Explanation: Both 'size' and 'length' return the length of a string in Ruby.

πŸ“Š Ruby
Q. Which keyword is used to pass a block in Ruby?
  • (A) do...end
  • (B) begin...end
  • (C) loop...end
  • (D) block...end
πŸ’¬ Discuss
βœ… Correct Answer: (A) do...end

Explanation: Blocks in Ruby are passed using do...end or curly braces {}.

πŸ“Š Ruby
Q. Which method removes the last element from an array in Ruby?
  • (A) pop
  • (B) push
  • (C) shift
  • (D) unshift
πŸ’¬ Discuss
βœ… Correct Answer: (A) pop

Explanation: The pop method removes and returns the last element of an array.

πŸ“Š Ruby
Q. What syntax is used for string interpolation in Ruby?
  • (A) "#{variable}"
  • (B) '#{variable}'
  • (C) ${variable}
  • (D) `#{variable}`
πŸ’¬ Discuss
βœ… Correct Answer: (A) "#{variable}"

Explanation: String interpolation is done inside double quotes with #{variable}.

πŸ“Š Ruby
Q. Instance variables in Ruby start with which symbol?
  • (A) $
  • (B) @
  • (C) @@
  • (D) #
πŸ’¬ Discuss
βœ… Correct Answer: (B) @

Explanation: Instance variables start with the @ symbol.

πŸ“Š Ruby
Q. Class variables in Ruby start with which symbol?
  • (A) $
  • (B) @
  • (C) @@
  • (D) #
πŸ’¬ Discuss
βœ… Correct Answer: (C) @@

Explanation: Class variables start with @@.

πŸ“Š Ruby
Q. Which method returns all the keys from a hash in Ruby?
  • (A) keys
  • (B) values
  • (C) items
  • (D) elements
πŸ’¬ Discuss
βœ… Correct Answer: (A) keys

Explanation: The keys method returns all keys of a hash.

πŸ“Š Ruby
Q. How do you define a symbol in Ruby?
  • (A) 'symbol'
  • (B) :symbol
  • (C) #symbol
  • (D) *symbol
πŸ’¬ Discuss
βœ… Correct Answer: (B) :symbol

Explanation: Symbols are defined by prefixing with a colon (:), e.g., :name.

πŸ“Š Ruby
Q. Which method executes a block passed to a method in Ruby?
  • (A) yield
  • (B) execute
  • (C) run
  • (D) call
πŸ’¬ Discuss
βœ… Correct Answer: (A) yield

Explanation: The yield keyword executes the block passed to a method.