MCQs on Working with Strings and Numbers | Ruby

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

  1. What is the output of the following Ruby code: "Hello".length?
    • A) 5
    • B) “Hello”
    • C) 6
    • D) nil
  2. How can you convert a string to lowercase in Ruby?
    • A) .downcase
    • B) .to_lower
    • C) .lowercase
    • D) .to_lowercase
  3. Which method is used to check if a string contains a substring in Ruby?
    • A) .include?
    • B) .contains?
    • C) .has_substring?
    • D) .find_substring?
  4. How do you remove leading and trailing spaces from a string in Ruby?
    • A) .strip
    • B) .trim
    • C) .clean
    • D) .clear
  5. What is the result of "Ruby"[1..3] in Ruby?
    • A) “uby”
    • B) “Ru”
    • C) “ub”
    • D) “y”
  6. How can you replace a substring in a Ruby string?
    • A) .sub
    • B) .replace
    • C) .change
    • D) .switch
  7. What method can you use to check if a string is empty in Ruby?
    • A) .empty?
    • B) .is_empty?
    • C) .nil?
    • D) .blank?
  8. How do you concatenate two strings in Ruby?
    • A) "hello" + "world"
    • B) "hello".concat("world")
    • C) Both A and B
    • D) concat("hello", "world")
  9. Which method returns the index of a substring in a string in Ruby?
    • A) .index
    • B) .find
    • C) .locate
    • D) .search
  10. What is the result of "Ruby"[0, 3]?
    • A) “Ruby”
    • B) “Rub”
    • C) “y”
    • D) “Ru”

2. Interpolation and Concatenation

  1. How is string interpolation performed in Ruby?
    • A) "Hello #{name}"
    • B) "Hello + name"
    • C) "Hello [name]"
    • D) "Hello {name}"
  2. What is the output of the following Ruby code: name = "Alice"; "Hello #{name}"?
    • A) “Hello Alice”
    • B) “Hello #{name}”
    • C) “Hello”
    • D) “Alice”
  3. 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
  4. How do you insert a variable into a string in Ruby?
    • A) Use #{variable}
    • B) Use + variable
    • C) Use []
    • D) Use () variable
  5. Which of the following is NOT a valid Ruby string concatenation method?
    • A) + operator
    • B) .concat
    • C) .append
    • D) .push
  6. 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
  7. 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"
  8. What is the output of "Hello" + " " + "World" in Ruby?
    • A) “HelloWorld”
    • B) “Hello World”
    • C) “Hello”
    • D) “WorldHello”
  9. Which method is used to replace a substring within a string in Ruby?
    • A) .replace
    • B) .change
    • C) .sub
    • D) .interpolate
  10. 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)

  1. What is the result of 5 + 3 * 2 in Ruby?
    • A) 16
    • B) 11
    • C) 13
    • D) 23
  2. How can you convert a string to an integer in Ruby?
    • A) .to_i
    • B) .to_int
    • C) .convert
    • D) .integer
  3. What is the result of 10 / 3 in Ruby?
    • A) 3.333333
    • B) 3
    • C) 10
    • D) 3.0
  4. Which method is used to round a number in Ruby?
    • A) .round
    • B) .floor
    • C) .ceil
    • D) .truncate
  5. What is the output of 5 == 5 in Ruby?
    • A) false
    • B) true
    • C) 0
    • D) 1
  6. Which operator checks if two numbers are equal in Ruby?
    • A) ==
    • B) ===
    • C) =
    • D) =
  7. How do you perform floating-point division in Ruby?
    • A) Use /
    • B) Use //
    • C) Use %
    • D) Use .
  8. What is the result of 3.5.to_i in Ruby?
    • A) 4
    • B) 3
    • C) 5
    • D) 3.5
  9. What method would you use to convert a float to a string in Ruby?
    • A) .to_s
    • B) .convert
    • C) .to_str
    • D) .stringify
  10. What is the result of 5 > 3 in Ruby?
    • A) false
    • B) true
    • C) 5
    • D) 3

Answers

QnoAnswer
1A) 5
2A) .downcase
3A) .include?
4A) .strip
5C) “ub”
6A) .sub
7A) .empty?
8C) Both A and B
9A) .index
10B) “Rub”
11A) "Hello #{name}"
12A) “Hello Alice”
13A) It returns a new string with both concatenated
14A) Use #{variable}
15D) .push
16A) Adds a character to the end of a string
17C) "Hello" + 5.to_s
18B) “Hello World”
19C) .sub
20A) \n
21B) 11
22A) .to_i
23B) 3
24A) .round
25B) true
26A) ==
27A) Use /
28B) 3
29A) .to_s
30B) true

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top