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

Access modifiers (public, private, internal) 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 make the field accessible from anywhere.

C Sharp (C#)
class MyClass {
    [1] int number;
}
Drag options to blanks, or click blank then click option'
Aprivate
Bprotected
Cpublic
Dinternal
Attempts:
3 left
💡 Hint
Common Mistakes
Using private makes the member accessible only inside the class.
Internal restricts access to the same assembly.
2fill in blank
medium

Complete the code to restrict access to the method only within the same assembly.

C Sharp (C#)
class Calculator {
    [1] int Add(int a, int b) {
        return a + b;
    }
}
Drag options to blanks, or click blank then click option'
Ainternal
Bpublic
Cprivate
Dprotected
Attempts:
3 left
💡 Hint
Common Mistakes
Using private restricts access only to the class itself.
Public allows access from anywhere, which is too broad here.
3fill in blank
hard

Complete the code to make the field accessible only inside the class.

C Sharp (C#)
class Person {
    [1] string name;

    public Person(string name) {
        this.name = name;
    }
}
Drag options to blanks, or click blank then click option'
Aprivate
Bpublic
Cinternal
Dprotected
Attempts:
3 left
💡 Hint
Common Mistakes
Using public exposes the field to all classes.
Internal allows access from the same assembly, which is broader than needed.
4fill in blank
hard

Fill in the blank to declare a method accessible only within the class and its derived classes, but not outside.

C Sharp (C#)
using System;

class Animal {
    [1] void Speak() {
        Console.WriteLine("Animal sound");
    }
}

class Dog : Animal {
    public void Bark() {
        Speak();
    }
}
Drag options to blanks, or click blank then click option'
Aprivate
Bprotected
Cpublic
Dinternal
Attempts:
3 left
💡 Hint
Common Mistakes
Using private restricts access only to the class itself.
Public allows access from anywhere, which is too broad.
5fill in blank
hard

Fill all three blanks to create a class with a private field, an internal method, and a public method.

C Sharp (C#)
class BankAccount {
    [1] decimal balance;

    [2] void UpdateBalance(decimal amount) {
        balance += amount;
    }

    [3] decimal GetBalance() {
        return balance;
    }
}
Drag options to blanks, or click blank then click option'
Aprivate
Binternal
Cpublic
Dprotected
Attempts:
3 left
💡 Hint
Common Mistakes
Making balance public exposes it directly.
Using private for UpdateBalance restricts it too much.
Using internal for GetBalance limits access unnecessarily.