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

Why encapsulation matters in C Sharp (C#) - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Encapsulation Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of accessing private field directly
What will be the output of this C# code when trying to access a private field directly?
C Sharp (C#)
using System;
class BankAccount {
    private decimal balance = 1000m;
}

class Program {
    static void Main() {
        BankAccount account = new BankAccount();
        Console.WriteLine(account.balance);
        Console.WriteLine("Accessing balance directly");
    }
}
A1000
B0
CCompilation error: 'BankAccount.balance' is inaccessible due to its protection level
DRuntime error: NullReferenceException
Attempts:
2 left
💡 Hint
Private fields cannot be accessed outside their class.
Predict Output
intermediate
2:00remaining
Output of using public property to access private field
What will be the output of this C# code that uses a public property to access a private field?
C Sharp (C#)
using System;
class BankAccount {
    private decimal balance = 1000m;
    public decimal Balance {
        get { return balance; }
    }
}

class Program {
    static void Main() {
        BankAccount account = new BankAccount();
        Console.WriteLine(account.Balance);
    }
}
ACompilation error: Cannot access private field
B1000
C0
DRuntime error: NullReferenceException
Attempts:
2 left
💡 Hint
Public properties can expose private fields safely.
🧠 Conceptual
advanced
2:00remaining
Why encapsulation improves code safety
Which of the following best explains why encapsulation improves code safety in object-oriented programming?
AIt hides internal data and only exposes controlled access, preventing unintended changes.
BIt forces the program to run faster by removing all checks on data.
CIt makes all data public so any part of the program can modify it freely.
DIt allows direct access to all variables to simplify debugging.
Attempts:
2 left
💡 Hint
Think about how hiding details helps protect data.
🔧 Debug
advanced
2:00remaining
Identify the encapsulation violation
Which option shows a violation of encapsulation principles in C#?
C Sharp (C#)
class Person {
    public string name;
    private int age;
}
AMaking 'name' public allows external code to change it directly.
BMaking 'age' private hides it from external access.
CUsing a private field for 'age' protects it from unintended changes.
DUsing properties to access 'age' is a violation of encapsulation.
Attempts:
2 left
💡 Hint
Encapsulation means hiding data from outside code.
🚀 Application
expert
2:00remaining
Result of modifying private field via method
What will be the output of this C# program that modifies a private field using a public method?
C Sharp (C#)
using System;
class Counter {
    private int count = 0;
    public void Increment() {
        count++;
    }
    public int GetCount() {
        return count;
    }
}

class Program {
    static void Main() {
        Counter c = new Counter();
        c.Increment();
        c.Increment();
        Console.WriteLine(c.GetCount());
    }
}
A0
BRuntime error: NullReferenceException
CCompilation error: Cannot access private field
D2
Attempts:
2 left
💡 Hint
Methods can safely modify private data inside the class.