4.20 | Which statements are true about the use of modifiers? Select the two correct answers. -
If no accessibility modifier ( public , protected , and private ) is specified for a member declaration, the member is only accessible for classes in the package of its class and subclasses of its class anywhere . -
You cannot specify accessibility of local variables . They are only accessible within the block in which they are declared. -
Subclasses of a class must reside in the same package as the class they extend. -
Local variables can be declared static . -
Objects themselves do not have any accessibility modifiers, only the object references do. |
4.21 | Given the following source code, which comment line can be uncommented without introducing errors? abstract class MyClass { abstract void f(); final void g() {} // final void h() {} // (1) protected static int i; private int j; } final class MyOtherClass extends MyClass { // MyOtherClass(int n) { m = n; } // (2) public static void main(String[] args) { MyClass mc = new MyOtherClass(); } void f() {} void h() {} // void k() { i++; } // (3) // void l() { j++; } // (4) int m; } Select the one correct answer. -
final void h() {} // (1) -
MyOtherClass(int n) { m = n; } // (2) -
void k() { i++; } // (3) -
void l() { j++; } // (4) |
4.22 | What would be the result of attempting to compile and run the following program? class MyClass { static MyClass ref; String[] arguments; public static void main(String[] args) { ref = new MyClass(); ref.func(args); } public void func(String[] args) { ref.arguments = args; } } Select the one correct answer. -
The program will fail to compile, since the static method main() cannot have a call to the non-static method func() . -
The program will fail to compile, since the non-static method func() cannot access the static variable ref . -
The program will fail to compile, since the argument args passed to the static method main() cannot be passed on to the non-static method func() . -
The program will fail to compile, since the method func() cannot assign the value of the static variable ref to the non-static variable arguments . -
The program will compile, but will throw an exception when run. -
The program will compile and run successfully. |
4.23 | Given the following member declarations, which statement is true? int a; // (1) static int a; // (2) int f() { return a; } // (3) static int f() { return a; } // (4) Select the one correct answer. -
Declarations (1) and (3) cannot occur in the same class definition. -
Declarations (2) and (4) cannot occur in the same class definition. -
Declarations (1) and (4) cannot occur in the same class definition. -
Declarations (2) and (3) cannot occur in the same class definition. |
4.24 | Which statement is true? Select the one correct answer. -
A static method can call other non-static methods in the same class by using the this keyword. -
A class may contain both static and non-static variables and both static and non-static methods. -
Each object of a class has its own instance of each static variable. -
Instance methods may access local variables of static methods. -
All methods in a class are implicitly passed a this parameter when called. |
4.25 | What, if anything, is wrong with the following code? abstract class MyClass { transient int j; synchronized int k; final void MyClass() {} static void f() {} } Select the one correct answer. -
The class MyClass cannot be declared abstract . -
The field j cannot be declared transient . -
The field k cannot be declared synchronized . -
The method MyClass() cannot be declared final . -
The method f() cannot be declared static . -
Nothing is wrong with the code; it will compile without errors. |
4.26 | Which one of these is not a legal member declaration within a class? Select the one correct answer. -
static int a; -
final Object[] fudge = { null }; -
abstract int t; -
native void sneeze(); -
final static private double PI = 3.14159265358979323846; |
4.27 | Which statements are true about modifiers? Select the two correct answers. -
Abstract classes can contain final methods. -
Fields can be declared native . -
Non- abstract methods can be declared in abstract classes. -
Classes can be declared native . -
Abstract classes can be declared final . |
4.28 | Which statement is true? Select the one correct answer. -
Transient fields will not be saved during serialization. -
Constructors can be declared abstract . -
The initial state of an array object constructed with the statement int[] a = new int[10] will depend on whether the array variable a is a local variable or a field. -
A subclass of a class with an abstract method must provide an implementation for the abstract method. -
Only static methods can access static members . |