Ruby Questions and Answers Part-5

1. 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

Answer: a
Explanation: .upcase method is used to change the given string to uppercase.

2. What will be the output of the following?
"Ruby".reverse.upcase
a) RUBY
b) ybuR
c) YBUR
d) YBUr

Answer: c
Explanation: First the given string is reversed and then .upcase method is used to capitalize it.
Output: "YBUR"

3. 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

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

4. 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

Answer: a
Explanation: The .reverse will reverse it then .upcase will capitalize it and .length method give the length.

5. The downcase method changes the whole string to smallcase letters.
a) True
b) False

Answer: a
Explanation: Changes everything to smallcase.

6. 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

Answer: c
Explanation: Everything is changed to smallcase because of downcase method is used.
Output: "i'am learning ruby language"

7. Methods should not be written inside double quotes.
a) True
b) False

Answer: a
Explanation: Always make sure to write methods outside double quotes.

8. 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

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"

9. 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

Answer: c
Explanation: Methods are never written inside double quotes.
Output: "COME LET'S LEARN.REVERSE RUBY.LENGTH LANGUAGE"

10. 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

Answer: d
Explanation: Strings should be in double quotes in Ruby language.
Output: Unterminated string meets end of file