Java Programming - Declarations and Access Control

Exercise :: Declarations and Access Control - General Questions


What is the most restrictive access modifier that will allow members of one class to have access to members of another class in the same package?

A. public
B. abstract
C. protected
D. synchronized
E. default access

Answer: Option E


default access is the "package oriented" access modifier.

Option A and C are wrong because public and protected are less restrictive. Option B and D are wrong because abstract and synchronized are not access modifiers.


Which of the following is/are legal method declarations?

  1. protected abstract void m1();
  2. static final void m1(){}
  3. synchronized public final void m1() {}
  4. private native void m1();

A. 1 and 3
B. 2 and 4
C. 1 only
D. All of them are legal declarations.

Answer: Option D


All the given statements are legal declarations.


Which cause a compiler error?

A. int[ ] scores = {3, 5, 7};
B. int [ ][ ] scores = {2,7,6}, {9,3,45};
C. String cats[ ] = {"Fluffy", "Spot", "Zeus"};
D. boolean results[ ] = new boolean [] {true, false, true};
E. Integer results[ ] = {new Integer(3), new Integer(5), new Integer(8)};

Answer: Option B


Option B generates a compiler error: <identifier> expected. The compiler thinks you are trying to create two arrays because there are two array initialisers to the right of the equals, whereas your intention was to create one 3 x 3 two-dimensional array.

To correct the problem and make option B compile you need to add an extra pair of curly brackets:

int [ ] [ ] scores = { {2,7,6}, {9,3,45} };


Which three are valid method signatures in an interface?

  1. private int getArea();
  2. public float getVol(float x);
  3. public void main(String [] args);
  4. public static void main(String [] args);
  5. boolean setFlag(Boolean [] test);

A. 1 and 2
B. 2, 3 and 5
C. 3, 4, and 5
D. 2 and 4

Answer: Option B


(2), (3), and (5). These are all valid interface method signatures.

(1), is incorrect because an interface method must be public; if it is not explicitly declared public it will be made public implicitly. (4) is incorrect because interface methods cannot be static.


You want a class to have access to members of another class in the same package. Which is the most restrictive access that accomplishes this objective?

A. public
B. private
C. protected
D. default access

Answer: Option D


The only two real contenders are C and D. Protected access Option C makes a member accessible only to classes in the same package or subclass of the class. While default access Option D makes a member accessible only to classes in the same package.