Challenge - 5 Problems
Extension Method Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of an extension method on string
What is the output of this C# code that uses an extension method on a string?
C Sharp (C#)
public static class StringExtensions { public static string Repeat(this string s, int times) { return string.Concat(System.Linq.Enumerable.Repeat(s, times)); } } class Program { static void Main() { string word = "Hi"; System.Console.WriteLine(word.Repeat(3)); } }
Attempts:
2 left
💡 Hint
Think about what string.Concat and Enumerable.Repeat do together.
✗ Incorrect
The extension method Repeat repeats the string the specified number of times by using Enumerable.Repeat and concatenating the results. So "Hi" repeated 3 times is "HiHiHi".
❓ Predict Output
intermediate2:00remaining
Output of extension method on int array
What is the output of this C# code that uses an extension method on an int array?
C Sharp (C#)
public static class ArrayExtensions { public static int SumPositive(this int[] arr) { int sum = 0; foreach (var n in arr) { if (n > 0) sum += n; } return sum; } } class Program { static void Main() { int[] numbers = { -1, 2, 3, -4, 5 }; System.Console.WriteLine(numbers.SumPositive()); } }
Attempts:
2 left
💡 Hint
Add only positive numbers in the array.
✗ Incorrect
The SumPositive extension method adds only positive numbers: 2 + 3 + 5 = 10.
🔧 Debug
advanced2:00remaining
Identify the error in this extension method
This extension method is intended to check if a string contains only digits. What error will this code cause when compiled?
C Sharp (C#)
public static class StringExtensions { public static bool IsDigitsOnly(string str) { foreach (char c in str) { if (!char.IsDigit(c)) return false; } return true; } } class Program { static void Main() { string s = "12345"; System.Console.WriteLine(s.IsDigitsOnly()); } }
Attempts:
2 left
💡 Hint
Check the method signature for extension methods.
✗ Incorrect
Extension methods must have the 'this' keyword before the first parameter to indicate which type they extend. This method lacks 'this' so it causes a compile error.
📝 Syntax
advanced2:00remaining
Which option correctly defines an extension method for List?
Which of the following options correctly defines an extension method named SumSquares that returns the sum of squares of all integers in a List?
Attempts:
2 left
💡 Hint
Extension methods must be static and have 'this' before the first parameter with the exact type.
✗ Incorrect
Option A correctly uses 'static' and 'this List' as the first parameter. Option A is not static. Option A lacks 'this'. Option A uses List without generic type which is invalid here.
🚀 Application
expert2:00remaining
Result of chaining extension methods on string
Given these extension methods, what is the output of the program?
C Sharp (C#)
public static class StringExtensions { public static string AddExclamation(this string s) => s + "!"; public static string RepeatTwice(this string s) => s + s; } class Program { static void Main() { string greeting = "Hello"; var result = greeting.AddExclamation().RepeatTwice(); System.Console.WriteLine(result); } }
Attempts:
2 left
💡 Hint
Apply the methods step by step: first add exclamation, then repeat twice.
✗ Incorrect
AddExclamation adds '!' to 'Hello' making 'Hello!'. RepeatTwice duplicates that string, resulting in 'Hello!Hello!'.