Bird
0
0

You want to store a constant greeting message that should never change in your Ruby program. Which approach is best?

hard📝 Application Q15 of 15
Ruby - String Operations
You want to store a constant greeting message that should never change in your Ruby program. Which approach is best?
GREETING = "Welcome!".freeze

Why is freezing important here?
AIt prevents accidental changes to the greeting, ensuring data integrity.
BIt makes the greeting print faster.
CIt allows the greeting to be changed later if needed.
DIt compresses the string to save memory.
Step-by-Step Solution
Solution:
  1. Step 1: Understand constant usage

    Constants like GREETING should not change during program execution to avoid bugs.
  2. Step 2: Role of freeze on constants

    Freezing the string ensures it cannot be modified accidentally, protecting the constant's value.
  3. Final Answer:

    It prevents accidental changes to the greeting, ensuring data integrity. -> Option A
  4. Quick Check:

    Freeze constant for safety = D [OK]
Quick Trick: Freeze constants to keep them safe from changes [OK]
Common Mistakes:
  • Thinking freeze speeds up printing
  • Believing freeze allows changes later
  • Assuming freeze compresses data

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes