Ruby Programming Questions and Answers – String Methods

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

Answer: d
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

Answer: b
Explanation: The .length method is used to check number of characters, spaces and even symbols.

3. What is the output of the following?

advertisement
advertisement
 "Iam learning ruby language".length

a) 26
b) 23
c) 20
d) 18
View Answer

Answer: a
Explanation: It checks the number of blanks, number of characters and symbols in the given string.

Note: Join free Sanfoundry classes at Telegram or Youtube
Output:
26

4. What is the output of the following?

advertisement
 ruby.reverse

a) Error in compilation
b) ybur
c) 4
d) Undefined local variable ruby
View Answer

Answer: d
Explanation: String should always be in double quotes and then the .reverse method can be used.

advertisement
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

Answer: c
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

Answer: a
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

Answer: c
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

Answer: d
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

Answer: a
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

Answer: a
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

Answer: c
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

Answer: b
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

Answer: c
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

Answer: d
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

Answer: a
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.

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.