Ruby Programming Questions and Answers – Comments in Ruby

This set of Ruby Programming Multiple Choice Questions & Answers (MCQs) focuses on “Comments in Ruby”.

1. Why do we use comments?
a) For later use to understand the working of the program
b) It is an efficient way of programming
c) It makes easy for the third person to understand
d) All of the mentioned
View Answer

Answer: d
Explanation: It is the efficient way of programming and easy to understand.

2. Which of the following type of comments are valid in ruby?
a) Single line comments
b) Multi line comments
c) Double line comments
d) All of the mentioned
View Answer

Answer: d
Explanation: Double line comments comes under multiline comment.

3. How to comment multiple lines in ruby?
a) Using multiline comments
b) Using single line comments
c) No option to comment
d) All of the mentioned
View Answer

Answer: a
Explanation: Multiple lines can be commented using multiline comments.
advertisement
advertisement

4. How to comment a single line?
a) Using #
b) Using begin and end
c) Using //
d) None of the mentioned
View Answer

Answer: a
Explanation: Use # to comment single line.

5. This the right way to comment a single line.
“Ruby”.length # I’m a single line comment!
a) True
b) False
View Answer

Answer: a
Explanation: We use # in a single line comment.
Note: Join free Sanfoundry classes at Telegram or Youtube

6. What is the output of the given code?
“Ruby”.length #to find the length of given string
a) 4 to find the length of given string
b) 4
c) To find the length of given string
d) Ruby
View Answer

Answer: b
Explanation: It finds the length of string.

7. The following syntax is used for multiline comment.

advertisement
=begin
    iam in a multi line comment
   =end

a) True
b) False
View Answer

Answer: a
Explanation: We use =begin and =end to mark the beginning and end of the comment.
advertisement

8. The following is the correct way to use multiline comment.

 = begin
   # comment
   = end

a) True
b) False
View Answer

Answer: b
Explanation: No space should be given between = and begin and = or end.

9. Why do we use =begin and =end?
a) To mark the start and end of multiline comment
b) To comment multiple lines
c) To avoid the use of # again and again
d) All of the mentioned
View Answer

Answer: d
Explanation: Easier way to comment multiple lines.

10. What is the output of the given code?

"Ruby Language".length
       =begin
       calculate length
       =end

a) 13
b) 13 calculate length
c) 12
d) None of the mentioned
View Answer

Answer: a
Explanation: Calculate the length and is commented using =begin and =end.

Output:
13

11. What is the output of the given code?

"Ruby Language".length
       = begin
       calculate length
       = end

a) 13
b) 12
c) Error
d) None of the mentioned
View Answer

Answer: c
Explanation: No space between = and begin and end

Output:
Syntax error, unexpected '=', expecting $end
= begin

12. Space between 2+5 or 2 + 5 is valid but =begin and = begin is not valid.
a) True
b) False
View Answer

Answer: a
Explanation: Ruby recognizes =begin and = begin in different way.

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.