Question 41
MediumWhich Java access modifier lets a member be used by subclasses in other packages but not by unrelated classes there?
- A.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Object-Oriented Programming
Which Java access modifier lets a member be used by subclasses in other packages but not by unrelated classes there?
private
protected
public
default (package-private)
Sign up free to see the answer and a step-by-step explanation.
In Java, which members of class A can class B (in the same package, not a subclass) access?
Only public members
public, protected and default members
Only private members
All members, including private
Sign up free to see the answer and a step-by-step explanation.
A Java class Order exposes a public field total that other classes update directly. What is the main risk?
The program will not compile
The field becomes static
Any code can set total to an invalid value, and the class cannot enforce rules or change its representation later
Memory usage doubles
Sign up free to see the answer and a step-by-step explanation.
What does a class invariant mean?
A static method
A constant defined in an interface
A variable that never changes
A condition about the object's state that its methods always keep true, such as balance ≥ 0
Sign up free to see the answer and a step-by-step explanation.
An immutable class Money has private final fields and no setters. How does it support 'adding' money?
add() returns a new Money object with the sum
add() changes a static field
add() changes the fields using reflection
It cannot support adding
Sign up free to see the answer and a step-by-step explanation.
Why is a getter that returns a reference to a private mutable Date object a problem?
Dates cannot be returned
Callers can change the Date and so change the object's internal state
It prevents garbage collection
Getters must return primitives
Sign up free to see the answer and a step-by-step explanation.
In C++, what does a const member function such as int get() const promise?
It can be called only once
It will not modify the object's data members
It returns a constant
It is static
Sign up free to see the answer and a step-by-step explanation.
Which C++ keyword allows one specific member to be changed even inside a const member function, for example a cache?
explicit
volatile
mutable
static
Sign up free to see the answer and a step-by-step explanation.
In Python, what does a single leading underscore, as in self._rate, signal?
It is a syntax error
It makes the attribute truly private
A convention that the attribute is internal and should not be used from outside
It makes it static
Sign up free to see the answer and a step-by-step explanation.
A Java class makes all fields private but provides a public setter for every field with no checks. What is the problem?
Setters slow down the program
It will not compile
The fields are private in name only; nothing is really protected
It breaks inheritance
Sign up free to see the answer and a step-by-step explanation.