Bird
Raised Fist0
C Sharp (C#)programming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the correct way to declare a method named Greet that returns nothing and takes no parameters in C#?
easy
A. public Greet() void { }
B. void Greet[] { }
C. void Greet { }
D. public void Greet() { }

Solution

  1. Step 1: Understand method declaration syntax

    A method declaration starts with an access modifier (like public), then the return type (void means no return), then the method name, and parentheses for parameters.
  2. Step 2: Check each option's syntax

    public void Greet() { } correctly uses 'public void Greet() { }'. Options A, B, and D have syntax errors like wrong brackets or missing parentheses.
  3. Final Answer:

    public void Greet() { } -> Option D
  4. Quick Check:

    Method declaration = public void Greet() { } [OK]
Hint: Method syntax: access + return type + name + parentheses [OK]
Common Mistakes:
  • Using square brackets [] instead of parentheses ()
  • Omitting parentheses after method name
  • Placing return type after method name
2. Which of the following is the correct way to call a method named CalculateSum that takes two integers as parameters?
easy
A. CalculateSum 5, 10;
B. CalculateSum[5, 10];
C. CalculateSum(5, 10);
D. CalculateSum{5, 10};

Solution

  1. Step 1: Recall method call syntax

    To call a method, use its name followed by parentheses containing arguments separated by commas.
  2. Step 2: Evaluate each option

    CalculateSum(5, 10); uses parentheses correctly. Options B, C, and D use incorrect brackets or omit parentheses, causing syntax errors.
  3. Final Answer:

    CalculateSum(5, 10); -> Option C
  4. Quick Check:

    Method call = name + parentheses + arguments [OK]
Hint: Call methods with parentheses and comma-separated arguments [OK]
Common Mistakes:
  • Using square brackets instead of parentheses
  • Omitting parentheses when calling methods
  • Using curly braces for method calls
3. What will be the output of the following C# code?
public class Program {
  public static int Double(int x) {
    return x * 2;
  }
  public static void Main() {
    int result = Double(4);
    Console.WriteLine(result);
  }
}
medium
A. 8
B. 4
C. 16
D. Compilation error

Solution

  1. Step 1: Understand the method Double

    The method takes an integer x and returns x multiplied by 2.
  2. Step 2: Trace the method call in Main

    Double(4) returns 4 * 2 = 8, which is stored in result and printed.
  3. Final Answer:

    8 -> Option A
  4. Quick Check:

    Double(4) = 8 [OK]
Hint: Multiply input by 2, then print result [OK]
Common Mistakes:
  • Confusing return value with input
  • Expecting 16 by squaring instead of doubling
  • Thinking method does not return a value
4. Identify the error in the following method declaration and fix it:
public int AddNumbers(int a, int b) {
  int sum = a + b;
}
medium
A. Add a return statement: return sum;
B. Change method return type to void
C. Remove parameters from method
D. Add semicolon after method name

Solution

  1. Step 1: Check method return type and body

    The method declares it returns an int but does not return any value.
  2. Step 2: Fix by adding a return statement

    Adding return sum; returns the calculated value, matching the return type.
  3. Final Answer:

    Add a return statement: return sum; -> Option A
  4. Quick Check:

    Return value needed for non-void methods [OK]
Hint: Non-void methods must return a value [OK]
Common Mistakes:
  • Forgetting to return a value in non-void methods
  • Changing return type incorrectly
  • Removing parameters unnecessarily
5. You want to create a method IsEven that returns true if a number is even, otherwise false. Which of the following is the correct method declaration and call to check if 7 is even?
hard
A. public bool IsEven(int num) { if(num / 2 == 0) return true; else return false; } ... bool result = IsEven(7);
B. public bool IsEven(int num) { return num % 2 == 0; } ... bool result = IsEven(7);
C. public void IsEven(int num) { if(num % 2 == 0) return true; else return false; } ... bool result = IsEven(7);
D. public int IsEven(int num) { return num % 2; } ... int result = IsEven(7);

Solution

  1. Step 1: Check method return type and logic

    Method should return bool and use modulo (%) to check evenness. public bool IsEven(int num) { return num % 2 == 0; } ... bool result = IsEven(7); correctly returns true if remainder is 0.
  2. Step 2: Verify method call and variable type

    public bool IsEven(int num) { return num % 2 == 0; } ... bool result = IsEven(7); calls IsEven(7) and stores result in a bool variable, matching the method return type.
  3. Final Answer:

    public bool IsEven(int num) { return num % 2 == 0; } ... bool result = IsEven(7); -> Option B
  4. Quick Check:

    Use modulo and bool return for even check [OK]
Hint: Use 'return num % 2 == 0;' for even check [OK]
Common Mistakes:
  • Using division instead of modulo for even check
  • Returning int instead of bool
  • Declaring method void but trying to return a value
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard