Challenge - 5 Problems
Method Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a method call with parameters
What is the output of the following C# program?
C Sharp (C#)
using System; class Program { static int Multiply(int a, int b) { return a * b; } static void Main() { Console.WriteLine(Multiply(3, 4)); } }
Attempts:
2 left
💡 Hint
Look at what the Multiply method returns when given 3 and 4.
✗ Incorrect
The Multiply method returns the product of a and b. 3 * 4 = 12, so the output is 12.
❓ Predict Output
intermediate2:00remaining
Return value from a void method
What will happen when this C# code runs?
C Sharp (C#)
using System; class Program { static void PrintMessage() { Console.WriteLine("Hello World"); } static void Main() { var result = PrintMessage(); Console.WriteLine(result == null); } }
Attempts:
2 left
💡 Hint
Void methods do not return a value and cannot be assigned to a variable.
✗ Incorrect
In C#, you cannot assign the result of a void method to a variable because void methods do not return a value. This causes a compilation error.
❓ Predict Output
advanced2:00remaining
Method overloading and output
What is the output of this C# program?
C Sharp (C#)
using System; class Program { static void Display(int x) { Console.WriteLine("Int: " + x); } static void Display(string x) { Console.WriteLine("String: " + x); } static void Main() { Display(5); Display("5"); } }
Attempts:
2 left
💡 Hint
Look at how method overloading works with different parameter types.
✗ Incorrect
The method Display is overloaded for int and string. Calling Display(5) calls the int version, Display("5") calls the string version.
❓ Predict Output
advanced2:00remaining
Output of recursive method call
What will this C# program print?
C Sharp (C#)
using System; class Program { static int Factorial(int n) { if (n <= 1) return 1; return n * Factorial(n - 1); } static void Main() { Console.WriteLine(Factorial(4)); } }
Attempts:
2 left
💡 Hint
Factorial of 4 is 4 * 3 * 2 * 1.
✗ Incorrect
The Factorial method calls itself recursively until n is 1 or less, multiplying all numbers down to 1. 4! = 24.
❓ Predict Output
expert2:00remaining
Effect of method parameters on output
Consider this C# code. What is the output after running Main?
C Sharp (C#)
using System; class Program { static void ChangeValue(int x) { x = 10; } static void Main() { int a = 5; ChangeValue(a); Console.WriteLine(a); } }
Attempts:
2 left
💡 Hint
Think about whether the method changes the original variable or just a copy.
✗ Incorrect
In C#, method parameters are passed by value by default. ChangeValue changes only the copy of a, so a remains 5.