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
Recall & Review
beginner
What does the protected access modifier mean in C#?
The <code>protected</code> access modifier allows a class member to be accessible within its own class and by derived classes (subclasses), but not from other unrelated classes.
Click to reveal answer
beginner
Can a <code>protected</code> member be accessed from an instance of the class outside the class hierarchy?
No, <code>protected</code> members cannot be accessed from outside the class or its subclasses, even if you have an instance of the class.
Click to reveal answer
beginner
Example: What will happen if you try to access a protected field from a non-derived class?
You will get a compile-time error because <code>protected</code> members are not visible outside the class and its subclasses.
Click to reveal answer
beginner
How does protected differ from private and public?
<code>private</code> means only the class itself can access the member.<br><code>protected</code> means the class and its subclasses can access it.<br><code>public</code> means anyone can access it.
Click to reveal answer
intermediate
Why use protected instead of private?
Use protected when you want to hide members from the outside world but still allow subclasses to use or modify them. It helps with controlled inheritance.
Click to reveal answer
Which classes can access a protected member in C#?
AAny class anywhere
BAny class in the same project
COnly the class where it is declared and its subclasses
DOnly the class where it is declared
✗ Incorrect
protected members are accessible only within the declaring class and its derived classes.
Can a protected member be accessed by an instance of a derived class from outside the class?
AYes, always
BNo, never
COnly if the member is also <code>public</code>
DYes, but only inside the derived class code
✗ Incorrect
You can access protected members from within the derived class code, but not from outside using an instance.
What happens if you try to access a protected member from a non-derived class?
ACompile-time error
BIt works fine
CRuntime error
DIt depends on the project settings
✗ Incorrect
Accessing protected members outside the class hierarchy causes a compile-time error.
Which access modifier allows access from anywhere?
Apublic
Bprotected
Cinternal
Dprivate
✗ Incorrect
public members are accessible from any code.
Why might a developer choose protected over private?
ATo make members accessible to all classes
BTo allow subclasses to access members
CTo hide members completely
DTo allow access only within the same assembly
✗ Incorrect
protected allows subclasses to access members, unlike private which restricts access to the declaring class only.
Explain in your own words what the protected access modifier does and when you would use it.
Think about who can see the member and why inheritance matters.
You got /4 concepts.
Describe the difference between private, protected, and public access modifiers with simple examples.
Imagine who can open a door labeled private, protected, or public.
You got /4 concepts.
Practice
(1/5)
1.
What does the protected access modifier mean in C#?
easy
A. Only the class itself and its subclasses can access the member.
B. Any code in the same assembly can access the member.
C. Only code outside the class can access the member.
D. The member is accessible everywhere without restriction.
Solution
Step 1: Understand the meaning of protected
The protected modifier restricts access to the class itself and any classes that inherit from it.
Step 2: Compare with other access levels
Unlike public or internal, protected hides members from outside classes except subclasses.
Final Answer:
Only the class itself and its subclasses can access the member. -> Option A
Quick Check:
Protected = class + subclasses access [OK]
Hint: Protected means class and subclasses only [OK]
Common Mistakes:
Confusing protected with public
Thinking protected allows access from unrelated classes
Mixing protected with internal or private
2.
Which of the following is the correct way to declare a protected method named Calculate in C#?
?
easy
A. internal void Calculate() { }
B. private void Calculate() { }
C. public void Calculate() { }
D. protected void Calculate() { }
Solution
Step 1: Recall protected method syntax
In C#, the keyword protected is used before the return type to declare a protected method.
Step 2: Check each option
protected void Calculate() { } uses protected void Calculate(), which is correct syntax for a protected method.
Final Answer:
protected void Calculate() { } -> Option D
Quick Check:
Protected method syntax = protected + return type [OK]
Hint: Protected methods start with 'protected' keyword [OK]
Common Mistakes:
Using private or public instead of protected
Omitting the return type
Placing protected after the method name
3.
Consider the following code:
class Parent {
protected int number = 5;
}
class Child : Parent {
public int GetNumber() {
return number;
}
}
class Program {
static void Main() {
Child c = new Child();
Console.WriteLine(c.GetNumber());
}
}
What will be the output when this program runs?
medium
A. 0
B. 5
C. Compilation error
D. Runtime error
Solution
Step 1: Understand protected member access in subclass
The number field is protected, so the subclass Child can access it directly.
Step 2: Trace the program output
The GetNumber method returns number which is 5, so Console.WriteLine prints 5.
Final Answer:
5 -> Option B
Quick Check:
Protected field accessed in subclass = 5 [OK]
Hint: Protected members accessible in subclass methods [OK]
Common Mistakes:
Thinking protected means inaccessible outside parent
Expecting compilation error due to access
Confusing protected with private
4.
Identify the error in this code snippet:
class Base {
protected int value = 10;
}
class Other {
void Show() {
Base b = new Base();
Console.WriteLine(b.value);
}
}
medium
A. Class Other must inherit Base to access value.
B. Missing semicolon after declaration.
C. Cannot access protected member from unrelated class instance.
D. No error, code runs fine.
Solution
Step 1: Check access of protected member from unrelated class
The class Other does not inherit from Base, so it cannot access value which is protected.
Step 2: Understand protected access rules
Protected members are accessible only within the class and its subclasses, not from unrelated classes.
Final Answer:
Cannot access protected member from unrelated class instance. -> Option C
Quick Check:
Protected access = class + subclasses only [OK]
Hint: Protected not accessible from unrelated classes [OK]
Common Mistakes:
Assuming protected is like public
Thinking inheritance is not required
Ignoring access modifier rules
5.
You want to create a base class Vehicle with a protected field speed. You also want a subclass Car that can set and get this speed, but no other class should access it directly. Which code snippet correctly implements this?
hard
A. class Vehicle { protected int speed; }
class Car : Vehicle {
public void SetSpeed(int s) { speed = s; }
public int GetSpeed() { return speed; }
}
B. class Vehicle { public int speed; }
class Car : Vehicle { }
// Any class can access speed directly
C. class Vehicle { private int speed; }
class Car : Vehicle {
public int GetSpeed() { return speed; }
}
D. class Vehicle { internal int speed; }
class Car : Vehicle { }
// speed accessible only in same assembly
Solution
Step 1: Use protected field in base class
The field speed is declared protected in Vehicle so only Vehicle and subclasses can access it.
Step 2: Provide public methods in subclass to access speed
The subclass Car has public methods SetSpeed and GetSpeed to safely access the protected field.
Step 3: Check other options for access control
class Vehicle { public int speed; }
class Car : Vehicle { }
// Any class can access speed directly uses public field, which allows all classes to access speed directly. class Vehicle { private int speed; }
class Car : Vehicle {
public int GetSpeed() { return speed; }
} uses private field, so subclass cannot access it. class Vehicle { internal int speed; }
class Car : Vehicle { }
// speed accessible only in same assembly uses internal, which restricts access to assembly, not subclasses.
Final Answer:
class Vehicle { protected int speed; } class Car : Vehicle { public void SetSpeed(int s) { speed = s; } public int GetSpeed() { return speed; } } -> Option A
Quick Check:
Protected field + subclass methods = correct encapsulation [OK]
Hint: Protected field with public subclass methods controls access [OK]