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

Protected access modifier in C Sharp (C#) - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a protected method named ShowMessage.

C Sharp (C#)
protected void [1]() {
    Console.WriteLine("Hello from protected method!");
}
Drag options to blanks, or click blank then click option'
ADisplay
BShowMessage
CPrintMessage
DMessageShow
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different method name than declared.
Confusing method name with access modifier.
2fill in blank
medium

Complete the code to allow the derived class to access the base class's protected method.

C Sharp (C#)
class BaseClass {
    protected void [1]() {
        Console.WriteLine("Base class method");
    }
}

class DerivedClass : BaseClass {
    public void CallBase() {
        [1]();
    }
}
Drag options to blanks, or click blank then click option'
ABaseMethod
BDisplay
CShow
DProtectedMethod
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to call a method not declared in the base class.
Using a public method name instead of the protected one.
3fill in blank
hard

Fix the error in accessing the protected member from outside the class hierarchy.

C Sharp (C#)
class MyClass {
    protected int [1] = 10;
}

class OtherClass {
    public void Access() {
        MyClass obj = new MyClass();
        Console.WriteLine(obj.[1]);
    }
}
Drag options to blanks, or click blank then click option'
Avalue
Bnumber
Cdata
Dsecret
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to access protected members from unrelated classes.
Confusing protected with public access.
4fill in blank
hard

Fill both blanks to correctly override a protected method in the derived class.

C Sharp (C#)
class Parent {
    protected virtual void [1]() {
        Console.WriteLine("Parent method");
    }
}

class Child : Parent {
    protected override void [2]() {
        Console.WriteLine("Child method");
    }
}
Drag options to blanks, or click blank then click option'
ADisplay
BShow
CExecute
DRun
Attempts:
3 left
💡 Hint
Common Mistakes
Using different method names in parent and child classes.
Forgetting the override keyword.
5fill in blank
hard

Fill all three blanks to create a protected property with a getter and setter in a class.

C Sharp (C#)
class Sample {
    protected [1] [2] { get; [3]; }
}
Drag options to blanks, or click blank then click option'
Aint
BValue
Cset
Dget
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong data type or property name.
Omitting the setter keyword.