Dynamic typing means the type of a variable is checked at run-time, not before. You can change what kind of value a variable holds while the program runs.
Click to reveal answer
beginner
What does strong typing mean?
Strong typing means the language prevents mixing different types in ways that don't make sense, catching errors when you try to do that.
Click to reveal answer
intermediate
Is Ruby dynamically typed, strongly typed, or both?
Ruby is both dynamically typed and strongly typed. You don't declare variable types, but Ruby won't let you mix incompatible types without errors.
Click to reveal answer
beginner
Example: What happens if you add a number and a string in Ruby?
Ruby raises an error because it is strongly typed and does not allow adding a number and a string directly.
Click to reveal answer
beginner
Why is dynamic typing helpful for beginners?
Dynamic typing lets you write code faster without worrying about types upfront. It feels like talking to a friend who understands what you mean.
Click to reveal answer
What does dynamic typing allow you to do?
APrevent mixing types
BDeclare variable types before running
CCompile code faster
DChange variable types while the program runs
✗ Incorrect
Dynamic typing means variable types are checked at run-time, so you can change what a variable holds while running.
What is a key feature of strong typing?
APrevents invalid operations between types
BAllows any operation between types
CRequires variable declarations
DRuns faster
✗ Incorrect
Strong typing stops you from mixing incompatible types in ways that cause errors.
Which statement is true about Ruby's typing?
ARuby is dynamically and strongly typed
BRuby is weakly typed
CRuby is statically typed
DRuby requires type declarations
✗ Incorrect
Ruby checks types at run-time (dynamic) and prevents invalid type mixing (strong).
What happens if you try to add a number and a string in Ruby?
AIt converts both to strings and adds
BIt raises an error
CIt ignores the string
DIt adds the number to the string length
✗ Incorrect
Ruby raises an error because it does not allow adding a number and a string directly.
Why might dynamic typing be easier for beginners?
ABecause it forces strict rules
BBecause it requires declaring types
CBecause it lets you focus on logic without types first
DBecause it runs faster
✗ Incorrect
Dynamic typing lets beginners write code without worrying about types upfront.
Explain the difference between dynamic typing and strong typing in your own words.
Think about when types are checked and how strict the language is about mixing types.
You got /3 concepts.
Describe how Ruby's typing system affects writing and running code.
Consider both the freedom and the rules Ruby gives you.
You got /3 concepts.
Practice
(1/5)
1. Which statement best describes Ruby's typing system?
easy
A. Ruby is dynamically typed and strongly typed.
B. Ruby is statically typed and weakly typed.
C. Ruby is statically typed and strongly typed.
D. Ruby is dynamically typed and weakly typed.
Solution
Step 1: Understand dynamic typing
Dynamic typing means variables can hold any type and change types anytime during execution.
Step 2: Understand strong typing
Strong typing means the language prevents mixing incompatible types without explicit conversion.
Final Answer:
Ruby is dynamically typed and strongly typed. -> Option A
Quick Check:
Dynamic + Strong = C [OK]
Hint: Remember Ruby allows type changes but checks type safety [OK]
Common Mistakes:
Confusing dynamic with static typing
Thinking Ruby allows unsafe type mixing
Mixing weak and strong typing concepts
2. Which Ruby code snippet shows dynamic typing in action?
easy
A. x = 10; x = "hello"
B. int x = 10; x = "hello"
C. var x: Integer = 10; x = "hello"
D. let x = 10; x = true
Solution
Step 1: Identify Ruby syntax
Ruby allows assigning different types to the same variable without error.
Step 2: Check each option
x = 10; x = "hello" uses Ruby syntax and changes x from integer to string, showing dynamic typing.
Final Answer:
x = 10; x = "hello" -> Option A
Quick Check:
Variable changes type dynamically = A [OK]
Hint: Look for variable reassignment with different types in Ruby syntax [OK]
Common Mistakes:
Choosing code with static typing syntax
Confusing variable declaration styles
Ignoring language-specific syntax rules
3. What will be the output of this Ruby code?
def add(a, b)
a + b
end
puts add(5, "10")
medium
A. 15
B. TypeError
C. 510
D. nil
Solution
Step 1: Analyze the method call
The method add tries to add an integer (5) and a string ("10") using + operator.
Step 2: Understand Ruby's strong typing
Ruby does not automatically convert types for +; adding integer and string causes a TypeError.
Final Answer:
TypeError -> Option B
Quick Check:
Strong typing prevents int + string = B [OK]
Hint: Adding different types without conversion causes TypeError [OK]
Common Mistakes:
Assuming automatic string conversion
Expecting concatenation of numbers and strings
Ignoring Ruby's type safety rules
4. Identify the error in this Ruby code:
def multiply(x, y)
x * y
end
result = multiply(3, "4")
puts result
medium
A. NameError for undefined variable
B. SyntaxError due to method definition
C. No error, outputs 12
D. TypeError due to multiplying integer and string
Solution
Step 1: Check method parameters and call
The method multiply is called with an integer (3) and a string ("4").
Step 2: Understand Ruby's type rules for *
Multiplying integer by string without conversion causes a TypeError in Ruby.
Final Answer:
TypeError due to multiplying integer and string -> Option D
Quick Check:
Strong typing blocks int * string = A [OK]
Hint: Check operand types for * operator to avoid TypeError [OK]
Common Mistakes:
Thinking Ruby auto-converts string to integer
Assuming no error on mixed types
Confusing SyntaxError with TypeError
5. You want to write a Ruby method that adds two values but safely handles if one is a string representing a number. Which approach best uses Ruby's dynamic and strong typing?
hard
A. Convert both to strings and concatenate.
B. Add values directly without conversion.
C. Convert both to integers before adding using to_i method.
D. Use static type declarations for parameters.
Solution
Step 1: Understand dynamic typing flexibility
Ruby variables can hold any type, so inputs can be string or integer.
Step 2: Use strong typing safety
To avoid TypeError, convert strings to integers explicitly before adding.
Step 3: Evaluate options
Converting both to integers using to_i before adding is safe. Adding directly risks TypeError. Static type declarations are invalid in Ruby. Converting to strings performs concatenation instead of addition.
Final Answer:
Convert both to integers before adding using to_i method. -> Option C