0
0
C Sharp (C#)programming~20 mins

Flags attribute and bitwise enums in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flags Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of combined flags with bitwise OR
What is the output of this C# code using a Flags enum?
C Sharp (C#)
using System;

[Flags]
public enum Permissions
{
    None = 0,
    Read = 1,
    Write = 2,
    Execute = 4
}

class Program
{
    static void Main()
    {
        Permissions userPerms = Permissions.Read | Permissions.Write;
        Console.WriteLine(userPerms);
    }
}
A"Read"
B"Read, Write"
C"3"
D"Write"
Attempts:
2 left
💡 Hint
Look at how Flags attribute changes enum ToString output when combined with bitwise OR.
Predict Output
intermediate
2:00remaining
Check if a flag is set using bitwise AND
What will this program print when checking if the Write flag is set?
C Sharp (C#)
using System;

[Flags]
public enum Access
{
    None = 0,
    Read = 1,
    Write = 2,
    Delete = 4
}

class Program
{
    static void Main()
    {
        Access rights = Access.Read | Access.Delete;
        bool canWrite = (rights & Access.Write) == Access.Write;
        Console.WriteLine(canWrite);
    }
}
ARuntime exception
BTrue
CCompile error
DFalse
Attempts:
2 left
💡 Hint
Bitwise AND with a flag returns the flag if set, otherwise zero.
Predict Output
advanced
2:00remaining
Output of combined flags with overlapping values
What is the output of this code with overlapping enum values and Flags attribute?
C Sharp (C#)
using System;

[Flags]
public enum Options
{
    None = 0,
    Alpha = 1,
    Beta = 2,
    Gamma = 3
}

class Program
{
    static void Main()
    {
        Options opt = Options.Alpha | Options.Beta;
        Console.WriteLine(opt);
    }
}
A"Gamma"
B"Alpha"
C"Alpha, Beta"
D"3"
Attempts:
2 left
💡 Hint
Check the numeric values and how bitwise OR combines them.
Predict Output
advanced
2:00remaining
Count how many flags are set in a Flags enum
What is the output of this program that counts set flags?
C Sharp (C#)
using System;

[Flags]
public enum Features
{
    None = 0,
    FeatureA = 1,
    FeatureB = 2,
    FeatureC = 4,
    FeatureD = 8
}

class Program
{
    static void Main()
    {
        Features enabled = Features.FeatureA | Features.FeatureC | Features.FeatureD;
        int count = 0;
        foreach (Features f in Enum.GetValues(typeof(Features)))
        {
            if (f != Features.None && (enabled & f) == f)
                count++;
        }
        Console.WriteLine(count);
    }
}
A3
B4
C2
D1
Attempts:
2 left
💡 Hint
Count how many individual flags are included in the combined value.
🧠 Conceptual
expert
2:00remaining
Why use powers of two in Flags enums?
Why must each flag in a Flags enum have a value that is a power of two?
ATo make the enum values easier to read as decimal numbers
BTo prevent the enum from being used in switch statements
CTo ensure each flag represents a unique bit so they can be combined without overlap
DTo allow the enum to be sorted alphabetically by value
Attempts:
2 left
💡 Hint
Think about how bits combine in binary when using bitwise OR.