Back to Practice OOP with C++

Encapsulation & Abstraction - Practice MCQs for CCAT

50 Questions Section B: Programming OOP with C++

Encapsulation & Abstraction Question Bank for C-CAT

Topic-wise Encapsulation & Abstraction MCQs for CDAC C-CAT preparation with answers and explanations.

Q1.
Encapsulation means:
AInheritance
BBundling data and methods together
CMultiple classes
DFunction calls
Show Answer & Explanation

Correct Answer: B - Bundling data and methods together

Encapsulation wraps data and methods that operate on data within a class.

Q2.
Data hiding is achieved by:
AMaking members public
BMaking members private
CUsing inheritance
DUsing pointers
Show Answer & Explanation

Correct Answer: B - Making members private

Private members hide data from outside access.

Q3.
Getter and setter methods are used for:
ADirect access
BDeleting data
CControlled access to private data
DCopying objects
Show Answer & Explanation

Correct Answer: C - Controlled access to private data

Getters/setters provide controlled access to private members.

Q4.
Abstraction means:
AShowing implementation
BOperator overloading
CMultiple inheritance
DHiding implementation details
Show Answer & Explanation

Correct Answer: D - Hiding implementation details

Abstraction hides complex implementation and shows only necessary features.

Q5.
Which access specifier provides most restriction?
Aprivate
Bprotected
Cpublic
Dfriend
Show Answer & Explanation

Correct Answer: A - private

Private is most restrictive - accessible only within the class.

Q6.
Protected members are accessible in:
AOnly same class
BSame class and derived classes
CEverywhere
DOnly derived class
Show Answer & Explanation

Correct Answer: B - Same class and derived classes

Protected members are accessible in the class and its derived classes.

Q7.
Which is NOT a pillar of OOP?
AEncapsulation
BInheritance
CCompilation
DPolymorphism
Show Answer & Explanation

Correct Answer: C - Compilation

Four pillars of OOP: Encapsulation, Abstraction, Inheritance, Polymorphism.

Q8.
Interface in C++ is achieved using:
Ainterface keyword
BUnion
CStruct
DAbstract class with pure virtual functions
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.

Q9.
Benefit of encapsulation:
AFaster execution
BData protection and modularity
CLess memory
DNo benefit
Show Answer & Explanation

Correct Answer: B - Data protection and modularity

Encapsulation protects data and provides modularity.

Q10.
const member function:
ACan modify members
BCannot modify non-static members
CMust be static
DCannot be called
Show Answer & Explanation

Correct Answer: B - Cannot modify non-static members

const member function cannot modify non-static data members.

Q11.
What is data abstraction?
ADeleting data
BCopying data
CHiding implementation details
DSorting data
Show Answer & Explanation

Correct Answer: C - Hiding implementation details

Data abstraction hides implementation details and shows only functionality to users.

Q12.
Pure virtual function is declared using:
Apure keyword
Bvirtual only
Cabstract keyword
D= 0 syntax
Show Answer & Explanation

Correct Answer: D - = 0 syntax

Pure virtual function is declared with = 0: virtual void func() = 0;

Q13.
Abstract class can have:
ABoth pure virtual and regular functions
BOnly pure virtual functions
CNo functions
DOnly static functions
Show Answer & Explanation

Correct Answer: A - Both pure virtual and regular functions

Abstract class can have both pure virtual functions and regular member functions.

Q14.
Getter methods are used to:
ARead private data safely
BModify private data
CDelete data
DCreate objects
Show Answer & Explanation

Correct Answer: A - Read private data safely

Getter (accessor) methods provide controlled read access to private data members.

Q15.
Setter methods are used to:
ARead data
BDelete objects
CPrint data
DModify private data with validation
Show Answer & Explanation

Correct Answer: D - Modify private data with validation

Setter (mutator) methods provide controlled write access with optional validation.

Q16.
Loose coupling means:
AClasses are tightly dependent
BSingle class design
CNo classes
DClasses have minimal dependencies
Show Answer & Explanation

Correct Answer: D - Classes have minimal dependencies

Loose coupling means classes have minimal dependencies on each other, improving maintainability.

Q17.
Information hiding is achieved through:
APublic members
BPrivate members and public interfaces
CGlobal variables
DFriend functions only
Show Answer & Explanation

Correct Answer: B - Private members and public interfaces

Information hiding uses private members accessed through public methods (getters/setters).

Q18.
Which provides better abstraction?
AConcrete class
BAbstract class or interface
CGlobal functions
DStruct
Show Answer & Explanation

Correct Answer: B - Abstract class or interface

Abstract classes and interfaces provide better abstraction by defining contracts without implementation.

Q19.
Advantage of encapsulation:
AFaster execution
BMore global access
CLess memory
DEasier maintenance and data integrity
Show Answer & Explanation

Correct Answer: D - Easier maintenance and data integrity

