0
0
Rubyprogramming~20 mins

Dynamic typing vs strong typing in Ruby - Practice Questions

Choose your learning style9 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.