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

Access modifiers (public, private, internal) in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Access Modifiers 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 code with access modifiers?

Consider the following C# code. What will be printed when Main runs?

C Sharp (C#)
class MyClass {
    private int secret = 42;
    public int GetSecret() {
        return secret;
    }
}

class Program {
    static void Main() {
        MyClass obj = new MyClass();
        System.Console.WriteLine(obj.GetSecret());
    }
}
ACompilation error due to private field access
B0
C42
DRuntime error
Attempts:
2 left
💡 Hint

Remember that private fields can be accessed inside the same class.

Predict Output
intermediate
2:00remaining
What happens when accessing an internal member from another assembly?

Assume ClassA is in Assembly1 and ClassB is in Assembly2. ClassA has an internal method InternalMethod(). What happens if ClassB tries to call InternalMethod()?

C Sharp (C#)
public class ClassA {
    internal void InternalMethod() {
        System.Console.WriteLine("Hello from internal method");
    }
}

public class ClassB {
    public void Call() {
        ClassA a = new ClassA();
        a.InternalMethod();
    }
}
ACompilation error: 'InternalMethod' is inaccessible due to its protection level
BPrints: Hello from internal method
CRuntime error: Method not found
DSilent failure, no output
Attempts:
2 left
💡 Hint

Internal members are accessible only within the same assembly.

🔧 Debug
advanced
2:00remaining
Why does this code cause a compilation error?

Look at the code below. Why does it fail to compile?

C Sharp (C#)
class Example {
    private int value = 10;
}

class Derived : Example {
    void Show() {
        System.Console.WriteLine(value);
    }
}
ABecause 'Show' method is missing access modifier
BBecause 'Derived' class must be declared public
CBecause 'value' is not initialized
DBecause 'value' is private and not accessible in the derived class
Attempts:
2 left
💡 Hint

Think about how private members behave with inheritance.

🧠 Conceptual
advanced
2:00remaining
Which access modifier allows access only within the same assembly and derived classes?

Choose the correct access modifier that allows a member to be accessed within the same assembly and also by derived classes outside the assembly.

Aprotected internal
Bprivate
Cinternal
Dpublic
Attempts:
2 left
💡 Hint

Think about combining 'protected' and 'internal' access.

Predict Output
expert
3:00remaining
What is the output of this code with nested classes and access modifiers?

Analyze the code and determine what will be printed when Main runs.

C Sharp (C#)
public class Outer {
    private int x = 5;
    public class Inner {
        public void Print(Outer o) {
            System.Console.WriteLine(o.x);
        }
    }
}

class Program {
    static void Main() {
        Outer outer = new Outer();
        Outer.Inner inner = new Outer.Inner();
        inner.Print(outer);
    }
}
ARuntime error
B5
C0
DCompilation error: 'x' is inaccessible
Attempts:
2 left
💡 Hint

Remember that nested classes can access private members of the outer class instance.