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

Return values and void methods in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Return Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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);
    }
}
A10
B5
C0
DNothing, it causes a compile error
Attempts:
2 left
💡 Hint
The method MultiplyByTwo returns the input number multiplied by 2.
❓ Predict Output
intermediate
2: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();
    }
}
AHello World
BCompile error because void methods cannot be called
CHelloWorld (without space)
DNothing, because void methods don't print
Attempts:
2 left
💡 Hint
Void methods can print to the screen using Console.WriteLine.
❓ Predict Output
advanced
2: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);
    }
}
AHi Anna!
BHi, Anna!
CHi, {name}!
DCompile error due to string interpolation
Attempts:
2 left
💡 Hint
The method uses string interpolation to insert the name.
❓ Predict Output
advanced
2: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();
    }
}
ANo error, returns 5 silently
BPrints 5
CRuntime error
DCompile error: Cannot return a value from a method with void return type
Attempts:
2 left
💡 Hint
Void methods cannot return any value.
🧠 Conceptual
expert
2: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);
    }
}
A0
B3
C4
DCompile error
Attempts:
2 left
💡 Hint
The method adds 3 items, then Main adds one more.

Practice

(1/5)
1. Which statement correctly describes a void method in C#?
easy
A. It does not return any value.
B. It must return an integer value.
C. It always returns a string.
D. It returns a boolean value.

Solution

  1. Step 1: Understand the meaning of void in C#

    The keyword void means the method does not return any value.
  2. Step 2: Compare with other return types

    Methods with other return types like int, string, or bool must return a value of that type, but void methods do not return anything.
  3. Final Answer:

    It does not return any value. -> Option A
  4. Quick Check:

    Void means no return value [OK]
Hint: Void means no value comes back from the method [OK]
Common Mistakes:
  • Thinking void methods return a default value
  • Confusing void with returning null
  • Assuming void methods must return something
2. Which of the following is the correct syntax for a method that returns an integer value in C#?
easy
A. public void GetNumber() { return 5; }
B. public void GetNumber() { Console.WriteLine(5); return 5; }
C. public int GetNumber() { Console.WriteLine(5); }
D. public int GetNumber() { return 5; }

Solution

  1. Step 1: Identify method return type and return statement

    The method declares int return type, so it must return an integer value using return.
  2. Step 2: Check each option for correct syntax

    public int GetNumber() { return 5; } correctly declares int and returns 5. public void GetNumber() { return 5; } is void but tries to return a value (invalid). public int GetNumber() { Console.WriteLine(5); } returns nothing despite int return type. public void GetNumber() { Console.WriteLine(5); return 5; } is void but tries to return a value (invalid).
  3. Final Answer:

    public int GetNumber() { return 5; } -> Option D
  4. Quick Check:

    Return type matches returned value [OK]
Hint: Return type must match the returned value type [OK]
Common Mistakes:
  • Returning a value from a void method
  • Not returning a value from a non-void method
  • Using Console.WriteLine instead of return
3. What is the output of the following C# code?
public class Program {
  public static int Add(int a, int b) {
    return a + b;
  }
  public static void Main() {
    int result = Add(3, 4);
    Console.WriteLine(result);
  }
}
medium
A. 7
B. 34
C. 0
D. Compile error

Solution

  1. Step 1: Understand the Add method

    The method Add takes two integers and returns their sum.
  2. Step 2: Trace the Main method execution

    Main calls Add(3, 4), which returns 7. Then it prints 7 to the console.
  3. Final Answer:

    7 -> Option A
  4. Quick Check:

    3 + 4 = 7 [OK]
Hint: Add returns sum; printed result is sum [OK]
Common Mistakes:
  • Concatenating numbers as strings
  • Confusing return with print
  • Expecting void method to return value
4. Identify the error in this C# method:
public int Multiply(int x, int y) {
  int product = x * y;
}
medium
A. Incorrect method name.
B. Variables cannot be declared inside methods.
C. Missing return statement for non-void method.
D. Method parameters must be strings.

Solution

  1. Step 1: Check method return type and body

    The method declares int return type but does not return any value.
  2. Step 2: Identify missing return statement

    Since the method is not void, it must have a return statement returning an integer value like return product;.
  3. Final Answer:

    Missing return statement for non-void method. -> Option C
  4. Quick Check:

    Non-void methods must return a value [OK]
Hint: Non-void methods must always return a value [OK]
Common Mistakes:
  • Forgetting to return a value in non-void methods
  • Thinking variable declaration is invalid inside methods
  • Assuming parameters must be strings
5. You want to create a method that prints a greeting message but does not return any value. Which method signature and body is correct in C#?
hard
A. public string Greet() { Console.WriteLine("Hello!"); return "Hello!"; }
B. public void Greet() { Console.WriteLine("Hello!"); }
C. public void Greet() { return "Hello!"; }
D. public string Greet() { Console.WriteLine("Hello!"); }

Solution

  1. Step 1: Understand method purpose and return type

    The method should print a message and not return any value, so it should be void.
  2. Step 2: Check each option for correctness

    public void Greet() { Console.WriteLine("Hello!"); } correctly uses void and prints the message without returning anything. public string Greet() { Console.WriteLine("Hello!"); return "Hello!"; } returns a string, which is not needed. public void Greet() { return "Hello!"; } tries to return a string in a void method (invalid). public string Greet() { Console.WriteLine("Hello!"); } declares string return type but does not return anything (invalid).
  3. Final Answer:

    public void Greet() { Console.WriteLine("Hello!"); } -> Option B
  4. Quick Check:

    Void method prints only, no return [OK]
Hint: Void methods do actions, no return needed [OK]
Common Mistakes:
  • Returning a value from a void method
  • Declaring return type but not returning
  • Confusing printing with returning
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