
This set focuses on how object oriented code protects its data. It covers encapsulation versus abstraction, C++ and Java access specifiers, package and protected access rules, getters and setters, immutability and common ways encapsulation leaks. The questions suit university semester exams, GATE and UGC NET preparation and the concept checks that interviewers use in placement rounds.
How to practise: read each question and tap the option you think is correct. You will see right away whether you got it, along with a short explanation, and your score is tracked at the bottom of the screen. This set has 18 questions.
Topics covered in this set
- Encapsulation and data hiding
- Abstraction vs encapsulation
- public, private and protected in C++
- Java access levels including package-private
- Access under public and private inheritance
- Getters, setters and immutability
- Defensive copies and leaking references
Object Oriented Programming MCQs with answers and explanations
Q1. Which definition best describes encapsulation?
a) Bundling data with the methods that operate on it in one unit and restricting direct outside access to that data
b) Deriving a new class from an existing class to reuse its code
c) Allowing one interface to be used for different underlying forms
d) Splitting a program into separately compiled files
Q2. Which statement correctly distinguishes abstraction from encapsulation?
a) Abstraction is achieved only with private fields, while encapsulation needs interfaces
b) They are identical concepts; the terms are interchangeable
c) Abstraction exposes only the essential behaviour of an entity, while encapsulation hides how its state is stored and protects that state
d) Encapsulation applies only to functions, while abstraction applies only to variables
Q3. In C++, what is the default access level of members declared without an access specifier?
a) public in a class and private in a struct
b) private in a class and public in a struct
c) protected in both class and struct
d) private in both class and struct
Q4. In Java, a field declared with no access modifier is accessible from:
a) Any class in any package
b) Only the declaring class
c) Subclasses in any package, but not other classes in the same package
d) Only classes in the same package
Q5. What is the result of compiling and running this Java program?
class Acc {
private int bal;
Acc(int b) { bal = b; }
boolean richerThan(Acc o) { return bal > o.bal; }
}
public class Main {
public static void main(String[] args) {
System.out.print(new Acc(50).richerThan(new Acc(80)));
}
}
a) Compilation error: bal has private access
b) true
c) false
d) IllegalAccessError at run time
Q6. What does this Java program print?
class Box {
private int[] d = {1, 2, 3};
int[] get() { return d; }
}
public class Main {
public static void main(String[] args) {
Box b = new Box();
b.get()[0] = 9;
System.out.print(b.get()[0]);
}
}
a) 1
b) 9
c) Compilation error
d) ArrayIndexOutOfBoundsException
Q7. What is printed by this C++ program?
#include <iostream>
using namespace std;
class Base {
protected: int p = 1;
private: int q = 2;
public: int r = 3;
};
class D : private Base {
public:
int sum() { return p + r; }
};
int main() { D d; cout << d.sum(); }
a) 6
b) 4
c) Compilation error: p is protected in Base
d) Compilation error: r becomes private
Q8. Under public inheritance in C++, a protected member of the base class becomes ___ in the derived class.
a) protected
b) public
c) private
d) inaccessible
Q9. Which ordering of Java access levels runs from most restrictive to least restrictive?
a) public, protected, default, private
b) private, protected, default, public
c) default, private, protected, public
d) private, default (package), protected, public
Q10. Which modifier is NOT allowed on a top-level (non-nested) Java class?
a) public
b) abstract
c) final
d) private
Q11. Why is a setter method usually preferred over making a field public?
a) Setters are always faster than direct field access
b) Public fields cannot be read by other classes
c) A setter can validate input and enforce the class invariants before changing the state
d) Setters make a class immutable
Q12. Which practice would break the immutability of a Java class?
a) Adding a public method that changes one of its fields after construction
b) Declaring the class final
c) Making all fields private and final
d) Returning a copy of an internal mutable list from a getter
Q13. Which member functions of D compile?
class B { protected: int v = 1; };
class D : public B {
public:
int f(B& other) { return other.v; }
int g(D& other) { return other.v; }
};
a) Both f and g
b) Only g
c) Only f
d) Neither f nor g
Q14. A class stores a private List of orders. Which getter design best preserves encapsulation?
a) Return the internal list reference directly
b) Make the list public and remove the getter
c) Return an unmodifiable view or a copy of the list
d) Return the list cast to Object
Q15. Which language features are mainly used to achieve abstraction in object oriented design?
a) Global variables and macros
b) Abstract classes and interfaces that declare what operations exist without fixing their implementation
c) Inline functions and templates only
d) Pointer arithmetic and casting
Q16. Class Account in package bank declares a protected method audit(). A class Report in package util, which does NOT extend Account, calls acc.audit(). What happens?
a) It compiles because protected is the same as public outside the package
b) It compiles but throws an exception at run time
c) It compiles only if Report is in the same module
d) Compilation error, because protected access outside the package is granted only to subclasses
Q17. According to the information-hiding principle introduced by David Parnas, what should each module hide?
a) Design decisions that are likely to change, behind a stable interface
b) Its public interface, so other modules cannot call it
c) All of its data, including constants needed by callers
d) Its documentation from other developers
Q18. What is printed by this C++ program?
#include <iostream>
using namespace std;
class Cache {
mutable int hits = 0;
int v = 7;
public:
int get() const { ++hits; return v; }
int count() const { return hits; }
};
int main() {
const Cache c;
c.get(); c.get();
cout << c.count();
}
a) 2
b) 0
c) 7
d) Compilation error: cannot modify a member in a const function
Quick revision notes
- Encapsulation = data + methods in one unit with controlled access.
- Abstraction shows what an object does; encapsulation hides how.
- C++ default access: class members private, struct members public.
- Java access, narrowest to widest: private, package (no modifier), protected, public.
- Access control is per class, not per object: a method can read private fields of another instance of its class.
- Returning a reference to a mutable private field leaks state; return a copy or unmodifiable view.
- Private inheritance makes inherited public and protected members private in the derived class.
- mutable lets a member change inside const member functions (logical constness).
Frequently asked questions
Is encapsulation the same as data hiding?
Data hiding is one part of encapsulation. Encapsulation means bundling state with the operations on it, and data hiding means restricting direct access to that state, usually by making fields private.
What is the difference between protected and default access in Java?
Default (package-private) members are visible only within the same package. Protected members are visible within the same package and also to subclasses in other packages.
Do getters and setters always guarantee encapsulation?
No. A setter that accepts any value, or a getter that returns a reference to a mutable internal object, exposes the state almost as much as a public field. Good accessors validate input and avoid leaking internal references.
Comments
Post a Comment