Bird
0
0

What is wrong with this Ruby code?

medium📝 Debug Q7 of 15
Ruby - File IO
What is wrong with this Ruby code?
Dir.chdir('nonexistent_folder')
AIt changes directory silently
BRaises an error because folder does not exist
CCreates the folder automatically
DReturns false without changing directory
Step-by-Step Solution
Solution:
  1. Step 1: Understand Dir.chdir behavior

    Dir.chdir changes directory but raises an error if the target folder does not exist. Since 'nonexistent_folder' does not exist, Dir.chdir raises an error.
  2. Final Answer:

    Raises an error because folder does not exist -> Option B
  3. Quick Check:

    Dir.chdir to missing folder = error [OK]
Quick Trick: Dir.chdir errors if directory missing [OK]
Common Mistakes:
  • Assuming it creates folder automatically
  • Expecting silent failure
  • Thinking it returns false instead of error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes