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

Ref and out parameters in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ref and Out Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of method using ref parameter
What is the output of the following C# code?
C Sharp (C#)
using System;
class Program {
    static void Increment(ref int x) {
        x += 5;
    }
    static void Main() {
        int a = 10;
        Increment(ref a);
        Console.WriteLine(a);
    }
}
A10
B15
C5
DCompilation error
Attempts:
2 left
💡 Hint
Remember that ref passes the variable by reference, so changes inside the method affect the original variable.
Predict Output
intermediate
2:00remaining
Output of method using out parameter
What will be printed when this C# code runs?
C Sharp (C#)
using System;
class Program {
    static void GetValues(out int x, out int y) {
        x = 3;
        y = 7;
    }
    static void Main() {
        int a, b;
        GetValues(out a, out b);
        Console.WriteLine(a + b);
    }
}
A0
BCompilation error
C10
D7
Attempts:
2 left
💡 Hint
Out parameters must be assigned inside the method before use.
Predict Output
advanced
2:00remaining
Behavior of ref vs out with uninitialized variables
What happens when you try to compile and run this C# code?
C Sharp (C#)
using System;
class Program {
    static void Modify(ref int x) {
        x = 10;
    }
    static void ModifyOut(out int y) {
        y = 20;
    }
    static void Main() {
        int a;
        //Modify(ref a); // Line 1
        ModifyOut(out a); // Line 2
        Console.WriteLine(a);
    }
}
APrints 20
BCompilation error at Line 2 because 'a' is uninitialized
CCompilation error at Line 1 because 'a' is uninitialized
DPrints 10
Attempts:
2 left
💡 Hint
ref requires the variable to be initialized before passing; out does not.
🧠 Conceptual
advanced
2:00remaining
Difference between ref and out parameters
Which statement correctly describes the difference between ref and out parameters in C#?
Aref parameters must be initialized before passing; out parameters do not need to be initialized but must be assigned inside the method.
Bout parameters must be initialized before passing; ref parameters do not need to be initialized.
CBoth ref and out parameters must be initialized before passing and assigned inside the method.
Dref parameters cannot be modified inside the method; out parameters can be modified.
Attempts:
2 left
💡 Hint
Think about initialization requirements and assignment inside the method.
🔧 Debug
expert
2:00remaining
Identify the error in this code using ref and out
What error will this C# code produce when compiled?
C Sharp (C#)
using System;
class Program {
    static void Process(ref int x, out int y) {
        x += 5;
        // y is not assigned
    }
    static void Main() {
        int a = 1, b;
        Process(ref a, out b);
        Console.WriteLine(a + b);
    }
}
ANo error, prints 6
BRuntime error: b is used before assignment
CCompilation error: ref parameter 'x' must be initialized before passing
DCompilation error: out parameter 'y' must be assigned before method returns
Attempts:
2 left
💡 Hint
Out parameters must be assigned a value inside the method before returning.