0
0
Rubyprogramming~20 mins

Comments and documentation in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ruby Comments Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of code with inline and block comments
What is the output of this Ruby code that uses both inline and block comments?
Ruby
puts 5 + 3 # adds five and three
=begin
This is a block comment
It should be ignored by Ruby
=end
puts 2 * 4
A8\n2 * 4
BSyntaxError
C5 + 3\n2 * 4
D8\n8
Attempts:
2 left
💡 Hint
Remember that comments do not affect the output and are ignored by Ruby.
Predict Output
intermediate
2:00remaining
Effect of comments on variable assignment
What will be the value of variable x after running this Ruby code?
Ruby
x = 10 # initial value
# x = 20
x = 30 # updated value
A30
Bnil
C20
D10
Attempts:
2 left
💡 Hint
Lines starting with # are ignored by Ruby.
🔧 Debug
advanced
2:00remaining
Identify the syntax error caused by incorrect comment usage
Which option shows code that will cause a syntax error due to incorrect comment usage in Ruby?
A# This is a valid comment\nputs 'World'
B=begin\nThis is a block comment without an end
Cputs 'Hello' # This is a comment
Dputs 'Test' # Another comment
Attempts:
2 left
💡 Hint
Block comments must have both =begin and =end.
🧠 Conceptual
advanced
1:30remaining
Purpose of documentation comments in Ruby
What is the main purpose of documentation comments in Ruby code?
ATo add extra functionality to the program
BTo make the code run faster
CTo explain the code to humans without affecting execution
DTo cause errors if the code is incorrect
Attempts:
2 left
💡 Hint
Think about why programmers write comments.
Predict Output
expert
2:30remaining
Output of code with nested comments and string literals
What is the output of this Ruby code snippet?
Ruby
puts "Hello # not a comment"
# puts "This line is commented out"
=begin
puts "This is inside a block comment"
=end
puts 'Goodbye'
AHello # not a comment\nGoodbye
BHello\nGoodbye
CHello # not a comment\nThis line is commented out\nGoodbye
DSyntaxError
Attempts:
2 left
💡 Hint
Comments do not affect strings inside quotes.