Encapsulation & Abstraction Question Bank for C-CAT
Topic-wise Encapsulation & Abstraction MCQs for CDAC C-CAT preparation with answers and explanations.
Show Answer & Explanation
Correct Answer: B - Bundling data and methods together
Encapsulation wraps data and methods that operate on data within a class.
Show Answer & Explanation
Correct Answer: B - Making members private
Private members hide data from outside access.
Show Answer & Explanation
Correct Answer: C - Controlled access to private data
Getters/setters provide controlled access to private members.
Show Answer & Explanation
Correct Answer: D - Hiding implementation details
Abstraction hides complex implementation and shows only necessary features.
Show Answer & Explanation
Correct Answer: A - private
Private is most restrictive - accessible only within the class.
Show Answer & Explanation
Correct Answer: B - Same class and derived classes
Protected members are accessible in the class and its derived classes.
Show Answer & Explanation
Correct Answer: C - Compilation
Four pillars of OOP: Encapsulation, Abstraction, Inheritance, Polymorphism.
Show Answer & Explanation
Correct Answer: D - Abstract class with pure virtual functions
C++ doesn't have interface keyword; uses abstract classes with pure virtual functions.
Show Answer & Explanation
Correct Answer: B - Data protection and modularity
Encapsulation protects data and provides modularity.
Show Answer & Explanation
Correct Answer: B - Cannot modify non-static members
const member function cannot modify non-static data members.
Show Answer & Explanation
Correct Answer: C - Hiding implementation details
Data abstraction hides implementation details and shows only functionality to users.
Show Answer & Explanation
Correct Answer: D - = 0 syntax
Pure virtual function is declared with = 0: virtual void func() = 0;
Show Answer & Explanation
Correct Answer: A - Both pure virtual and regular functions
Abstract class can have both pure virtual functions and regular member functions.
Show Answer & Explanation
Correct Answer: A - Read private data safely
Getter (accessor) methods provide controlled read access to private data members.
Show Answer & Explanation
Correct Answer: D - Modify private data with validation
Setter (mutator) methods provide controlled write access with optional validation.
Show Answer & Explanation
Correct Answer: D - Classes have minimal dependencies
Loose coupling means classes have minimal dependencies on each other, improving maintainability.
Show Answer & Explanation
Correct Answer: B - Private members and public interfaces
Information hiding uses private members accessed through public methods (getters/setters).
Show Answer & Explanation
Correct Answer: B - Abstract class or interface
Abstract classes and interfaces provide better abstraction by defining contracts without implementation.
Show Answer & Explanation
Correct Answer: D - Easier maintenance and data integrity
Encapsulation provides easier maintenance, data integrity, and protection from misuse.
Show Answer & Explanation
Correct Answer: B - Interface abstraction and separate compilation
Header files separate interface from implementation, promoting abstraction and modularity.
Show Answer & Explanation
Correct Answer: A - Bundling data and methods that operate on data within a single unit
Encapsulation is the mechanism of bundling data (variables) and the methods (functions) that operate on that data within a single unit (class), restricting direct access to some components.
Show Answer & Explanation
Correct Answer: B - Restricting access to class members using access specifiers
Data hiding is the practice of restricting access to the internal state of an object using access specifiers (private/protected), ensuring that data can only be accessed through defined interfaces.
Show Answer & Explanation
Correct Answer: B - A non-member function that can access private and protected members of a class
A friend function is declared using the 'friend' keyword inside a class. Though not a member, it can access the class's private and protected members.
What will be the output?
class Box {
int width;
public:
Box(int w) : width(w) {}
friend void showWidth(Box b);
};
void showWidth(Box b) {
cout << b.width;
}
int main() {
Box box(10);
showWidth(box);
}Show Answer & Explanation
Correct Answer: C - 10
showWidth is a friend function of Box and can access its private member width. It prints the value 10.
Show Answer & Explanation
Correct Answer: B - A class whose all member functions can access private members of another class
When a class is declared as a friend of another class, all member functions of the friend class can access private and protected members of the other class.
Show Answer & Explanation
Correct Answer: D - No, friendship is not symmetric
Friendship is not symmetric. If class A declares class B as a friend, B can access A's private members, but A cannot access B's private members unless B also declares A as a friend.
Show Answer & Explanation
Correct Answer: A - No, friendship is not transitive
Friendship is not transitive. If A is a friend of B and B is a friend of C, A is NOT automatically a friend of C. Each friendship must be explicitly declared.
Show Answer & Explanation
Correct Answer: A - To provide controlled access to private data members
Getter (accessor) and setter (mutator) methods provide controlled access to private data members, allowing validation and maintaining encapsulation while providing an interface for data access.
What will be the output?
class Account {
double balance;
public:
Account(double b) : balance(b) {}
void setBalance(double b) {
if (b >= 0) balance = b;
}
double getBalance() { return balance; }
};
int main() {
Account acc(1000);
acc.setBalance(-500);
cout << acc.getBalance();
}Show Answer & Explanation
Correct Answer: C - 1000
The setter validates that the balance must be >= 0. Since -500 is negative, the balance is not changed and remains 1000. This demonstrates encapsulation with validation.
Show Answer & Explanation
Correct Answer: C - Encapsulation hides implementation; abstraction hides complexity
Encapsulation hides internal implementation details by bundling data and methods together. Abstraction hides complexity by showing only essential features. They complement each other.
What will be the output?
class A {
int x = 5;
friend class B;
};
class B {
public:
void show(A a) { cout << a.x; }
};
int main() {
A a;
B b;
b.show(a);
}Show Answer & Explanation
Correct Answer: D - 5
Class B is a friend of class A, so B's member functions can access A's private members. b.show(a) prints a.x which is 5.
Show Answer & Explanation
Correct Answer: A - Yes, a member function of one class can be a friend of another class
A member function of one class can be declared as a friend of another class. This is called a friend member function: friend void B::show(A&).
Show Answer & Explanation
Correct Answer: C - Data integrity and controlled access
Making data members private ensures data integrity by preventing unauthorized or invalid modifications. Access is controlled through public methods that can validate input.
What will be the output?
class Student {
string name;
int age;
public:
Student(string n, int a) : name(n), age(a) {}
string getName() const { return name; }
int getAge() const { return age; }
};
int main() {
const Student s("John", 20);
cout << s.getName() << " " << s.getAge();
}Show Answer & Explanation
Correct Answer: C - John 20
The getter methods are declared as const, so they can be called on the const object s. The output is 'John 20'.
Show Answer & Explanation
Correct Answer: D - To allow calling on const objects and guarantee no modification
Declaring getters as const ensures they don't modify the object's state, allowing them to be called on const objects and providing stronger guarantees about the function's behavior.
What will happen if you access a private member without a getter?
class Test {
int x = 42;
};
int main() {
Test t;
cout << t.x;
}Show Answer & Explanation
Correct Answer: A - Compilation error
The member x is private (default access in class). Accessing it directly from outside the class causes a compilation error: 'x is a private member of Test'.
Show Answer & Explanation
Correct Answer: C - Give each component only the minimum access needed
The principle of least privilege states that each component should have only the minimum access necessary to perform its function. In OOP, this means using the most restrictive access specifier possible.
What will be the output?
class Engine {
int hp;
public:
Engine(int h) : hp(h) {}
int getHP() const { return hp; }
};
class Car {
Engine engine;
public:
Car(int hp) : engine(hp) {}
void showHP() { cout << engine.getHP(); }
};
int main() {
Car c(200);
c.showHP();
}Show Answer & Explanation
Correct Answer: D - 200
This demonstrates composition with encapsulation. Car contains an Engine object. The Engine's hp is private but accessible through getHP(). showHP() prints 200.
Show Answer & Explanation
Correct Answer: C - A class defined inside another class
A nested class is a class defined inside another class. The inner class has access to the outer class's members (including private), but the outer class does not automatically have access to the inner class's private members.
Show Answer & Explanation
Correct Answer: A - Making all data members public
Making all data members public breaks encapsulation because it allows unrestricted access to the internal state of an object, bypassing any validation or control mechanisms.
What will be the output?
class Counter {
int count;
public:
Counter() : count(0) {}
void increment() { count++; }
void decrement() { if (count > 0) count--; }
int getCount() const { return count; }
};
int main() {
Counter c;
c.increment();
c.increment();
c.increment();
c.decrement();
cout << c.getCount();
}Show Answer & Explanation
Correct Answer: A - 2
Three increments make count = 3, one decrement makes count = 2. The validation in decrement (count > 0) demonstrates encapsulation with controlled access.
Show Answer & Explanation
Correct Answer: C - Yes, that is the purpose of friend functions
The primary purpose of a friend function is to allow a non-member function to access private and protected members of a class. This is a controlled exception to encapsulation.
Show Answer & Explanation
Correct Answer: D - Structs default to public; classes default to private
The key difference is the default access specifier: struct defaults to public and class defaults to private. Both support full encapsulation features.
What will be the output?
class Temp {
int celsius;
public:
Temp(int c) : celsius(c) {}
void setCelsius(int c) {
if (c >= -273) celsius = c;
}
int getCelsius() const { return celsius; }
double toFahrenheit() const { return celsius * 9.0/5 + 32; }
};
int main() {
Temp t(100);
cout << t.toFahrenheit();
}Show Answer & Explanation
Correct Answer: A - 212
toFahrenheit() converts 100°C to Fahrenheit: 100 * 9/5 + 32 = 180 + 32 = 212°F. The celsius member is encapsulated with validation in the setter.
Show Answer & Explanation
Correct Answer: B - A method that reads and returns the value of a private data member
An accessor method (getter) is a public method that reads and returns the value of a private data member without modifying it. A mutator method (setter) is used for modification.
What will be the output?
class A {
int x;
public:
A(int v) : x(v) {}
friend bool isEqual(A a1, A a2);
};
bool isEqual(A a1, A a2) {
return a1.x == a2.x;
}
int main() {
A a(5), b(5);
cout << isEqual(a, b);
}Show Answer & Explanation
Correct Answer: A - 1
isEqual is a friend function that can access private member x. It compares a1.x (5) with a2.x (5), which are equal, so it returns true (1).
Show Answer & Explanation
Correct Answer: D - When a function needs to access private members of two or more classes
Friend functions are useful when an operation needs to access private members of two or more unrelated classes, like operator overloading for binary operators or comparison functions.
Show Answer & Explanation
Correct Answer: A - Prevents implicit type conversions using the constructor
The 'explicit' keyword prevents a constructor from being used for implicit type conversions. This helps maintain encapsulation by preventing unintended object creation.
What is the output?
class Logger {
static int logCount;
string message;
public:
Logger(string msg) : message(msg) { logCount++; }
static int getLogCount() { return logCount; }
string getMessage() const { return message; }
};
int Logger::logCount = 0;
int main() {
Logger l1("Error"), l2("Warning");
cout << Logger::getLogCount() << " " << l1.getMessage();
}Show Answer & Explanation
Correct Answer: C - 2 Error
Two Logger objects are created, incrementing logCount to 2. getLogCount() returns 2 and l1.getMessage() returns 'Error'. Output: '2 Error'.
Show Answer & Explanation
Correct Answer: B - Faster program execution
Encapsulation provides data protection, easier code maintenance, and reduced complexity. It does not inherently make programs execute faster; the overhead of function calls may even be slightly slower.