Encapsulation provides easier maintenance, data integrity, and protection from misuse.

Q20.
Header files in C++ promote:
ATight coupling
BInterface abstraction and separate compilation
CSlower compilation
DData exposure
Show Answer & Explanation

Correct Answer: B - Interface abstraction and separate compilation

Header files separate interface from implementation, promoting abstraction and modularity.

Q21.
What is encapsulation in OOP?
ABundling data and methods that operate on data within a single unit
BInheriting properties from a parent class
CCreating multiple forms of a function
DConverting one type to another
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.

Q22.
What is data hiding?
AEncrypting class data
BRestricting access to class members using access specifiers
CDeleting data after use
DCompressing data in memory
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.

Q23.
What is a friend function in C++?
AA member function of the class
BA non-member function that can access private and protected members of a class
CA virtual function
DA constructor of another class
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.

Q24.

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);
}
A0
BCompilation error
C10
DGarbage value
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.

Q25.
What is a friend class in C++?
AA class that inherits from another class
BA class whose all member functions can access private members of another class
CA class that is declared inside another class
DA template class
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.

Q26.
Is friendship symmetric in C++?
AYes, if A is friend of B, B is friend of A
BOnly when both classes declare friendship
COnly for public classes
DNo, friendship is not symmetric
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.

Q27.
Is friendship transitive in C++?
ANo, friendship is not transitive
BYes, friend of a friend is also a friend
COnly for inherited classes
DOnly for template classes
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.

Q28.
What is the purpose of getter and setter methods?
ATo provide controlled access to private data members
BTo make all members public
CTo create new objects
DTo delete objects
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.

Q29.

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();
}
A-500
B0
C1000
D500
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.

Q30.
Which of the following best describes the relationship between encapsulation and abstraction?
AThey are the same concept
BAbstraction is a type of encapsulation
CEncapsulation hides implementation; abstraction hides complexity
DEncapsulation requires inheritance
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.

Q31.

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);
}
AGarbage value
B0
CCompilation error
D5
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.

Q32.
Can a friend function be a member of another class?
AYes, a member function of one class can be a friend of another class
BNo, friend functions must be standalone
COnly static member functions can be friends
DOnly virtual functions can be friends
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&).

Q33.
What is the advantage of making data members private?
AFaster execution
BLess memory usage
CData integrity and controlled access
DAutomatic initialization
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.

Q34.

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();
}
AGarbage values
BCompilation error
CJohn 20
DEmpty output
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'.

Q35.
Why are getters typically declared as const member functions?
ATo improve performance
BConst is mandatory for getters
CTo make them virtual
DTo allow calling on const objects and guarantee no modification
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.

Q36.

What will happen if you access a private member without a getter?

class Test {
  int x = 42;
};
int main() {
  Test t;
  cout << t.x;
}
ACompilation error
B0
C42
DRuntime error
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'.

Q37.
What is the principle of least privilege in the context of encapsulation?
AGive all members public access
BUse only private inheritance
CGive each component only the minimum access needed
DAvoid using constructors
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.

Q38.

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();
}
A0
BGarbage value
CCompilation error
D200
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.

Q39.
What is a nested class in C++?
AA class that inherits from another class
BA class with only private members
CA class defined inside another class
DA class with no constructors
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.

Q40.
Which of the following breaks encapsulation?
AMaking all data members public
BUsing getters and setters
CUsing private access specifier
DUsing friend functions judiciously
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.

Q41.

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();
}
A2
B1
C0
D3
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.

Q42.
Can a friend function access private members of a class without being a member?
ANo, only member functions can access private members
BOnly if declared static
CYes, that is the purpose of friend functions
DOnly if declared virtual
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.

Q43.
What is the difference between struct and class in terms of encapsulation?
AStructs have no encapsulation; classes do
BStructs cannot have member functions
CThere is no difference
DStructs default to public; classes default to private
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.

Q44.

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();
}
A212
B100
C180
D373
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.

Q45.
What is an accessor method?
AA method that modifies data members
BA method that reads and returns the value of a private data member
CA method that creates objects
DA method that destroys objects
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.

Q46.

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);
}
A1
B0
CCompilation error
D5
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).

Q47.
When should you use friend functions instead of member functions?
AAlways, for better performance
BOnly for static functions
CNever, they break encapsulation completely
DWhen a function needs to access private members of two or more classes
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.

Q48.
What does the 'explicit' keyword do for constructors?
APrevents implicit type conversions using the constructor
BMakes the constructor public
CMakes the constructor virtual
DAllows multiple inheritance
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.

Q49.

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();
}
A0 Error
B1 Warning
C2 Error
D2 Warning
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'.

Q50.
Which of the following is NOT a benefit of encapsulation?
AData protection
BFaster program execution
CCode maintenance
DReduced complexity
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.

Showing 1-10 of 50 questions