This set of Ruby Programming Multiple Choice Questions & Answers (MCQs) focuses on “String Methods”.
1. Which of the following is the valid string method?
a) The .length method
b) The .upcase method
c) The .downcase method
d) The .irreverse method
View Answer
Explanation: There is no predefined method which can reverse an already reversed string.
2. The .length method is used to check number of characters.
a) True
b) False
View Answer
Explanation: The .length method is used to check number of characters, spaces and even symbols.
3. What is the output of the following?
"Iam learning ruby language".length
a) 26
b) 23
c) 20
d) 18
View Answer
Explanation: It checks the number of blanks, number of characters and symbols in the given string.
Output: 26
4. What is the output of the following?
ruby.reverse
a) Error in compilation
b) ybur
c) 4
d) Undefined local variable ruby
View Answer
Explanation: String should always be in double quotes and then the .reverse method can be used.
Output: undefined local variable or method `ruby' for #<Context:0x7420ac>
5. What will be the output of the following?
"Eric".irreverse
a) Eric
b) cirE
c) undefined method
d) None of the mentioned
View Answer
Explanation: There is no predefined method named irreverse in ruby.
Output: undefined method `irreverse' for "eric":String
6. What does the .upcase method do?
a) Convert the string to uppercase
b) Convert only lowercase string to uppercase and vice-versa
c) Convert the string to lowercase
d) None o the mentioned
View Answer
Explanation: .upcase method is used to change the given string to uppercase.
7. What will be the output of the following?
"Ruby".reverse.upcase
a) RUBY
b) ybuR
c) YBUR
d) YBUr
View Answer
Explanation: First the given string is reversed and then .upcase method is used to capitalize it.
Output: "YBUR"
8. What will be the output of the given code?
"I'am learning Ruby Language".length.reverse.upcase
a) 27
b) egaugnal ybuR gninreal ma’I
c) ERAUGNAL YBUR GNINREAL MA’I
d) Undefined method `reverse’ for 27:Fixnum
View Answer
Explanation: The .length method finds the length which is a number and can’t be reversed.
Output: undefined method `reverse' for 27:Fixnum
9. What will be the output of the given code?
"I'am learning Ruby Language".reverse.upcase.length
a) 27
b) egaugnal ybuR gninreal ma’I
c) ERAUGNAL YBUR GNINREAL MA’I
d) Undefined method `reverse’ for 27:Fixnum
View Answer
Explanation: The .reverse will reverse it then .upcase will capitalize it and .length method give the length.
Output: 27
10. The downcase method changes the whole string to smallcase letters.
a) True
b) False
View Answer
Explanation: Changes everything to smallcase.
11. What will we the output of the given code?
"I'am Learning RUBY Language".downcase
a) iam learning ruby language
b) i’AM lEARNING ruby lANGUAGE
c) “i’am learning ruby language”
d) None of the mentioned
View Answer
Explanation: Everything is changed to smallcase because of downcase method is used.
Output: "i'am learning ruby language"
12. What is the output of given code?
string="I'am Learning RUBY Language".downcase string.upcase
a) Undefined method
b) “I’AM LEARNING RUBY LANGUAGE”
c) I’am Learning RUBY Language
d) None of the mentioned
View Answer
Explanation: First the given string is changed to uppercase and stored in variable named string and then the string is changed to uppercase.
Output: "I'AM LEARNING RUBY LANGUAGE"
13. What will be the output of the given code?
"Come let's learn.reverse Ruby.length language".upcase
a) nreal s’tel emoC 4 LANGUAGE
b) Undefined error
c) “COME LET’S LEARN.REVERSE RUBY.LENGTH LANGUAGE”
d) None of the above
View Answer
Explanation: Methods are never written inside double quotes.
Output: "COME LET'S LEARN.REVERSE RUBY.LENGTH LANGUAGE"
14. What will we the output of the given code?
I'am Learning RUBY Language.downcase
a) iam learning ruby language
b) i’AM lEARNING ruby lANGUAGE
c) “i’am learning ruby language”
d) unterminated string meets end of file
View Answer
Explanation: Strings should be in double quotes in Ruby language.
Output: Unterminated string meets end of file
15. Methods should not be written inside double quotes.
a) True
b) False
View Answer
Explanation: Always make sure to write methods outside double quotes.
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
- Check Information Technology Books
- Practice Programming MCQs
- Check Ruby Programming Books