This set of Ruby Programming Multiple Choice Questions & Answers (MCQs) focuses on “Formatting String Methods”.
1. The .upcase and .capitalize method are used for capitalizing the whole string.
a) True
b) False
View Answer
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
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
Explanation: It is used for modifying the variable.
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
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?
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
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.
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
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
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
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
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
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
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
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
Explanation: It is same as back tick command output `ls`.
Sanfoundry Global Education & Learning Series – Ruby Programming.
To practice all areas of Ruby Programming, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Apply for Programming Internship
- Practice Programming MCQs
- Check Ruby Programming Books
- Check Information Technology Books