Bird
Raised Fist0
Rubyprogramming~20 mins

Dynamic typing vs strong typing in Ruby - Practice Questions

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Ruby Typing Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Ruby code with dynamic typing?

Consider this Ruby code snippet. What will it print?

Ruby
x = 10
x = "hello"
puts x
Anil
B10
Chello
DTypeError
Attempts:
2 left
💡 Hint

In Ruby, variables can hold any type and can change type.

❓ Predict Output
intermediate
2:00remaining
What error does this Ruby code raise due to strong typing?

What error will this Ruby code produce?

Ruby
a = 5
b = "3"
puts a + b
A8
BTypeError
CArgumentError
DNoMethodError
Attempts:
2 left
💡 Hint

Ruby does not automatically convert between strings and numbers for addition.

🧠 Conceptual
advanced
2:00remaining
Which statement best describes Ruby's typing system?

Choose the statement that correctly describes Ruby's typing.

ARuby is dynamically typed and strongly typed.
BRuby is statically typed and weakly typed.
CRuby is statically typed and strongly typed.
DRuby is dynamically typed and weakly typed.
Attempts:
2 left
💡 Hint

Think about when types are checked and if implicit conversions happen.

❓ Predict Output
advanced
2:00remaining
What is the output of this Ruby code mixing types?

What will this Ruby code print?

Ruby
def add(x, y)
  x + y
end

puts add(2, 3)
puts add("2", "3")
puts add(2, "3")
A
5
23
TypeError
B
5
23
5
C
TypeError
TypeError
TypeError
D
5
TypeError
TypeError
Attempts:
2 left
💡 Hint

Consider how Ruby handles addition for integers and strings, and what happens when types differ.

🧠 Conceptual
expert
2:00remaining
Why does Ruby raise a TypeError when adding integer and string?

Choose the best explanation for why Ruby raises a TypeError when adding an integer and a string.

ABecause Ruby is dynamically typed and allows any operation between types.
BBecause Ruby automatically converts strings to integers but fails here due to syntax.
CBecause Ruby is statically typed and checks types at compile time.
DBecause Ruby is strongly typed and does not perform implicit type conversion between incompatible types.
Attempts:
2 left
💡 Hint

Think about how Ruby treats types during operations and if it converts types automatically.

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

  1. Step 1: Understand dynamic typing

    Dynamic typing means variables can hold any type and change types anytime during execution.
  2. Step 2: Understand strong typing

    Strong typing means the language prevents mixing incompatible types without explicit conversion.
  3. Final Answer:

    Ruby is dynamically typed and strongly typed. -> Option A
  4. 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

  1. Step 1: Identify Ruby syntax

    Ruby allows assigning different types to the same variable without error.
  2. Step 2: Check each option

    x = 10; x = "hello" uses Ruby syntax and changes x from integer to string, showing dynamic typing.
  3. Final Answer:

    x = 10; x = "hello" -> Option A
  4. 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

  1. Step 1: Analyze the method call

    The method add tries to add an integer (5) and a string ("10") using + operator.
  2. Step 2: Understand Ruby's strong typing

    Ruby does not automatically convert types for +; adding integer and string causes a TypeError.
  3. Final Answer:

    TypeError -> Option B
  4. 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

  1. Step 1: Check method parameters and call

    The method multiply is called with an integer (3) and a string ("4").
  2. Step 2: Understand Ruby's type rules for *

    Multiplying integer by string without conversion causes a TypeError in Ruby.
  3. Final Answer:

    TypeError due to multiplying integer and string -> Option D
  4. 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

  1. Step 1: Understand dynamic typing flexibility

    Ruby variables can hold any type, so inputs can be string or integer.
  2. Step 2: Use strong typing safety

    To avoid TypeError, convert strings to integers explicitly before adding.
  3. 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.
  4. Final Answer:

    Convert both to integers before adding using to_i method. -> Option C
  5. Quick Check:

    Dynamic + Strong typing = explicit conversion [OK]
Hint: Convert strings to numbers before math to avoid errors [OK]
Common Mistakes:
  • Ignoring type conversion before arithmetic
  • Assuming Ruby auto-converts types
  • Using static typing syntax in Ruby
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard