Ruby Programming Questions and Answers – Formatting String Methods

This set of Ruby Programming Interview Questions and Answers focuses on “Formatting String Methods”.

1. The .upcase and .capitalize method are used for capitalizing the whole string.
a) True
b) False
View Answer

Answer: b
Explanation: The .capitalize method doesn’t change the whole string to uppercase, it just capitalizes the first letter.

2. What is the use of .capitalize method?
a) It capitalizes the entire string
b) It capitalize on the first letter of the string
c) It capitalize the strings which are in small case
d) All of the mentioned
View Answer

Answer: b
Explanation: It capitalizes only the first letter.

3. What is the role of ! at the end of the capitalize method?
a) It is the syntax for using capitalize method
b) It modifies the value stored in the variable
c) It indicates the termination of string
d) None of the mentioned
View Answer

Answer: b
Explanation: It is used for modifying the variable.
advertisement
advertisement

4. What is the output of the code?

print "What's your first name?"
   first_name=gets.chomp
   a=first_name.capitalize
   first_name.capitalize!
   print "What's your last name?"
   last_name=gets.chomp
   b=last_name.capitalize
   last_name.capitalize!
   puts "My name is #{first_name} #{last_name}"

a) Amil Jones
b) What’s your first name? amil
What’s your last name? jones
My name is Amil Jones
c) My name is Amil Jones
d) None of the mentioned
View Answer

Answer: b
Explanation: First it takes the input from user and then modifies accordingly and displays output.

Output:
What's your first name? amil
What's your last name? jones
My name is Amil Jones

5. What is the output of the code?

advertisement
print "What's your first name?"
   first_name=gets.chomp
   a=first_name.capitalize
   first_name.capitalize!
   print "What's your last name?"
   last_name=gets.chomp
   b=last_name.capitalize
   last_name.capitalize
   puts "My name is #{first_name} #{last_name}"

a) Amil Jones
b) What’s your first name? amil
What’s your last name? jones
My name is Amil Jones
c) What’s your first name? amil
What’s your last name? jones
My name is Amil jones
d) None of the mentioned
View Answer

Answer: b
Explanation: Capitalize method was not called at the time of last name, jones was not printed correctly it was not modified because ! was not used.

advertisement
Output :
What's your first name? amil
What's your last name? jones
My name is Amil jones

6. What is the output of the given code?

print "what's your first name?"
   first_name=gets.chomp
   a=first_name.capitalize
   "first_name.capitalize!".reverse
   puts"My name is #{first_name}"

a) amil jones
b) What’s your first name? amil
My name is amil
c) My name is lima
d) My name is limA
View Answer

Answer: b
Explanation: “first_name.capitalize!” can not be in double quotes because it is a method.

Output:
What's your first name? amil
My name is amil

7. What is the output of the given code?

print "what's your first name?"
   first_name=gets.chomp
   a=first_name.capitalize
   "a".reverse
   puts"My name is #{a}"

a) amil jones
b) What’s your first name? amil
My name is Amil
c) My name is lima
d) My name is limA
View Answer

Answer: b
Explanation: The string is reversed but it is not stored in any variable.

Output:
What's your first name? amil
My name is amil

8. What is the output of the given code?

 print "What's your first name?"
   first_name=gets.chomp
   a=first_name.capitalize
   a=a.reverse
   puts"My name is #{a}"

a) amil jones
b) What’s your first name? amil
My name is Amil
c) What’s your first name? amil
My name is limA
d) My name is limA
View Answer

Answer: b
Explanation: The string is reversed then and stored in a variable.

Output:
what's your first name? amil
My name is limA

9. What do we mean by expression substitution?
a) Embedding the value of Ruby expression into a string using #{ }
b) Substituting string functions
c) Storing string value
d) None of the mentioned
View Answer

Answer: a
Explanation: Strings are displayed #{} sequence.

10. What is the output of the given code?

 x, y, z = 12, 36, 72
    puts "The value of x is #{ x }."
    puts "The sum of x and y is #{ x + y }."
    puts "The average was #{ (x + y + z)/3 }."

a) 12,48,40
b) Syntax error
c) The value of x is 12.
The sum of x and y is 48.
The average was 40.
d) None of the mentioned
View Answer

Answer: c
Explanation: x, y, z assigned the values 12, 36, 72 and then arithmetic operations performed on them.

Output:
   The value of x is 12.
   The sum of x and y is 48.
   The average was 40.

11. What does %{Learn ruby language} represents?
a) “Learn Ruby language”
b) “%{Learn Ruby language}”
c) ” Learn Ruby language ”
d) None of the mentioned
View Answer

Answer: a
Explanation: Anything written inside %{} is same as anything written inside “”.

12. What does %Q{ Learn ruby language } represents?
a) ” Learn Ruby language ”
b) “%{ Learn Ruby language }”
c) ” Learn Ruby language ”
d) None of the mentioned
View Answer

Answer: a
Explanation: Anything inside %Q{} is written inside “”.

13. What does %x!ls! represents?
a) Same as back tick command output `ls`
b) Same as ‘x’
c) Same as “xls”
d) None of the mentioned
View Answer

Answer: a
Explanation: It is same as back tick command output `ls`.

Sanfoundry Global Education & Learning Series – Ruby Programming.

To practice all areas of Ruby Programming for Interviews, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.