Which of the below given statement(s) is/are true?
Qualcomm technical mcq question, verified with a worked answer. Free to practise - no sign-up.
Which of the below given statement(s) is/are true?
I) If the access specifier of the constructor is private then an object of corresponding class can be created in the context of the same class but not in the context of some other classes.
II) If the access specifier of the constructor is not private then an object of corresponding class can be created both in the same class context and in other class context.
Show answer & explanation
Statement (I) is true: a private constructor restricts object creation to within the same class (e.g., in static factory methods or nested classes), preventing instantiation from external classes. Statement (II) is also true: non-private constructors (public or protected) allow object creation both within the same class and from other classes, depending on the specific access level. Both statements accurately describe constructor access control behavior.
Step-by-step Derivation:
Analysis of constructor access specifiers:
Statement (I) - Private Constructor:
- private class MyClass { private MyClass() {} }
- Objects can only be created inside MyClass methods (e.g., static factory methods)
- External code: MyClass obj = new MyClass(); // COMPILER ERROR
- This statement is TRUE.
Statement (II) - Non-Private Constructor (public or protected):
- public class MyClass { public MyClass() {} }
- Objects can be created inside MyClass: MyClass obj = new MyClass(); // OK
- Objects can be created outside MyClass in other classes: MyClass obj = new MyClass(); // OK
- Same applies for protected constructors in subclass context
- This statement is TRUE.
Conclusion: Both statements (I) and (II) are correct, so the answer is (D).