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

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1.

Which access modifier allows a class member to be accessed from any other code in the same project or assembly?

easy
A. internal
B. private
C. public
D. protected

Solution

  1. Step 1: Understand the meaning of internal

    The internal modifier allows access within the same assembly or project but not outside it.
  2. Step 2: Compare with other modifiers

    private restricts access to the same class only, public allows access from anywhere, and protected allows access in derived classes.
  3. Final Answer:

    internal -> Option A
  4. Quick Check:

    Access inside project = internal [OK]
Hint: Internal means accessible only within the same project [OK]
Common Mistakes:
  • Confusing internal with public
  • Thinking private allows access outside class
  • Mixing protected with internal
2.

Which of the following is the correct way to declare a private integer field named count in a class?

?
easy
A. internal count int;
B. int private count;
C. public int count;
D. private int count;

Solution

  1. Step 1: Recall correct syntax for access modifiers

    The access modifier comes first, then the type, then the variable name.
  2. Step 2: Check each option's order and keywords

    private int count; follows correct order: private int count;. Others have incorrect order or missing type.
  3. Final Answer:

    private int count; -> Option D
  4. Quick Check:

    Access modifier + type + name [OK]
Hint: Access modifier always comes before type in declaration [OK]
Common Mistakes:
  • Placing access modifier after type
  • Omitting the type
  • Using invalid order of keywords
3.

Consider the following code snippet:

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

MyClass obj = new MyClass();
Console.WriteLine(obj.secret);

What will happen when this code runs?

medium
A. Runtime error: NullReferenceException
B. It prints 42
C. Compilation error: 'secret' is inaccessible due to its protection level
D. It prints 0

Solution

  1. Step 1: Identify access modifier of 'secret'

    The field secret is declared private, so it cannot be accessed outside MyClass.
  2. Step 2: Check code accessing 'secret'

    The code tries to access obj.secret outside the class, which is not allowed and causes a compile-time error.
  3. Final Answer:

    Compilation error: 'secret' is inaccessible due to its protection level -> Option C
  4. Quick Check:

    Private fields cannot be accessed outside class [OK]
Hint: Private members cannot be accessed from outside their class [OK]
Common Mistakes:
  • Assuming private fields are accessible outside class
  • Confusing runtime errors with compile errors
  • Thinking public methods expose private fields directly
4.

Given this code snippet, identify the error and fix it:

class Sample {
    internal int value;
}

class Test {
    void Show() {
        Sample s = new Sample();
        Console.WriteLine(s.value);
    }
}

Assuming these classes are in different projects, what is the problem?

medium
A. No error, code runs fine
B. Error: 'value' is inaccessible due to protection level; fix by changing 'internal' to 'public'
C. Error: 'value' must be private; fix by changing 'internal' to 'private'
D. Error: 'value' must be static; fix by adding 'static' keyword

Solution

  1. Step 1: Understand 'internal' access modifier scope

    internal allows access only within the same project or assembly.
  2. Step 2: Check class locations

    Since Sample and Test are in different projects, Test cannot access internal members of Sample.
  3. Step 3: Fix the access level

    Changing value to public allows access from other projects.
  4. Final Answer:

    Error: 'value' is inaccessible due to protection level; fix by changing 'internal' to 'public' -> Option B
  5. Quick Check:

    Internal = same project only; public = accessible everywhere [OK]
Hint: Internal limits access to same project; use public for cross-project [OK]
Common Mistakes:
  • Assuming internal allows cross-project access
  • Changing internal to private incorrectly
  • Confusing static with access modifiers
5.

You have a class library project with a class Helper that has a method Calculate() marked as internal. You want to allow another project in the same solution to use Calculate() without making it public. What is the best way to achieve this?

hard
A. Use the InternalsVisibleTo attribute to expose internal members to the other project
B. Change the method to public
C. Change the method to private and create a public wrapper
D. Move the method to the other project

Solution

  1. Step 1: Understand internal and project boundaries

    internal restricts access to the same assembly, so other projects cannot access it by default.
  2. Step 2: Use InternalsVisibleTo attribute

    This attribute allows you to specify friend assemblies that can access internal members without making them public.
  3. Step 3: Evaluate other options

    Changing to public exposes to all, private hides too much, moving method is impractical.
  4. Final Answer:

    Use the InternalsVisibleTo attribute to expose internal members to the other project -> Option A
  5. Quick Check:

    InternalsVisibleTo grants internal access to specific projects [OK]
Hint: Use InternalsVisibleTo to share internal members across projects [OK]
Common Mistakes:
  • Making method public unnecessarily
  • Thinking private allows cross-project access
  • Moving code instead of using attributes