Java Questions & Answers – Access Control – 2

This set of Java Interview Questions and Answers focuses on “Access Control – 2”.

1. Which one of the following is not an access modifier?
a) Public
b) Private
c) Protected
d) Void
View Answer

Answer: d
Explanation: Public, private, protected and default are the access modifiers.

2. All the variables of class should be ideally declared as?
a) private
b) public
c) protected
d) default
View Answer

Answer: a
Explanation: The variables should be private and should be accessed with get and set methods.

3. Which of the following modifier means a particular variable cannot be accessed within the package?
a) private
b) public
c) protected
d) default
View Answer

Answer: a
Explanation: Private variables are accessible only within the class.
advertisement
advertisement

4. How can a protected modifier be accessed?
a) accessible only within the class
b) accessible only within package
c) accessible within package and outside the package but through inheritance only
d) accessible by all
View Answer

Answer: c
Explanation: The protected access modifier is accessible within package and outside the package but only through inheritance. The protected access modifier can be used with data member, method and constructor. It cannot be applied in the class.

5. What happens if constructor of class A is made private?
a) Any class can instantiate objects of class A
b) Objects of class A can be instantiated only within the class where it is declared
c) Inherited class can instantiate objects of class A
d) classes within the same package as class A can instantiate objects of class A
View Answer

Answer: b
Explanation: If we make any class constructor private, we cannot create the instance of that class from outside the class.
Note: Join free Sanfoundry classes at Telegram or Youtube

6. All the variables of interface should be?
a) default and final
b) default and static
c) public, static and final
d) protect, static and final
View Answer

Answer: c
Explanation: Variables of an interface are public, static and final by default because the interfaces cannot be instantiated, final ensures the value assigned cannot be changed with the implementing class and public for it to be accessible by all the implementing classes.

7. What is true of final class?
a) Final class cause compilation failure
b) Final class cannot be instantiated
c) Final class cause runtime failure
d) Final class cannot be inherited
View Answer

Answer: d
Explanation: Final class cannot be inherited. This helps when we do not want classes to provide extension to these classes.
advertisement

8. How many copies of static and class variables are created when 10 objects are created of a class?
a) 1, 10
b) 10, 10
c) 10, 1
d) 1, 1
View Answer

Answer: a
Explanation: Only one copy of static variables are created when a class is loaded. Each object instantiated has its own copy of instance variables.

9. Can a class be declared with a protected modifier.
a) True
b) False
View Answer

Answer: b
Explanation: Protected class member (method or variable) is like package-private (default visibility), except that it also can be accessed from subclasses. Since there is no such concept as ‘subpackage’ or ‘package-inheritance’ in Java, declaring class protected or package-private would be the same thing.
advertisement

10. Which is the modifier when there is none mentioned explicitly?
a) protected
b) private
c) public
d) default
View Answer

Answer: d
Explanation: Default is the access modifier when none is defined explicitly. It means the member (method or variable) can be accessed within the same package.

Sanfoundry Global Education & Learning Series – Java Programming Language.

To practice all areas of Java for Interviews, here is complete set on 1000+ Multiple Choice Questions and Answers on Java.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.