Question 1
EasyWhat is encapsulation?
- A.
Inheriting from several classes
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Object-Oriented Programming
What is encapsulation?
Inheriting from several classes
Having many forms of one method
Breaking a program into files
Bundling data and the methods that act on it into one unit, and hiding the internal details
Answer: D
A class exposes a public interface while keeping its fields private.
Which access modifier makes a member visible only inside its own class?
protected
default
private
public
Answer: C
Private members can't be accessed from outside the class.
What are getters and setters for?
Creating objects
Inheriting classes
Deleting objects
Controlled reading and writing of private fields
Answer: D
A setter can validate a value before storing it, for example rejecting a negative age.
Which access modifier makes a member accessible from anywhere?
internal-only
private
protected
public
Answer: D
Public members form the class's interface to the outside world.
What is the main benefit of encapsulation?
Every field becomes public
Internal data can't be changed in invalid ways, and the implementation can change without breaking users
Programs run faster
Classes can't have methods
Answer: B
Callers depend only on the public methods, not on the internal representation.
In Java, who can access a protected member?
Only the class itself
Everyone
Only classes in other packages
The class itself, its subclasses and classes in the same package
Answer: D
protected is wider than package-private: subclasses in other packages can also access it.
In Python, what does a leading double underscore (as in __balance) do to an attribute name?
It makes the attribute truly private and inaccessible
It deletes the attribute
It makes the attribute static
It triggers name mangling, making accidental access from outside harder
Answer: D
Python changes __balance to _ClassName__balance. It is a convention-based protection, not a strict one.
In C++, what is the default access level of members declared in a class?
protected
private
public
package-private
Answer: B
Members of a C++ class are private unless an access specifier says otherwise.
What does a read-only property of a class provide?
The value is stored on disk
The value is hidden from the class itself
Other code can change it but not read it
Other code can read the value but cannot change it directly
Answer: D
It exposes data for reading while keeping control of changes inside the class.
Why are fields usually made private while methods are public?
Because compilers require it
Private fields use less memory
So the class controls how its data is read and changed
Public methods run faster
Answer: C
Methods can check values and keep the object in a valid state.