Challenge - 5 Problems
Return Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this C# code with return values?
Look at this code. What will it print when run?
C Sharp (C#)
using System; class Program { static int MultiplyByTwo(int x) { return x * 2; } static void Main() { int result = MultiplyByTwo(5); Console.WriteLine(result); } }
Attempts:
2 left
💡 Hint
The method MultiplyByTwo returns the input number multiplied by 2.
✗ Incorrect
The method MultiplyByTwo returns 5 * 2 = 10. The Main method prints this value.
❓ Predict Output
intermediate2:00remaining
What will this void method print?
Check this code. What does it print when run?
C Sharp (C#)
using System; class Program { static void PrintMessage() { Console.WriteLine("Hello World"); } static void Main() { PrintMessage(); } }
Attempts:
2 left
💡 Hint
Void methods can print to the screen using Console.WriteLine.
✗ Incorrect
The method PrintMessage prints "Hello World". Void means it returns no value but can still print.
❓ Predict Output
advanced2:00remaining
What is the output of this method returning a string?
What does this program print when run?
C Sharp (C#)
using System; class Program { static string GetGreeting(string name) { return $"Hi, {name}!"; } static void Main() { string greet = GetGreeting("Anna"); Console.WriteLine(greet); } }
Attempts:
2 left
💡 Hint
The method uses string interpolation to insert the name.
✗ Incorrect
The method returns the string with the name inserted using $"..." syntax. It prints "Hi, Anna!".
❓ Predict Output
advanced2:00remaining
What happens if you try to return a value from a void method?
Look at this code. What error will it cause?
C Sharp (C#)
using System; class Program { static void DoSomething() { return 5; } static void Main() { DoSomething(); } }
Attempts:
2 left
💡 Hint
Void methods cannot return any value.
✗ Incorrect
The method DoSomething is declared void but tries to return 5, which is not allowed and causes a compile error.
🧠 Conceptual
expert2:00remaining
How many items are in the list after this method runs?
Consider this code. How many items does the list contain after Main runs?
C Sharp (C#)
using System; using System.Collections.Generic; class Program { static List<int> AddNumbers() { var list = new List<int>(); for (int i = 0; i < 3; i++) { list.Add(i); } return list; } static void Main() { var numbers = AddNumbers(); numbers.Add(3); Console.WriteLine(numbers.Count); } }
Attempts:
2 left
💡 Hint
The method adds 3 items, then Main adds one more.
✗ Incorrect
AddNumbers creates a list with 3 items (0,1,2). Main adds one more item (3). Total is 4.