Bird
Raised Fist0
Rubyprogramming~10 mins

Ternary operator usage in Ruby - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to assign 'even' if number is divisible by 2, otherwise 'odd'.

Ruby
result = number % 2 == 0 ? [1] : 'odd'
Drag options to blanks, or click blank then click option'
A'even'
B'odd'
C'zero'
D'none'
Attempts:
3 left
💡 Hint
Common Mistakes
Putting 'odd' before the colon instead of 'even'.
Forgetting quotes around the string.
2fill in blank
medium

Complete the code to assign 'adult' if age is 18 or more, otherwise 'minor'.

Ruby
status = age >= 18 ? [1] : 'minor'
Drag options to blanks, or click blank then click option'
A'child'
B'adult'
C'teen'
D'senior'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'child' or 'teen' instead of 'adult'.
Confusing the true and false parts.
3fill in blank
hard

Fix the error in the ternary operator to assign 'positive' if num > 0, else 'non-positive'.

Ruby
label = num > 0 ? [1] : 'non-positive'
Drag options to blanks, or click blank then click option'
Atrue
Bpositive
Cnum
D'positive'
Attempts:
3 left
💡 Hint
Common Mistakes
Missing quotes around 'positive'.
Using a variable name instead of a string.
4fill in blank
hard

Fill both blanks to assign 'even' if n is divisible by 2, else 'odd'.

Ruby
result = n % 2 [1] 0 ? 'even' : [2]
Drag options to blanks, or click blank then click option'
A==
B!=
C'odd'
D'even'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '=='.
Swapping 'even' and 'odd' in the false part.
5fill in blank
hard

Fill all three blanks to assign 'positive', 'negative', or 'zero' based on the value of val.

Ruby
label = val > 0 ? [1] : val < 0 ? [2] : [3]
Drag options to blanks, or click blank then click option'
A'positive'
B'negative'
C'zero'
D'none'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up the order of conditions.
Forgetting quotes around strings.

Practice

(1/5)
1. What does the ternary operator condition ? value_if_true : value_if_false do in Ruby?
easy
A. It chooses one of two values based on the condition's truth.
B. It creates a loop that runs twice.
C. It defines a new method with two parameters.
D. It comments out the code after the colon.

Solution

  1. Step 1: Understand the ternary operator format

    The ternary operator uses a condition followed by a question mark, then two values separated by a colon.
  2. Step 2: Identify its purpose

    It returns the first value if the condition is true, otherwise the second value.
  3. Final Answer:

    It chooses one of two values based on the condition's truth. -> Option A
  4. Quick Check:

    condition ? true_value : false_value = chooses value [OK]
Hint: Think: condition true? pick first value, else second [OK]
Common Mistakes:
  • Confusing ternary with loops
  • Thinking it defines methods
  • Misunderstanding the colon as comment
2. Which of the following is the correct syntax for a ternary operator in Ruby?
easy
A. condition ? value_if_true : value_if_false
B. condition : value_if_true ? value_if_false
C. condition ? value_if_true ; value_if_false
D. condition ? value_if_true, value_if_false

Solution

  1. Step 1: Recall ternary operator syntax

    The correct syntax places a question mark after the condition, then the true value, a colon, then the false value.
  2. Step 2: Compare options

    Only condition ? value_if_true : value_if_false matches the correct format: condition ? true_value : false_value.
  3. Final Answer:

    condition ? value_if_true : value_if_false -> Option A
  4. Quick Check:

    Syntax = condition ? true : false [OK]
Hint: Remember: question mark before true, colon before false [OK]
Common Mistakes:
  • Swapping question mark and colon
  • Using semicolon instead of colon
  • Separating values with comma
3. What is the output of this Ruby code?
age = 20
puts age >= 18 ? "Adult" : "Minor"
medium
A. Minor
B. 20
C. SyntaxError
D. Adult

Solution

  1. Step 1: Evaluate the condition

    The condition age >= 18 is true because age is 20.
  2. Step 2: Apply ternary operator

    Since the condition is true, the expression returns "Adult".
  3. Final Answer:

    Adult -> Option D
  4. Quick Check:

    20 >= 18 ? "Adult" : "Minor" = "Adult" [OK]
Hint: Check condition true? pick first value, else second [OK]
Common Mistakes:
  • Choosing the false value by mistake
  • Printing the variable instead of result
  • Expecting syntax error
4. Identify the error in this Ruby code using a ternary operator:
score = 85
result = score > 60 ? "Pass" "Fail"
puts result
medium
A. Missing question mark after condition
B. Missing colon between true and false values
C. Ternary operator cannot be used with strings
D. Wrong variable name 'score'

Solution

  1. Step 1: Check ternary operator syntax

    The ternary operator requires a colon between the true and false values.
  2. Step 2: Identify missing colon

    The code has "Pass" "Fail" without a colon, causing a syntax error.
  3. Final Answer:

    Missing colon between true and false values -> Option B
  4. Quick Check:

    Syntax = condition ? true_value : false_value [OK]
Hint: Always put colon between true and false parts [OK]
Common Mistakes:
  • Omitting colon
  • Using comma instead of colon
  • Forgetting question mark
5. Given this Ruby code, what will be the output?
values = [0, 1, nil, 5]
results = values.map { |v| v ? "Yes" : "No" }
puts results.inspect
hard
A. ["No", "Yes", "No", "Yes"]
B. ["No", "No", "No", "No"]
C. ["Yes", "Yes", "No", "Yes"]
D. ["Yes", "No", "Yes", "No"]

Solution

  1. Step 1: Understand truthiness in Ruby

    In Ruby, 0 and 1 are truthy, nil is falsy.
  2. Step 2: Apply ternary operator to each element

    For each value: 0 (truthy) -> "Yes", 1 (truthy) -> "Yes", nil (falsy) -> "No", 5 (truthy) -> "Yes".
  3. Step 3: Check mapped results

    The results array is ["Yes", "Yes", "No", "Yes"].
  4. Final Answer:

    ["Yes", "Yes", "No", "Yes"] -> Option C
  5. Quick Check:

    Truthy values = "Yes", nil = "No" [OK]
Hint: Remember: only nil and false are falsey in Ruby [OK]
Common Mistakes:
  • Thinking 0 is falsy
  • Confusing nil with false
  • Misreading map block
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