Bird
0
0

Given this Ruby code, what will puts Vehicle::TYPE print?

hard📝 Application Q15 of 15
Ruby - Class Methods and Variables
Given this Ruby code, what will puts Vehicle::TYPE print?
class Vehicle
  TYPE = "Transport"
  class Car
    TYPE = "Car"
  end
end

puts Vehicle::TYPE
A"Transport"
B"Car"
CError: uninitialized constant
Dnil
Step-by-Step Solution
Solution:
  1. Step 1: Understand constant lookup in nested classes

    Constants are scoped to the class they are defined in. Vehicle::TYPE refers to the constant TYPE inside Vehicle, not inside Vehicle::Car.
  2. Step 2: Check the code output

    Vehicle::TYPE is "Transport". The inner class Car has its own TYPE, but it is not accessed here.
  3. Final Answer:

    "Transport" -> Option A
  4. Quick Check:

    Outer class constant accessed by Vehicle::TYPE = "Transport" [OK]
Quick Trick: Outer class constant accessed by ClassName::CONSTANT [OK]
Common Mistakes:
  • Confusing inner class constant with outer class constant
  • Expecting error for nested constants
  • Using wrong scope operator

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes