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

Protected access modifier in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Protected Access Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of accessing protected member in derived class
What is the output of this C# code when run?
C Sharp (C#)
class Animal {
    protected string sound = "Roar";
}

class Lion : Animal {
    public void MakeSound() {
        System.Console.WriteLine(sound);
    }
}

class Program {
    static void Main() {
        Lion lion = new Lion();
        lion.MakeSound();
    }
}
ARoar
BCompilation error: 'sound' is inaccessible due to its protection level
CRuntime error: NullReferenceException
DNo output
Attempts:
2 left
💡 Hint
Remember that protected members are accessible in derived classes.
Predict Output
intermediate
2:00remaining
Accessing protected member from outside derived class
What happens when this C# code is compiled?
C Sharp (C#)
class Vehicle {
    protected int speed = 100;
}

class Car : Vehicle {
}

class Program {
    static void Main() {
        Vehicle v = new Vehicle();
        System.Console.WriteLine(v.speed);
    }
}
ACompilation error: 'speed' is inaccessible due to its protection level
BNo output
C100
DRuntime error: NullReferenceException
Attempts:
2 left
💡 Hint
Protected members are not accessible from outside the class or its derived classes.
🔧 Debug
advanced
2:00remaining
Why does this code cause a compilation error?
This code tries to access a protected member from an unrelated class. What is the cause of the compilation error?
C Sharp (C#)
class Parent {
    protected int number = 42;
}

class Child : Parent {
}

class Stranger {
    void ShowNumber() {
        Parent p = new Parent();
        System.Console.WriteLine(p.number);
    }
}
AThe 'number' field is private, so it cannot be accessed outside Parent.
BThe 'Parent' class must be declared as public to access its members.
CProtected members can only be accessed within the class or its derived classes, not from unrelated classes.
DThe 'ShowNumber' method is not static, causing the error.
Attempts:
2 left
💡 Hint
Think about who can access protected members.
Predict Output
advanced
2:00remaining
Output when accessing protected member via derived class reference
What is the output of this C# program?
C Sharp (C#)
class Base {
    protected string message = "Hello from Base";
}

class Derived : Base {
    public void PrintMessage() {
        System.Console.WriteLine(message);
    }
}

class Program {
    static void Main() {
        Base b = new Derived();
        ((Derived)b).PrintMessage();
    }
}
ACompilation error: Cannot convert Base to Derived
BHello from Base
CRuntime error: InvalidCastException
DNo output
Attempts:
2 left
💡 Hint
Casting to the derived type allows access to its methods.
🧠 Conceptual
expert
3:00remaining
Understanding protected access in nested classes
Consider this C# code. What is the output when running Main?
C Sharp (C#)
class Outer {
    protected int value = 10;
    public class Inner : Outer {
        public void ShowValue() {
            System.Console.WriteLine(value);
        }
    }
}

class Program {
    static void Main() {
        Outer.Inner inner = new Outer.Inner();
        inner.ShowValue();
    }
}
ANo output
BCompilation error: 'value' is inaccessible due to its protection level
CRuntime error: NullReferenceException
D10
Attempts:
2 left
💡 Hint
Nested classes can inherit and access protected members of the outer class.