Java Questions and Answers Part-7

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

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

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

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

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

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

Answer: b
Explanation: If we make any class constructor private, we cannot create the instance of that class from outside the class.

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

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

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

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

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

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.

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

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.