0
0
C Sharp (C#)programming~20 mins

Method declaration and calling in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Method Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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));
    }
}
AError: Method Multiply not found
B7
C12
D34
Attempts:
2 left
💡 Hint
Look at what the Multiply method returns when given 3 and 4.
Predict Output
intermediate
2: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);
    }
}
AHello World\nTrue
BCompilation error: Cannot assign void to variable
CHello World\nFalse
DRuntime error
Attempts:
2 left
💡 Hint
Void methods do not return a value and cannot be assigned to a variable.
Predict Output
advanced
2: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");
    }
}
AString: 5\nInt: 5
BRuntime error
CCompilation error: Ambiguous call to Display
DInt: 5\nString: 5
Attempts:
2 left
💡 Hint
Look at how method overloading works with different parameter types.
Predict Output
advanced
2: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));
    }
}
A24
B10
CError: Stack overflow
D1
Attempts:
2 left
💡 Hint
Factorial of 4 is 4 * 3 * 2 * 1.
Predict Output
expert
2: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);
    }
}
A5
B10
CCompilation error
DRuntime error
Attempts:
2 left
💡 Hint
Think about whether the method changes the original variable or just a copy.