Bird
Raised Fist0
Kotlinprogramming~5 mins

Enum class declaration in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is an enum class in Kotlin?
An enum class in Kotlin is a special class that represents a group of constants. It helps to define a fixed set of related values, like days of the week or directions.
Click to reveal answer
beginner
How do you declare a simple enum class in Kotlin?
Use the keyword <code>enum class</code> followed by the name and list of constants inside curly braces. For example:<br><pre>enum class Direction { NORTH, SOUTH, EAST, WEST }</pre>
Click to reveal answer
intermediate
Can enum constants have properties and methods in Kotlin?
Yes! Enum constants can have their own properties and methods. You can define a constructor for the enum class and add functions to customize behavior.
Click to reveal answer
intermediate
What is the default superclass of an enum class in Kotlin?
All enum classes in Kotlin inherit from the built-in <code>Enum</code> class automatically. This gives them useful functions like <code>values()</code> and <code>valueOf()</code>.
Click to reveal answer
beginner
How do you access all constants of an enum class?
You can call the <code>values()</code> function on the enum class to get an array of all constants. For example:<br><pre>Direction.values()</pre>
Click to reveal answer
Which keyword is used to declare an enum class in Kotlin?
Aenum class
Benum
Cclass enum
DenumType
What does the values() function return when called on an enum class?
AThe first enum constant
BA list of all enum constants
CThe number of enum constants
DA random enum constant
Can enum constants in Kotlin have their own properties?
AOnly if they extend another class
BNo, enum constants are fixed and cannot have properties
CYes, by defining a constructor in the enum class
DOnly if declared as data classes
What is the superclass of all enum classes in Kotlin?
ANothing
BAny
CObject
DEnum
Which of the following is a valid enum class declaration?
Aenum class Color { RED, GREEN, BLUE }
Benum Color { RED, GREEN, BLUE }
Cclass enum Color { RED, GREEN, BLUE }
Denumclass Color { RED, GREEN, BLUE }
Explain how to declare an enum class in Kotlin and give an example.
Think about how you list fixed options like directions or colors.
You got /4 concepts.
    Describe how enum constants can have properties and methods in Kotlin.
    Consider how each constant can carry extra information.
    You got /4 concepts.

      Practice

      (1/5)
      1. What is the main purpose of an enum class in Kotlin?
      easy
      A. To define a function that returns multiple values
      B. To group a fixed set of related constants under one type
      C. To create a class that can be instantiated multiple times
      D. To declare a variable that can hold any data type

      Solution

      1. Step 1: Understand enum class purpose

        An enum class is designed to hold a fixed set of constants that belong together logically.
      2. Step 2: Compare options with enum usage

        Only To group a fixed set of related constants under one type correctly describes grouping related constants under one type, which is the main use of enum classes.
      3. Final Answer:

        To group a fixed set of related constants under one type -> Option B
      4. Quick Check:

        Enum class purpose = group constants [OK]
      Hint: Enums group fixed constants for clarity and safety [OK]
      Common Mistakes:
      • Thinking enums are for creating multiple instances
      • Confusing enums with regular classes
      • Believing enums can hold any data type variable
      2. Which of the following is the correct syntax to declare an enum class named Direction with constants NORTH, SOUTH, EAST, and WEST?
      easy
      A. enum class Direction { NORTH, SOUTH, EAST, WEST }
      B. class enum Direction { NORTH, SOUTH, EAST, WEST }
      C. enum Direction { NORTH; SOUTH; EAST; WEST }
      D. enum class Direction = (NORTH, SOUTH, EAST, WEST)

      Solution

      1. Step 1: Recall Kotlin enum class syntax

        Kotlin enum classes start with enum class followed by the name and curly braces containing constants separated by commas.
      2. Step 2: Check each option's syntax

        enum class Direction { NORTH, SOUTH, EAST, WEST } matches the correct syntax. Options A, C, and D have incorrect keywords, punctuation, or structure.
      3. Final Answer:

        enum class Direction { NORTH, SOUTH, EAST, WEST } -> Option A
      4. Quick Check:

        Correct enum syntax = enum class Direction { NORTH, SOUTH, EAST, WEST } [OK]
      Hint: Use 'enum class Name { CONST1, CONST2 }' syntax [OK]
      Common Mistakes:
      • Using 'class enum' instead of 'enum class'
      • Separating constants with semicolons instead of commas
      • Assigning enum constants with '=' instead of listing
      3. What will be the output of the following Kotlin code?
      enum class Color { RED, GREEN, BLUE }
      fun main() {
          val c = Color.GREEN
          println(c.name)
      }
      medium
      A. green
      B. Color.GREEN
      C. 1
      D. GREEN

      Solution

      1. Step 1: Understand enum constant property 'name'

        Each enum constant has a built-in name property that returns its exact name as a string.
      2. Step 2: Analyze the code output

        The code prints c.name where c is Color.GREEN, so it prints "GREEN" exactly as declared.
      3. Final Answer:

        GREEN -> Option D
      4. Quick Check:

        Enum constant name property = GREEN [OK]
      Hint: Enum.name returns constant name as string [OK]
      Common Mistakes:
      • Expecting lowercase output
      • Printing enum constant directly without .name
      • Confusing ordinal number with name
      4. Identify the error in this Kotlin enum class declaration:
      enum class Size {
          SMALL,
          MEDIUM
          LARGE
      }
      medium
      A. Cannot declare enum class without constructor
      B. Enum constants must be lowercase
      C. Missing comma after MEDIUM
      D. Enum class must have at least 5 constants

      Solution

      1. Step 1: Check enum constants separation

        Enum constants must be separated by commas except the last one. Here, MEDIUM is missing a comma after it.
      2. Step 2: Validate other options

        Constants can be uppercase, no constructor is required, and no minimum count is needed. So only missing comma is the error.
      3. Final Answer:

        Missing comma after MEDIUM -> Option C
      4. Quick Check:

        Enum constants separated by commas [OK]
      Hint: Separate enum constants with commas except last [OK]
      Common Mistakes:
      • Omitting commas between constants
      • Using lowercase for constants (not required)
      • Thinking constructor is mandatory
      5. How can you add a property rgb to an enum class Color that stores the color's RGB integer value, and access it for Color.RED? Choose the correct code snippet.
      hard
      A. enum class Color(val rgb: Int) { RED(0xFF0000), GREEN(0x00FF00), BLUE(0x0000FF) } fun main() { println(Color.RED.rgb) }
      B. enum class Color { RED = 0xFF0000, GREEN = 0x00FF00, BLUE = 0x0000FF } fun main() { println(Color.RED.rgb) }
      C. enum class Color { RED(0xFF0000), GREEN(0x00FF00), BLUE(0x0000FF) } fun main() { println(Color.RED.rgb) }
      D. enum class Color { RED, GREEN, BLUE } fun main() { println(Color.RED.rgb) }

      Solution

      1. Step 1: Understand enum property declaration

        To add a property, declare a constructor parameter in the enum class and pass values to each constant.
      2. Step 2: Check each option for correct syntax

        enum class Color(val rgb: Int) { RED(0xFF0000), GREEN(0x00FF00), BLUE(0x0000FF) } fun main() { println(Color.RED.rgb) } correctly declares val rgb: Int in constructor and assigns values. enum class Color { RED = 0xFF0000, GREEN = 0x00FF00, BLUE = 0x0000FF } fun main() { println(Color.RED.rgb) } uses invalid assignment syntax. enum class Color { RED(0xFF0000), GREEN(0x00FF00), BLUE(0x0000FF) } fun main() { println(Color.RED.rgb) } misses constructor declaration. enum class Color { RED, GREEN, BLUE } fun main() { println(Color.RED.rgb) } has no property defined.
      3. Final Answer:

        enum class Color(val rgb: Int) { RED(0xFF0000), GREEN(0x00FF00), BLUE(0x0000FF) } fun main() { println(Color.RED.rgb) } -> Option A
      4. Quick Check:

        Enum property via constructor = enum class Color(val rgb: Int) { RED(0xFF0000), GREEN(0x00FF00), BLUE(0x0000FF) } fun main() { println(Color.RED.rgb) } [OK]
      Hint: Add properties via constructor parameters in enum class [OK]
      Common Mistakes:
      • Trying to assign values with '=' inside enum constants
      • Not declaring constructor parameters in enum class
      • Accessing property that was never declared
      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