Master Ruby’s String and Number Manipulation with this comprehensive chapter on string basics, manipulation techniques, interpolation, concatenation, and number operations, including arithmetic, conversion, and comparison in Ruby.
1. String Basics and Manipulation
What is the output of the following Ruby code: "Hello".length?
A) 5
B) “Hello”
C) 6
D) nil
How can you convert a string to lowercase in Ruby?
A) .downcase
B) .to_lower
C) .lowercase
D) .to_lowercase
Which method is used to check if a string contains a substring in Ruby?
A) .include?
B) .contains?
C) .has_substring?
D) .find_substring?
How do you remove leading and trailing spaces from a string in Ruby?
A) .strip
B) .trim
C) .clean
D) .clear
What is the result of "Ruby"[1..3] in Ruby?
A) “uby”
B) “Ru”
C) “ub”
D) “y”
How can you replace a substring in a Ruby string?
A) .sub
B) .replace
C) .change
D) .switch
What method can you use to check if a string is empty in Ruby?
A) .empty?
B) .is_empty?
C) .nil?
D) .blank?
How do you concatenate two strings in Ruby?
A) "hello" + "world"
B) "hello".concat("world")
C) Both A and B
D) concat("hello", "world")
Which method returns the index of a substring in a string in Ruby?
A) .index
B) .find
C) .locate
D) .search
What is the result of "Ruby"[0, 3]?
A) “Ruby”
B) “Rub”
C) “y”
D) “Ru”
2. Interpolation and Concatenation
How is string interpolation performed in Ruby?
A) "Hello #{name}"
B) "Hello + name"
C) "Hello [name]"
D) "Hello {name}"
What is the output of the following Ruby code: name = "Alice"; "Hello #{name}"?
A) “Hello Alice”
B) “Hello #{name}”
C) “Hello”
D) “Alice”
What happens when you use the + operator to concatenate two strings in Ruby?
A) It returns a new string with both concatenated
B) It modifies the first string
C) It raises an error
D) It repeats the string
How do you insert a variable into a string in Ruby?
A) Use #{variable}
B) Use + variable
C) Use []
D) Use () variable
Which of the following is NOT a valid Ruby string concatenation method?
A) + operator
B) .concat
C) .append
D) .push
What does the << operator do in Ruby?
A) Adds a character to the end of a string
B) Subtracts characters from a string
C) Reverses the string
D) Finds a substring
How would you concatenate a string and a number in Ruby?
A) "Hello " + 5
B) "Hello " + "5"
C) "Hello" + 5.to_s
D) "Hello 5"
What is the output of "Hello" + " " + "World" in Ruby?
A) “HelloWorld”
B) “Hello World”
C) “Hello”
D) “WorldHello”
Which method is used to replace a substring within a string in Ruby?
A) .replace
B) .change
C) .sub
D) .interpolate
How do you insert a newline character in a string in Ruby?
A) \n
B) \t
C) \r
D) \b
3. Number Operations (Arithmetic, Conversion, Comparison)
What is the result of 5 + 3 * 2 in Ruby?
A) 16
B) 11
C) 13
D) 23
How can you convert a string to an integer in Ruby?
A) .to_i
B) .to_int
C) .convert
D) .integer
What is the result of 10 / 3 in Ruby?
A) 3.333333
B) 3
C) 10
D) 3.0
Which method is used to round a number in Ruby?
A) .round
B) .floor
C) .ceil
D) .truncate
What is the output of 5 == 5 in Ruby?
A) false
B) true
C) 0
D) 1
Which operator checks if two numbers are equal in Ruby?
A) ==
B) ===
C) =
D) =
How do you perform floating-point division in Ruby?
A) Use /
B) Use //
C) Use %
D) Use .
What is the result of 3.5.to_i in Ruby?
A) 4
B) 3
C) 5
D) 3.5
What method would you use to convert a float to a string in Ruby?