Challenge - 5 Problems
Protected Access Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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(); } }
Attempts:
2 left
💡 Hint
Remember that protected members are accessible in derived classes.
✗ Incorrect
The protected member 'sound' is accessible inside the derived class Lion, so printing it works and outputs 'Roar'.
❓ Predict Output
intermediate2: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); } }
Attempts:
2 left
💡 Hint
Protected members are not accessible from outside the class or its derived classes.
✗ Incorrect
The 'speed' field is protected, so it cannot be accessed from an instance of Vehicle outside the class or derived classes, causing a compilation error.
🔧 Debug
advanced2: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); } }
Attempts:
2 left
💡 Hint
Think about who can access protected members.
✗ Incorrect
Protected members are accessible only inside the class and its subclasses. The 'Stranger' class is unrelated and cannot access 'number'.
❓ Predict Output
advanced2: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(); } }
Attempts:
2 left
💡 Hint
Casting to the derived type allows access to its methods.
✗ Incorrect
The object is actually a Derived instance, so casting to Derived and calling PrintMessage works and prints the protected member.
🧠 Conceptual
expert3: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(); } }
Attempts:
2 left
💡 Hint
Nested classes can inherit and access protected members of the outer class.
✗ Incorrect
Inner inherits from Outer and can access the protected member 'value', so it prints 10.