Challenge - 5 Problems
Set Operations Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Union operation on two sets
What is the output of the following C# code that uses Union on two integer sets?
C Sharp (C#)
using System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { var setA = new HashSet<int> {1, 2, 3}; var setB = new HashSet<int> {3, 4, 5}; var unionSet = setA.Union(setB); foreach(var item in unionSet) { Console.Write(item + " "); } } }
Attempts:
2 left
💡 Hint
Union combines all unique elements from both sets.
✗ Incorrect
The Union method returns all distinct elements from both sets. Since 3 is common, it appears once. So the output is 1 2 3 4 5.
❓ Predict Output
intermediate2:00remaining
Output of Intersect operation on two sets
What does the following C# code print when using Intersect on two sets?
C Sharp (C#)
using System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { var setA = new HashSet<int> {10, 20, 30, 40}; var setB = new HashSet<int> {30, 40, 50, 60}; var intersectSet = setA.Intersect(setB); foreach(var item in intersectSet) { Console.Write(item + " "); } } }
Attempts:
2 left
💡 Hint
Intersect returns only elements present in both sets.
✗ Incorrect
Only 30 and 40 are in both sets, so the output is 30 40.
❓ Predict Output
advanced2:00remaining
Output of Except operation on two sets
What is the output of this C# code using Except to find elements in setA not in setB?
C Sharp (C#)
using System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { var setA = new HashSet<string> {"apple", "banana", "cherry"}; var setB = new HashSet<string> {"banana", "date"}; var exceptSet = setA.Except(setB); foreach(var item in exceptSet) { Console.Write(item + " "); } } }
Attempts:
2 left
💡 Hint
Except returns elements in the first set that are not in the second.
✗ Incorrect
Only "apple" and "cherry" are in setA but not in setB, so output is apple cherry.
🔧 Debug
advanced2:00remaining
Identify the error in set operation code
This C# code tries to get the union of two sets but causes a compile error. What is the error?
C Sharp (C#)
using System; using System.Collections.Generic; class Program { static void Main() { var setA = new HashSet<int> {1, 2, 3}; var setB = new HashSet<int> {4, 5, 6}; var unionSet = setA.Union(setB); foreach(int item in unionSet) { Console.WriteLine(item); } } }
Attempts:
2 left
💡 Hint
Check if all required namespaces are included for LINQ methods.
✗ Incorrect
Union is an extension method in System.Linq namespace. Without 'using System.Linq;', Union() is not recognized, causing a compile error.
🧠 Conceptual
expert2:00remaining
Number of elements after combined set operations
Given these sets:
Set A = {1, 2, 3, 4, 5}
Set B = {4, 5, 6, 7}
Set C = {5, 7, 8, 9}
What is the number of elements in the set resulting from:
Set A = {1, 2, 3, 4, 5}
Set B = {4, 5, 6, 7}
Set C = {5, 7, 8, 9}
What is the number of elements in the set resulting from:
var result = A.Union(B).Intersect(C);Attempts:
2 left
💡 Hint
First find A union B, then find intersection with C.
✗ Incorrect
A union B = {1,2,3,4,5,6,7}. Intersect with C = {5,7,8,9} gives {5,7}. So the count is 2.