An enum is a special class that represents a fixed set of constants. It helps group related values together in a clear way.
Click to reveal answer
beginner
Why do enums constrain values?
Enums limit values to a predefined set. This prevents errors by ensuring only valid options are used.
Click to reveal answer
intermediate
How do enums improve code safety?
By restricting values to known constants, enums avoid invalid inputs and make code easier to understand and maintain.
Click to reveal answer
beginner
Give an example of enum usage in Kotlin.
Example:
<pre>enum class Direction { NORTH, SOUTH, EAST, WEST }</pre>
This enum limits directions to these four options only.
Click to reveal answer
intermediate
What happens if you try to assign a value not in the enum?
You get a compile-time error. Kotlin won’t allow values outside the enum constants, preventing bugs early.
Click to reveal answer
What does an enum in Kotlin represent?
AA variable number of values
BA data type for strings only
CA function
DA fixed set of constants
✗ Incorrect
Enums represent a fixed set of constants, grouping related values together.
Why are enums useful for constraining values?
AThey make code slower
BThey allow any value to be assigned
CThey restrict values to predefined options
DThey convert values to strings
✗ Incorrect
Enums restrict values to predefined options, preventing invalid inputs.
What error occurs if you assign a value not in the enum?
ACompile-time error
BLogical error only
CNo error
DRuntime error
✗ Incorrect
Kotlin throws a compile-time error if a value outside the enum constants is assigned.
Which of these is a valid Kotlin enum declaration?
Aenum class Color { RED, GREEN, BLUE }
Benum Color = { RED, GREEN, BLUE }
Cclass enum Color { RED, GREEN, BLUE }
Denum Color: RED, GREEN, BLUE
✗ Incorrect
The correct syntax is 'enum class Color { RED, GREEN, BLUE }'.
How do enums help with code readability?
ABy hiding values
BBy grouping related constants with clear names
CBy allowing any value
DBy making code longer
✗ Incorrect
Enums group related constants with clear names, making code easier to read.
Explain why enums constrain values and how this helps prevent errors.
Think about how limiting options can stop mistakes.
You got /4 concepts.
Describe a real-life example where using an enum would be helpful in Kotlin.
Imagine you only want to allow certain fixed options.
You got /4 concepts.
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.