Java Programming - Declarations and Access Control

Exercise : Declarations and Access Control - General Questions
16.
Which two cause a compiler error?
  1. float[ ] f = new float(3);
  2. float f2[ ] = new float[ ];
  3. float[ ]f1 = new float[3];
  4. float f3[ ] = new float[3];
  5. float f5[ ] = {1.0f, 2.0f, 2.0f};
2, 4
3, 5
4, 5
1, 2
Answer: Option
Explanation:

(1) causes two compiler errors ( '[' expected and illegal start of expression) because the wrong type of bracket is used, ( ) instead of [ ]. The following is the correct syntax: float[ ] f = new float[3];

(2) causes a compiler error ( '{' expected ) because the array constructor does not specify the number of elements in the array. The following is the correct syntax: float f2[ ] = new float[3];

(3), (4), and (5) compile without error.


17.
Given a method in a protected class, what access modifier do you use to restrict access to that method to only the other members of the same class?
final
static
private
protected
volatile
Answer: Option
Explanation:

The private access modifier limits access to members of the same class.

Option A, B, D, and E are wrong because protected are the wrong access modifiers, and final, static, and volatile are modifiers but not access modifiers.


18.
Which is a valid declaration within an interface?
public static short stop = 23;
protected short stop = 23;
transient short stop = 23;
final void madness(short stop);
Answer: Option
Explanation:

(A) is valid interface declarations.

(B) and (C) are incorrect because interface variables cannot be either protected or transient. (D) is incorrect because interface methods cannot be final or static.