📖 Scenario: Imagine you are building a simple app that tracks the status of orders in a store. Each order can only have one of a few fixed statuses, like Pending, Shipped, or Delivered. You want to make sure the status can never be set to something invalid by mistake.
🎯 Goal: You will create an enum class in Kotlin to hold the allowed order statuses. Then you will use this enum to assign and print the status of an order, showing how enums help keep values safe and limited.
📋 What You'll Learn
Create an enum class called OrderStatus with values PENDING, SHIPPED, and DELIVERED
Create a variable called currentStatus and set it to OrderStatus.PENDING
Change currentStatus to OrderStatus.SHIPPED
Print the value of currentStatus to show the current order status
💡 Why This Matters
🌍 Real World
Enums are used in apps to keep data clean and predictable, like order statuses, user roles, or traffic light colors.
💼 Career
Understanding enums is important for writing safe, clear code in Kotlin, which is widely used for Android apps and backend services.
Progress0 / 4 steps
1
Create the enum class for order statuses
Create an enum class called OrderStatus with the values PENDING, SHIPPED, and DELIVERED.
Kotlin
Hint
Use enum class OrderStatus { PENDING, SHIPPED, DELIVERED } to define the fixed set of statuses.
2
Create a variable for the current order status
Create a variable called currentStatus and set it to OrderStatus.PENDING.
Kotlin
Hint
Use var currentStatus = OrderStatus.PENDING to start with the pending status.
3
Change the current status to shipped
Change the variable currentStatus to OrderStatus.SHIPPED.
Kotlin
Hint
Assign OrderStatus.SHIPPED to currentStatus to update the status.
4
Print the current order status
Write a println statement to print the value of currentStatus.
Kotlin
Hint
Use println(currentStatus) to show the current status on the screen.
Practice
(1/5)
1. Why do Kotlin enums constrain values to a fixed set?
easy
A. To prevent invalid values and reduce errors
B. To allow any value to be assigned
C. To make the code run faster
D. To increase the size of the program
Solution
Step 1: Understand enum purpose
Enums define a limited set of named values to represent options clearly.
Step 2: Identify why limiting values helps
Limiting values prevents invalid or unexpected inputs, reducing bugs.
Final Answer:
To prevent invalid values and reduce errors -> Option A
Quick Check:
Enums limit values = prevent errors [OK]
Hint: Enums fix allowed values to avoid mistakes [OK]
Common Mistakes:
Thinking enums allow any value
Believing enums improve speed directly
Confusing enums with variables
2. Which of the following is the correct syntax to declare an enum in Kotlin?
easy
A. enum Direction = { NORTH, SOUTH, EAST, WEST }
B. class enum Direction { NORTH, SOUTH, EAST, WEST }
C. enum class Direction { NORTH, SOUTH, EAST, WEST }
D. enum Direction { NORTH; SOUTH; EAST; WEST }
Solution
Step 1: Recall Kotlin enum syntax
Kotlin enums use 'enum class' followed by the name and curly braces with values separated by commas.
Step 2: Check each option
enum class Direction { NORTH, SOUTH, EAST, WEST } matches correct syntax; others have wrong keywords or separators.
Final Answer:
enum class Direction { NORTH, SOUTH, EAST, WEST } -> Option C
Quick Check:
Correct enum syntax = enum class Direction { NORTH, SOUTH, EAST, WEST } [OK]
Hint: Remember: 'enum class Name { VALUES }' [OK]
Common Mistakes:
Using 'class enum' instead of 'enum class'
Separating values with semicolons
Assigning enum like a variable
3. What will be the output of this Kotlin code?
enum class Color { RED, GREEN, BLUE }
fun main() {
val c: Color = Color.GREEN
println(c == Color.GREEN)
println(c == Color.RED)
}
medium
A. false\nfalse
B. false\ntrue
C. true\ntrue
D. true\nfalse
Solution
Step 1: Understand enum comparison
Enum values are compared by identity. c is assigned Color.GREEN.
Step 2: Evaluate println statements
c == Color.GREEN is true; c == Color.RED is false.
Final Answer:
true\nfalse -> Option D
Quick Check:
Enum equality matches assigned value [OK]
Hint: Enum comparisons check exact named value [OK]
Common Mistakes:
Assuming enums compare by string content
Confusing true/false order
Using == incorrectly with enums
4. Identify the error in this Kotlin enum usage:
enum class Size { SMALL, MEDIUM, LARGE }
fun main() {
val s: Size = Size.EXTRA_LARGE
println(s)
}
medium
A. Cannot print enum values directly
B. Size.EXTRA_LARGE does not exist in the enum
C. Enum values must be lowercase
D. Missing semicolon after enum values
Solution
Step 1: Check enum declaration
Enum Size has only SMALL, MEDIUM, LARGE values defined.
Step 2: Analyze usage error
Code tries to assign Size.EXTRA_LARGE which is not defined, causing error.
Final Answer:
Size.EXTRA_LARGE does not exist in the enum -> Option B
Quick Check:
Using undefined enum value = error [OK]
Hint: Use only defined enum values [OK]
Common Mistakes:
Assuming enums accept any value
Thinking semicolons are required
Believing enum values must be lowercase
5. You want to restrict user input to only three modes: EASY, MEDIUM, HARD using Kotlin enums. Which approach best ensures invalid modes cannot be used in your program?
hard
A. Define enum class Mode { EASY, MEDIUM, HARD } and use Mode type for input
B. Use strings "EASY", "MEDIUM", "HARD" and check with if-else
C. Use integers 1, 2, 3 to represent modes without enum
D. Allow any string input and convert to uppercase
Solution
Step 1: Understand enum benefits for input restriction
Enums limit values to a fixed set, preventing invalid inputs.
Step 2: Compare approaches
Define enum class Mode { EASY, MEDIUM, HARD } and use Mode type for input uses enum type, ensuring only EASY, MEDIUM, HARD are valid. Others allow invalid inputs or require manual checks.
Final Answer:
Define enum class Mode { EASY, MEDIUM, HARD } and use Mode type for input -> Option A
Quick Check:
Enums enforce valid values automatically [OK]
Hint: Use enums to restrict allowed options [OK]
Common Mistakes:
Using strings without validation
Using integers without clear meaning
Allowing any input without checks
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.