Bird
Raised Fist0
Cprogramming~20 mins

Enum declaration - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Enum Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of enum variable assignment

What is the output of this C code?

C
#include <stdio.h>

enum Color { RED, GREEN, BLUE };

int main() {
    enum Color c = GREEN;
    printf("%d", c);
    return 0;
}
A0
B1
CCompilation error
D2
Attempts:
2 left
💡 Hint

Remember that enum values start from 0 by default.

❓ Predict Output
intermediate
2:00remaining
Enum with custom values output

What will this program print?

C
#include <stdio.h>

enum Status { OK = 5, WARNING = 10, ERROR = 15 };

int main() {
    enum Status s = WARNING;
    printf("%d", s);
    return 0;
}
A0
B5
C15
D10
Attempts:
2 left
💡 Hint

Check the assigned values for each enum member.

🔧 Debug
advanced
2:00remaining
Identify the error in enum declaration

What error will this code produce?

C
enum Days { MON, TUE, WED = "3", THU, FRI };

int main() {
    enum Days today = WED;
    return 0;
}
ANo error, compiles fine
BWarning: implicit conversion from string to int
CTypeError: enum member must be integer constant
DSyntaxError: enum member cannot be a string
Attempts:
2 left
💡 Hint

Enum values must be integer constants.

❓ Predict Output
advanced
2:00remaining
Enum auto-increment behavior

What is the output of this program?

C
#include <stdio.h>

enum Numbers { ONE = 1, TWO, FIVE = 5, SIX };

int main() {
    printf("%d %d %d %d", ONE, TWO, FIVE, SIX);
    return 0;
}
A1 2 5 6
B1 1 5 6
C1 2 5 5
D1 2 6 7
Attempts:
2 left
💡 Hint

Enum values auto-increment from the previous value.

🧠 Conceptual
expert
2:00remaining
Number of items in enum

How many items are in this enum?

C
enum Letters { A = 3, B, C = 7, D, E };
A5
B4
C3
D6
Attempts:
2 left
💡 Hint

Count all declared enum members regardless of their values.

Practice

(1/5)
1. What is the main purpose of using enum in C programming?
easy
A. To declare a function prototype
B. To give names to a set of integer constants for better code readability
C. To create a new data type for floating-point numbers
D. To allocate memory dynamically

Solution

  1. Step 1: Understand enum usage

    An enum is used to assign names to integer constants, making code easier to read and maintain.
  2. Step 2: Compare other options

    Other options describe unrelated concepts: function prototypes, floating-point types, and memory allocation.
  3. Final Answer:

    To give names to a set of integer constants for better code readability -> Option B
  4. Quick Check:

    Enum = Named integer constants [OK]
Hint: Enums name numbers to make code clearer [OK]
Common Mistakes:
  • Confusing enums with structs or unions
  • Thinking enums allocate memory dynamically
  • Assuming enums are for floating-point types
2. Which of the following is the correct syntax to declare an enum named Color with values RED, GREEN, and BLUE?
easy
A. enum Color { RED, GREEN, BLUE };
B. enum Color = { RED, GREEN, BLUE };
C. enum Color ( RED, GREEN, BLUE );
D. enum Color: RED, GREEN, BLUE;

Solution

  1. Step 1: Recall enum declaration syntax

    The correct syntax uses curly braces to list values after the enum name without assignment or colons.
  2. Step 2: Check each option

    enum Color { RED, GREEN, BLUE }; uses correct syntax: enum Color { RED, GREEN, BLUE };. Others use invalid symbols like '=', '(', or ':'.
  3. Final Answer:

    enum Color { RED, GREEN, BLUE }; -> Option A
  4. Quick Check:

    Enum syntax = enum Name { values } [OK]
Hint: Use curly braces and no equals sign in enum declaration [OK]
Common Mistakes:
  • Using '=' instead of braces
  • Using parentheses instead of braces
  • Using colons after enum name
3. What will be the output of the following C code?
enum Day { SUN, MON, TUE = 5, WED, THU };
#include 
int main() {
    printf("%d %d %d %d %d", SUN, MON, TUE, WED, THU);
    return 0;
}
medium
A. 0 1 5 5 5
B. 0 1 2 3 4
C. 1 2 5 6 7
D. 0 1 5 6 7

Solution

  1. Step 1: Understand enum value assignment

    By default, enum values start at 0 and increment by 1. Here, SUN=0, MON=1. TUE is explicitly set to 5.
  2. Step 2: Calculate following values

    After TUE=5, WED becomes 6, and THU becomes 7 automatically.
  3. Final Answer:

    0 1 5 6 7 -> Option D
  4. Quick Check:

    Explicit enum value sets next values incrementally [OK]
Hint: Explicit enum value resets counting from that number [OK]
Common Mistakes:
  • Assuming all values increment from zero ignoring explicit assignment
  • Thinking WED and THU have same value as TUE
  • Starting enum values from 1 by default
4. Identify the error in the following enum declaration:
enum Status {
    OK = 1,
    ERROR,
    WARNING = "2"
};
medium
A. ERROR must be assigned a value explicitly
B. Missing semicolon after enum declaration
C. Enum values must be integers, not strings
D. Enum names cannot be uppercase

Solution

  1. Step 1: Check enum value types

    Enum values must be integer constants. Assigning a string like "2" is invalid.
  2. Step 2: Verify other parts

    Semicolon is present, uppercase names are allowed, and ERROR can be auto-assigned after OK=1.
  3. Final Answer:

    Enum values must be integers, not strings -> Option C
  4. Quick Check:

    Enum values = integers only [OK]
Hint: Enum values must be integers, not strings [OK]
Common Mistakes:
  • Using string literals as enum values
  • Forgetting semicolon after enum
  • Thinking enum names must be lowercase
5. Given the enum declaration:
enum Flags { FLAG_A = 1 << 0, FLAG_B = 1 << 1, FLAG_C = 1 << 2 };

int flags = FLAG_A | FLAG_C;
Which expression correctly checks if FLAG_B is set in flags?
hard
A. (flags & FLAG_B) != 0
B. (flags | FLAG_B) == 0
C. (flags & FLAG_B) == FLAG_B + 1
D. (flags ^ FLAG_B) == 0

Solution

  1. Step 1: Understand bitwise flag checking

    To check if a flag is set, use bitwise AND and compare to zero.
  2. Step 2: Analyze each option

    (flags & FLAG_B) != 0 correctly checks if FLAG_B bit is set by (flags & FLAG_B) != 0. Others use incorrect bitwise operations or comparisons.
  3. Final Answer:

    (flags & FLAG_B) != 0 -> Option A
  4. Quick Check:

    Bitwise AND with flag and compare to zero [OK]
Hint: Use bitwise AND and compare to zero to check flags [OK]
Common Mistakes:
  • Using bitwise OR instead of AND for checking
  • Comparing to wrong values
  • Using XOR to check flags
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