πŸ“Š Ruby
Q. How do you create a hash (dictionary) in Ruby?
  • (A) { key1: 'value1', key2: 'value2' }
  • (B) [ key1 => 'value1', key2 => 'value2' ]
  • (C) ( key1: 'value1', key2: 'value2' )
  • (D) < key1: 'value1', key2: 'value2' >
πŸ’¬ Discuss
βœ… Correct Answer: (A) { key1: 'value1', key2: 'value2' }

Explanation: Hashes in Ruby are created using curly braces with key-value pairs.

πŸ“Š Ruby
Q. What is the output of '5.times { |i| puts i }'?
  • (A) 0 1 2 3 4
  • (B) 1 2 3 4 5
  • (C) 5 4 3 2 1
  • (D) Error
πŸ’¬ Discuss
βœ… Correct Answer: (A) 0 1 2 3 4

Explanation: 'times' iterates from 0 up to one less than the number.

πŸ“Š Ruby
Q. Which method is used to convert a string to integer in Ruby?
  • (A) to_int
  • (B) to_i
  • (C) int()
  • (D) convert_to_int
πŸ’¬ Discuss
βœ… Correct Answer: (B) to_i

Explanation: The 'to_i' method converts a string to an integer in Ruby.

πŸ“Š Ruby
Q. Which operator is used for string concatenation in Ruby?
  • (A) +
  • (B) &
  • (C) ++
  • (D) .
πŸ’¬ Discuss
βœ… Correct Answer: (A) +

Explanation: The '+' operator concatenates strings in Ruby.

πŸ“Š Ruby
Q. How do you write an if-else statement in Ruby?
  • (A) if condition then ... else ... end
  • (B) if (condition) { ... } else { ... }
  • (C) if condition: ... else: ...
  • (D) if condition ... end else ... end
πŸ’¬ Discuss
βœ… Correct Answer: (A) if condition then ... else ... end

Explanation: Ruby uses 'if condition then ... else ... end' syntax.

πŸ“Š Ruby
Q. Which of the following is a Ruby symbol?
  • (A) :name
  • (B) 'name'
  • (C) "name"
  • (D) #name
πŸ’¬ Discuss
βœ… Correct Answer: (A) :name

Explanation: Symbols start with a colon ':' in Ruby.

πŸ“Š Ruby
Q. What does the 'attr_accessor' method do in Ruby?
  • (A) Defines getter methods only
  • (B) Defines setter methods only
  • (C) Defines both getter and setter methods
  • (D) Defines class methods
πŸ’¬ Discuss
βœ… Correct Answer: (C) Defines both getter and setter methods

Explanation: 'attr_accessor' creates both getter and setter methods for instance variables.

πŸ“Š Ruby
Q. How do you rescue an exception in Ruby?
  • (A) try ... catch
  • (B) begin ... rescue ... end
  • (C) catch ... rescue
  • (D) try ... except
πŸ’¬ Discuss
βœ… Correct Answer: (B) begin ... rescue ... end

Explanation: Ruby uses 'begin ... rescue ... end' for exception handling.

πŸ“Š Ruby
Q. What does the 'self' keyword represent in Ruby?
  • (A) Current class
  • (B) Current method
  • (C) Current object
  • (D) Global variable
πŸ’¬ Discuss
βœ… Correct Answer: (C) Current object

Explanation: 'self' refers to the current object instance.

πŸ“Š Ruby
Q. How do you add an element to the end of an array in Ruby?
  • (A) array.add(element)
  • (B) array.append(element)
  • (C) array.push(element)
  • (D) array.insert(element)
πŸ’¬ Discuss
βœ… Correct Answer: (C) array.push(element)

Explanation: 'push' method adds an element to the end of an array.