Challenge - 5 Problems
Flags Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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); } }
Attempts:
2 left
💡 Hint
Look at how Flags attribute changes enum ToString output when combined with bitwise OR.
✗ Incorrect
The Flags attribute allows combined enum values to be displayed as a comma-separated list of names. Here, Read (1) OR Write (2) equals 3, but ToString shows "Read, Write".
❓ Predict Output
intermediate2: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); } }
Attempts:
2 left
💡 Hint
Bitwise AND with a flag returns the flag if set, otherwise zero.
✗ Incorrect
The rights variable has Read and Delete flags set, but not Write. So (rights & Access.Write) equals 0, which is not equal to Access.Write (2), so canWrite is false.
❓ Predict Output
advanced2: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); } }
Attempts:
2 left
💡 Hint
Check the numeric values and how bitwise OR combines them.
✗ Incorrect
Alpha is 1, Beta is 2, so Alpha | Beta is 3. Gamma is also 3. The Flags attribute ToString returns the exact matching name if found, so it prints "Gamma".
❓ Predict Output
advanced2: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); } }
Attempts:
2 left
💡 Hint
Count how many individual flags are included in the combined value.
✗ Incorrect
FeatureA, FeatureC, and FeatureD are set, so count is 3.
🧠 Conceptual
expert2: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?
Attempts:
2 left
💡 Hint
Think about how bits combine in binary when using bitwise OR.
✗ Incorrect
Using powers of two ensures each flag corresponds to a unique bit position. This allows combining flags with bitwise OR without losing information or causing overlap.