Ruby Strings Quiz
40 comprehensive questions exploring Ruby string manipulation, interpolation, methods, and mutability — with 16 code examples covering string creation, concatenation, common operations, and escape sequences in this cpp quiz.
Question 1
How do you create a basic string in Ruby?
Question 2
What is string interpolation in Ruby and how does it work?
Question 3
When processing user registration data where names contain apostrophes, which string creation method ensures proper handling of the apostrophe character without escape sequences?
name = 'O'Connor'Question 4
How does Ruby handle string concatenation with the + operator versus the << shovel operator?
Question 5
What happens when you attempt string interpolation in a single-quoted string?
name = 'Ruby'
greeting = 'Hello #{name}'Question 6
In a web application processing form data, how would you safely combine user input with HTML templates using string interpolation?
Question 7
What does the strip method do in Ruby strings and when is it particularly useful?
Question 8
How do you split a comma-separated string into an array while handling potential extra whitespace around values?
data = 'apple, banana , cherry'
fruits = data.split(',').map(&:strip)Question 9
What is the difference between upcase and capitalize methods in Ruby strings?
Question 10
When working with file paths that contain backslashes on Windows systems, how should you handle escape sequences in Ruby strings?
Question 11
What does the chomp method do and how does it differ from strip?
input = "Hello\n"
clean = input.chompQuestion 12
In a data processing application, how would you handle strings containing both single and double quotes that need to be embedded in SQL queries?
Question 13
What happens when you modify a string using the << shovel operator versus the + operator in terms of object identity?
str1 = 'Hello'
str2 = str1 << ' World'
str3 = str1 + ' World'Question 14
How does Ruby handle escape sequences like \n and \t in string literals?
Question 15
When building dynamic SQL queries with user-provided table names, what string method helps prevent SQL injection while maintaining readability?
Question 16
What does the include? method do when working with Ruby strings?
text = 'Hello World'
result = text.include?('World')Question 17
In a multilingual application processing names from different cultures, how should you handle case conversion when the application needs to display names in title case?
Question 18
What is the purpose of the %Q literal syntax in Ruby and when would you choose it over regular double quotes?
Question 19
How does Ruby's string mutability affect memory usage when processing large CSV files with many string operations?
Question 20
When parsing configuration files with key-value pairs separated by equals signs, how would you extract clean key and value strings?
line = ' database_url = http://localhost:5432 '
key, value = line.split('=').map(&:strip)Question 21
What does the reverse method do to Ruby strings and what type of string does it return?
Question 22
In a logging system that needs to format timestamps with user messages, how should you combine static text with dynamic content efficiently?
Question 23
What is the difference between %q and %Q string literals in Ruby?
Question 24
When processing text files with mixed line endings (Unix \n vs Windows \r\n), how should you normalize line endings for consistent processing?
Question 25
How does the squeeze method work on Ruby strings and what problem does it solve?
text = 'Hello World'
clean = text.squeeze(' ')Question 26
In a web scraping application extracting article titles that may contain HTML entities, how should you handle text normalization?
Question 27
What does the start_with? method do and how is it useful for string validation?
Question 28
When building file paths dynamically from user input, how should you handle path separators to ensure cross-platform compatibility?
base_path = '/app/data'
filename = 'report.txt'
full_path = File.join(base_path, filename)Question 29
What is the behavior of the * operator when used with strings and numbers in Ruby?
text = 'Hi'
result = text * 3Question 30
In a content management system processing article slugs for URLs, how should you transform titles containing spaces and special characters?
Question 31
What does the lstrip method do compared to strip in Ruby strings?
Question 32
When processing JSON data that contains escaped quotes within string values, how should you handle the parsing to preserve the original content?
Question 33
What is the purpose of the rstrip method and when would you choose it over strip?
line = "Data\n\n\n"
clean = line.rstripQuestion 34
In a reporting application generating CSV output with quoted fields containing commas, how should you handle string escaping?
Question 35
What does the tr method do in Ruby strings and how does it differ from gsub?
Question 36
When implementing a search feature that needs to match user queries against database content regardless of case, how should you normalize the strings?
Question 37
What is the behavior of the [] slice method when used with ranges on Ruby strings?
text = 'Hello World'
part = text[6..10]Question 38
In a localization system handling text direction for right-to-left languages, how should you approach string reversal compared to left-to-right languages?
Question 39
What does the partition method do when working with Ruby strings and delimiters?
text = 'name: John Doe'
key, sep, value = text.partition(':')Question 40
Considering Ruby's string handling ecosystem as a whole, what fundamental advantage do Ruby strings provide compared to low-level string implementations in other languages?
