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

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.

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.

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

7. The following syntax is used for multiline comment.

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

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

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.

advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn