Encapsulation is the practice of bundling data and methods that operate on that data within one unit, like a class, to protect the data from outside interference and misuse.
Click to reveal answer
beginner
Define information hiding and its purpose.
Information hiding means restricting access to some parts of a system to reduce complexity and increase security by exposing only what is necessary.
Click to reveal answer
intermediate
How does encapsulation help in maintaining a system?
Encapsulation helps by keeping internal details hidden, so changes inside a component do not affect others, making the system easier to maintain and update.
Click to reveal answer
beginner
Give a real-life example of encapsulation.
A TV remote controls the TV without showing its internal circuits. The remote hides its internal details and only exposes buttons to the user.
Click to reveal answer
intermediate
What is the difference between encapsulation and information hiding?
Encapsulation is about grouping data and methods together, while information hiding is about restricting access to the internal details of that group.
Click to reveal answer
What does encapsulation primarily protect in a system?
ANetwork connections
BData and methods inside a component
CUser interface design
DDatabase schema
✗ Incorrect
Encapsulation bundles data and methods to protect them from outside interference.
Which of the following best describes information hiding?
AExposing all system details to users
BRemoving all comments from code
CRestricting access to internal details
DSharing data between all components
✗ Incorrect
Information hiding restricts access to internal details to reduce complexity and improve security.
Why is encapsulation important for system maintenance?
AIt hides internal changes from other parts
BIt makes the system slower
CIt exposes all data to users
DIt removes all errors automatically
✗ Incorrect
Encapsulation hides internal changes so other parts of the system are not affected.
Which real-life object is a good example of encapsulation?
AA transparent glass
BA public notice board
CAn open book
DA TV remote control
✗ Incorrect
A TV remote hides its internal circuits and only exposes buttons to the user.
What is the main goal of information hiding?
ATo reduce system complexity and improve security
BTo increase system complexity
CTo share all data openly
DTo make the system slower
✗ Incorrect
Information hiding reduces complexity and improves security by limiting access to internal details.
Explain in your own words what encapsulation means and why it is useful in system design.
Think about how a TV remote hides its internal parts but still works for you.
You got /4 concepts.
Describe the difference between encapsulation and information hiding with simple examples.
Focus on what each concept controls or hides.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of encapsulation in system design?
easy
A. To hide internal details and protect data from outside access
B. To make all data publicly accessible for easy modification
C. To increase the size of the system components
D. To allow direct access to all internal variables
Solution
Step 1: Understand encapsulation concept
Encapsulation means hiding the internal details of a system or component so that outside code cannot directly access or change them.
Step 2: Identify the main goal
The main goal is to protect data and control how it is accessed or modified, improving security and reducing errors.
Final Answer:
To hide internal details and protect data from outside access -> Option A
Quick Check:
Encapsulation = Hide details and protect data [OK]
Hint: Encapsulation means hiding details, not exposing them [OK]
Common Mistakes:
Thinking encapsulation means making data public
Confusing encapsulation with system size
Believing encapsulation allows direct variable access
2. Which of the following is the correct way to implement encapsulation in a class?
easy
A. Make all variables public and access them directly
B. Avoid using any methods to access data
C. Use private variables and provide public getter and setter methods
D. Use global variables for all data
Solution
Step 1: Recall encapsulation implementation
Encapsulation is done by making variables private so they cannot be accessed directly from outside the class.
Step 2: Use public methods for controlled access
Public getter and setter methods allow controlled reading and updating of private data.
Final Answer:
Use private variables and provide public getter and setter methods -> Option C
Quick Check:
Private variables + public methods = Encapsulation [OK]
Hint: Private variables + public getters/setters = encapsulation [OK]
Common Mistakes:
Making variables public defeats encapsulation
Using global variables breaks encapsulation
Not providing any access methods causes usability issues
3. Consider this code snippet in a system design context:
class Account {
private balance = 1000;
public getBalance() { return this.balance; }
public setBalance(amount) { if(amount >= 0) this.balance = amount; }
}
const acc = new Account();
acc.setBalance(-500);
console.log(acc.getBalance());
What will be the output?
medium
A. -500
B. 1000
C. 0
D. Error
Solution
Step 1: Analyze setBalance method behavior
The method only updates balance if the amount is greater than or equal to zero.
Step 2: Check the call with -500
Since -500 is less than zero, the balance is not updated and remains 1000.
Final Answer:
1000 -> Option B
Quick Check:
Negative amount ignored, balance stays 1000 [OK]
Hint: Setter blocks negative values, so balance stays unchanged [OK]
Common Mistakes:
Assuming balance changes to -500
Expecting zero as default
Thinking code throws an error
4. A developer wrote this code:
class User {
public name;
public setName(newName) { this.name = newName; }
}
const user = new User();
user.name = null;
console.log(user.name);
What is the problem related to encapsulation here?
medium
A. The name variable is public and can be set to invalid values directly
B. The setName method is missing
C. The class should not have any variables
D. The console.log statement is incorrect
Solution
Step 1: Identify variable access level
The variable name is public, so it can be changed directly without validation.
Step 2: Understand encapsulation violation
Direct access allows setting invalid values like null, bypassing any checks in setName.
Final Answer:
The name variable is public and can be set to invalid values directly -> Option A
Quick Check:
Public variable allows invalid direct access [OK]
Hint: Public variables can be changed without checks, breaking encapsulation [OK]
Common Mistakes:
Thinking setName method is missing
Believing variables should be removed
Blaming console.log for output
5. You are designing a banking system where account balance must never be negative. Which approach best uses encapsulation and information hiding to enforce this rule?
hard
A. Make balance public and rely on users to not set negative values
B. Store balance as a global variable accessible everywhere
C. Do not provide any methods to access balance
D. Make balance private and provide a setter that only updates if amount ≥ 0
Solution
Step 1: Protect balance data
Making balance private hides it from direct external changes, preventing accidental or malicious updates.
Step 2: Control updates via setter method
The setter method enforces the rule by allowing updates only if the new amount is zero or positive.
Step 3: Avoid unsafe alternatives
Public or global variables allow uncontrolled changes, risking negative balances and errors.
Final Answer:
Make balance private and provide a setter that only updates if amount ≥ 0 -> Option D