Ruby Variable and Data Type Quiz
Master Ruby's variable system and data types including local/global/instance/class variables, numbers, booleans, symbols, strings, and naming conventions with this comprehensive ruby quiz covering fundamental data handling concepts.
Question 1
What is the scope of a local variable in Ruby?
def example_method
local_var = 'Hello'
puts local_var
end
example_method
puts local_varQuestion 2
How do you define a global variable in Ruby?
$global_var = 'I am global'
def test_method
puts $global_var
end
test_methodQuestion 3
What is the difference between instance variables and local variables?
class Person
def initialize(name)
@name = name # instance variable
local_var = 'temporary' # local variable
end
def display
puts @name
puts local_var # This will cause an error
end
endQuestion 4
How are class variables defined and used in Ruby?
class Vehicle
@@vehicle_count = 0 # class variable
def initialize
@@vehicle_count += 1
end
def self.total_vehicles
@@vehicle_count
end
endQuestion 5
What are Ruby symbols and how do they differ from strings?
name_symbol = :name
name_string = 'name'
puts name_symbol.object_id # Same object each time
puts :name.object_id # Same object
puts 'name'.object_id # Different object each time
puts 'name'.object_id # Different objectQuestion 6
What is the primary difference between Integer and Float in Ruby?
integer_num = 42 # Integer
float_num = 42.0 # Float
puts integer_num.class # Integer
puts float_num.class # FloatQuestion 7
How do boolean values work in Ruby conditionals?
age = 25
is_adult = age >= 18
if is_adult
puts 'You are an adult'
else
puts 'You are not an adult'
endQuestion 8
What are Ruby constants and how are they named?
PI = 3.14159
MAX_USERS = 100
class Circle
RADIUS = 5
end
puts PI
puts Circle::RADIUSQuestion 9
How do you check if a variable is defined in Ruby?
def check_variable
if defined? local_var
puts 'local_var is defined'
else
puts 'local_var is not defined'
end
end
check_variableQuestion 10
What is variable interpolation in Ruby strings?
name = 'Alice'
age = 30
message = "Hello, my name is #{name} and I am #{age} years old."
puts messageQuestion 11
How do instance variables behave in Ruby inheritance?
class Animal
def initialize(name)
@name = name
end
end
class Dog < Animal
def initialize(name, breed)
super(name)
@breed = breed
end
def info
puts "#{@name} is a #{@breed}"
end
endQuestion 12
What is the purpose of the nil value in Ruby?
result = nil
if result
puts 'Result exists'
else
puts 'Result is nil or false'
endQuestion 13
How do class variables differ from class instance variables?
class Example
@@class_var = 'shared' # class variable
@instance_var = 'per class' # class instance variable
def self.show_vars
puts @@class_var
puts @instance_var
end
endQuestion 14
What are Ruby naming conventions for different identifiers?
Question 15
How do symbols improve memory efficiency in Ruby?
symbols = [:name, :age, :email] * 1000 # Same objects reused
strings = ['name', 'age', 'email'] * 1000 # New objects each time
puts symbols.uniq.size # 3 unique symbols
puts strings.uniq.size # Still 3000 stringsQuestion 16
What happens when you try to access an undefined local variable?
puts undefined_variableQuestion 17
How do boolean operators work with different data types in Ruby?
result1 = true && 'hello' # returns 'hello'
result2 = false && 'hello' # returns false
result3 = 'hello' || 'world' # returns 'hello'
result4 = nil || 'default' # returns 'default'Question 18
What is variable shadowing in Ruby?
message = 'outer'
def display_message
message = 'inner' # This shadows the outer variable
puts message
end
display_message
puts messageQuestion 19
How do constants behave in Ruby inheritance?
class Parent
CONSTANT = 'parent'
end
class Child < Parent
CONSTANT = 'child' # Creates new constant
end
puts Parent::CONSTANT
puts Child::CONSTANTQuestion 20
What is the difference between == and === operators in Ruby?
puts 1 == 1.0 # true (value equality)
puts 1 === 1.0 # true (value equality for numbers)
puts String === 'hello' # true (class membership)
puts (1..10) === 5 # true (range membership)Question 21
How do global variables affect code maintainability?
$global_counter = 0
def increment
$global_counter += 1
end
def reset
$global_counter = 0
endQuestion 22
What is duck typing and how does it relate to Ruby's type system?
def process_data(data)
puts data.upcase # Works if data responds to upcase
end
process_data('string') # Works
process_data(:symbol) # Error - Symbol doesn't have upcaseQuestion 23
How do instance variables initialize in Ruby objects?
class Person
def initialize(name)
@name = name
end
def name
@name # nil if not initialized
end
end
person = Person.new('Alice')
puts person.nameQuestion 24
What are the implications of Ruby's dynamic typing for variable usage?
variable = 'string'
variable = 42
variable = [1, 2, 3]
puts variable.class # Changes based on assignmentQuestion 25
How do symbols contribute to Ruby's performance?
Question 26
What is the role of constants in Ruby module system?
module MathConstants
PI = 3.14159
E = 2.71828
end
puts MathConstants::PIQuestion 27
How does Ruby handle numeric precision differences?
integer = 1
float = 1.0
puts integer == float # true
puts integer.eql?(float) # false
puts integer.equal?(float) # falseQuestion 28
What are the best practices for variable naming in Ruby?
Question 29
How do class variables behave in Ruby inheritance hierarchy?
class Parent
@@count = 0
end
class Child < Parent
@@count = 1 # Affects all classes in hierarchy
end
puts Parent.class_variable_get(:@@count) # 1
puts Child.class_variable_get(:@@count) # 1Question 30
What is the significance of Ruby's object model for data types?
Question 31
How do symbols enhance hash performance in Ruby?
hash_with_symbols = { name: 'Alice', age: 30 }
hash_with_strings = { 'name' => 'Alice', 'age' => 30 }
# Symbol keys are faster to compare and use less memoryQuestion 32
What are the implications of nil in Ruby's type system?
result = find_user('nonexistent')
if result
puts 'User found'
else
puts 'User not found' # This executes
endQuestion 33
How do naming conventions improve Ruby code maintainability?
Question 34
What is the relationship between Ruby's dynamic nature and variable safety?
def process(data)
# No type checking - accepts any object
data.upcase # May fail if data doesn't respond to upcase
endQuestion 35
How do constants support Ruby's module composition?
module DatabaseConfig
HOST = 'localhost'
PORT = 5432
end
class UserRepository
include DatabaseConfig
def connect
# Can access HOST and PORT
end
endQuestion 36
What are the performance considerations for boolean operations in Ruby?
result = expensive_operation? && another_expensive_operation?
# Short-circuits if first operation returns falseQuestion 37
How do instance variables contribute to object encapsulation in Ruby?
class BankAccount
def initialize(balance)
@balance = balance # Encapsulated state
end
def deposit(amount)
@balance += amount
end
def balance
@balance # Controlled access
end
endQuestion 38
What is the impact of Ruby's truthiness system on conditional logic?
def process_list(items)
if items # Works for both nil and empty arrays
items.each { |item| puts item }
end
endQuestion 39
How do symbols support Ruby's metaprogramming capabilities?
class DynamicClass
[:name, :age, :email].each do |attr|
define_method(attr) { instance_variable_get("@#{attr}") }
define_method("#{attr}=") { |value| instance_variable_set("@#{attr}", value) }
end
endQuestion 40
Considering Ruby's type system and variable scoping rules as a comprehensive foundation for application development, what fundamental principle guides the effective use of Ruby's data types and variable management in building maintainable, scalable applications where multiple developers collaborate on complex systems with evolving requirements and changing data structures?
