Bird
0
0

Given the class below, how can you use an infix function to check if a number is within a range?

hard📝 Application Q8 of 15
Kotlin - Functions
Given the class below, how can you use an infix function to check if a number is within a range?
class Range(val start: Int, val end: Int) {
    infix fun contains(number: Int) = number in start..end
}

fun main() {
    val range = Range(1, 10)
    // Fill in the blank to check if 5 is in range
    val result = range ____ 5
    println(result)
}
Awithin
Bcontains
Chas
Din
Step-by-Step Solution
Solution:
  1. Step 1: Identify the infix function name

    The class defines an infix function named contains to check if a number is in the range.
  2. Step 2: Use the infix function in the call

    To call it infix style, write range contains 5.
  3. Final Answer:

    contains -> Option B
  4. Quick Check:

    Infix call uses function name = contains [OK]
Quick Trick: Use the exact infix function name between objects [OK]
Common Mistakes:
MISTAKES
  • Using keywords like 'in' instead of function name
  • Guessing function names not declared
  • Forgetting infix call syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes