📖 Scenario: You are helping a store manager compare product prices to decide which items are cheaper or more expensive.
🎯 Goal: Build a Kotlin program that compares two product prices using comparison operators and shows the results.
📋 What You'll Learn
Create two variables with exact prices
Create a variable to hold a threshold price
Use comparison operators to compare prices with the threshold
Print the comparison results clearly
💡 Why This Matters
🌍 Real World
Comparing prices helps shoppers and store managers decide which products are cheaper or more expensive.
💼 Career
Understanding comparison operators is essential for decision-making logic in software development.
Progress0 / 4 steps
1
Create product price variables
Create two variables called priceA and priceB with values 150 and 200 respectively.
Kotlin
Hint
Use val to create variables and assign the exact numbers.
2
Add a threshold price variable
Create a variable called threshold and set it to 180.
Kotlin
Hint
Use val threshold = 180 to create the variable.
3
Compare prices with threshold
Create two Boolean variables called isPriceALess and isPriceBMore. Set isPriceALess to the result of priceA < threshold and isPriceBMore to the result of priceB > threshold.
Kotlin
Hint
Use < and > operators to compare values.
4
Print the comparison results
Print the values of isPriceALess and isPriceBMore using two separate println statements.
Kotlin
Hint
Use println(isPriceALess) and println(isPriceBMore) to show the results.
Practice
(1/5)
1. Which of the following Kotlin comparison operators checks if two values are equal?
easy
A. <=
B. !=
C. >
D. ==
Solution
Step 1: Understand the meaning of comparison operators
In Kotlin, the operator == is used to check if two values are equal.
Step 2: Identify the correct operator for equality
The operator == returns true if both values are equal, otherwise false.
Final Answer:
== -> Option D
Quick Check:
Equality operator = == [OK]
Hint: Remember: '==' means equal, '!=' means not equal [OK]
Common Mistakes:
Confusing '==' with '=' which is assignment
Using '!=' to check equality
Mixing up '>' and '<=' operators
2. Which of the following is the correct Kotlin syntax to check if variable a is not equal to variable b?
easy
A. a != b
B. a <> b
C. a =! b
D. a !== b
Solution
Step 1: Recall Kotlin syntax for inequality
Kotlin uses != to check if two values are not equal.
Step 2: Verify each option
a != b is correct; a <> b is invalid in Kotlin; a =! b is incorrect syntax; a !== b checks referential inequality, not value inequality.
Final Answer:
a != b -> Option A
Quick Check:
Not equal operator = != [OK]
Hint: Use '!=' for not equal, never '<>' in Kotlin [OK]
Common Mistakes:
Using '<>' which is not valid in Kotlin
Swapping '=' and '!' in 'a =! b'
Confusing '!=' with '!==', which checks references
3. What will be the output of the following Kotlin code?
val x = 10
val y = 20
println(x > y)
medium
A. false
B. true
C. 10
D. Compilation error
Solution
Step 1: Evaluate the comparison expression
The expression x > y compares if 10 is greater than 20, which is false.
Step 2: Understand the println output
Printing the boolean result of the comparison will output false.
Final Answer:
false -> Option A
Quick Check:
10 > 20 = false [OK]
Hint: Compare values carefully; 10 is not greater than 20 [OK]
Common Mistakes:
Assuming 10 > 20 is true
Confusing output with variable values
Expecting a number instead of boolean output
4. Identify the error in this Kotlin code snippet:
val a = 5
val b = 10
if (a = b) {
println("Equal")
} else {
println("Not Equal")
}
medium
A. Variables a and b must be mutable
B. Missing semicolon after println
C. Using '=' instead of '==' in if condition
D. Incorrect println syntax
Solution
Step 1: Check the if condition syntax
The condition uses a = b, which is assignment, not comparison.
Step 2: Correct the operator for comparison
Use == to compare values, so it should be a == b.
Final Answer:
Using '=' instead of '==' in if condition -> Option C
Quick Check:
Comparison needs '==' not '=' [OK]
Hint: Use '==' for comparison, '=' is assignment [OK]
Common Mistakes:
Using '=' in if condition instead of '=='
Thinking semicolons are required in Kotlin
Assuming val variables must be mutable for comparison
5. Given the Kotlin code below, what will be the output?
val a = 7
val b = 7
val c = 10
println((a == b) && (b < c))
hard
A. false
B. true
C. 7
D. Compilation error
Solution
Step 1: Evaluate the first comparison (a == b)
Since both a and b are 7, a == b is true.
Step 2: Evaluate the second comparison (b < c)
Since 7 is less than 10, b < c is true.
Step 3: Evaluate the logical AND
Both conditions are true, so true && true is true.
Final Answer:
true -> Option B
Quick Check:
(7 == 7) && (7 < 10) = true [OK]
Hint: Both conditions must be true for && to return true [OK]
Common Mistakes:
Confusing && with || operator
Misreading comparison results
Expecting numeric output instead of boolean
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.