Bird
0
0

What will be the output of the following Ruby code?

medium📝 Predict Output Q4 of 15
Ruby - File IO
What will be the output of the following Ruby code?
Dir.mkdir('new_folder') unless Dir.exist?('new_folder')
puts Dir.exist?('new_folder')
Anil
Bfalse
Ctrue
DError
Step-by-Step Solution
Solution:
  1. Step 1: Check if directory exists

    The code checks if 'new_folder' exists using Dir.exist?. If not, it creates it. After creation, Dir.exist?('new_folder') returns true, so puts prints true.
  2. Final Answer:

    true -> Option C
  3. Quick Check:

    Dir.exist? after mkdir = true [OK]
Quick Trick: Dir.exist? returns true after Dir.mkdir creates folder [OK]
Common Mistakes:
  • Assuming output is false before creation
  • Expecting nil instead of boolean
  • Thinking code raises error if folder exists

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes