Challenge - 5 Problems
Ruby Comments Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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
Attempts:
2 left
💡 Hint
Remember that comments do not affect the output and are ignored by Ruby.
✗ Incorrect
The inline comment after 'puts 5 + 3' is ignored, so 8 is printed. The block comment between =begin and =end is also ignored. Then 'puts 2 * 4' prints 8.
❓ Predict Output
intermediate2: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
Attempts:
2 left
💡 Hint
Lines starting with # are ignored by Ruby.
✗ Incorrect
The line 'x = 20' is commented out, so it does not run. The last assignment 'x = 30' sets x to 30.
🔧 Debug
advanced2: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?
Attempts:
2 left
💡 Hint
Block comments must have both =begin and =end.
✗ Incorrect
Option B starts a block comment with =begin but does not close it with =end, causing a syntax error.
🧠 Conceptual
advanced1:30remaining
Purpose of documentation comments in Ruby
What is the main purpose of documentation comments in Ruby code?
Attempts:
2 left
💡 Hint
Think about why programmers write comments.
✗ Incorrect
Documentation comments help humans understand the code. They do not change how the program runs.
❓ Predict Output
expert2: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'
Attempts:
2 left
💡 Hint
Comments do not affect strings inside quotes.
✗ Incorrect
The string "Hello # not a comment" prints exactly as is because # inside quotes is not a comment. The commented out puts line and block comment are ignored. Then 'Goodbye' prints.