| No. | Answer | Remark | |
|---|---|---|---|
| 1 | e | Compile-time error | The access modifiers public, protected and private, can not be applied to variables declared inside methods. |
| 2 | d | Prints: 0 | Member variables are initialized automatically. Type int variables are initialized to zero. |
| 3 | d | Prints: 1, 1, 0, 1 | Both instances of class Red share a single copy of the static field b. Although field b is only incremented using the r1 reference, the change is visible in the r2 instance of class Red. |
| 4 | e | Compile-time error | A static method can not access a non-static variable. |
| 5 | b c d e | final private protected public | The access modifiers, private, protected and public, can be applied to a field. A final field can not have its value assigned more than once. The abstract modifier may be applied to methods but not to fields. |
| 6 | a c d | static transient volatile | A transient field is not part of the persistent state of an object. Transient fields are not serialized. Fields that are shared between threads may be marked volatile to force each thread to reconcile its own working copy of the field with the master copy stored in the main memory. The synchronized modifier may be applied to methods but not to fields. |
| 7 | d | transient | A transient field is not part of the persistent state of an object, so it is not serialized. A static field is also not part of the persistent state of an object, and also is not serialized. |
| 8 | a d e | A value can not be assigned to a final field more than once. A compile-time error is thrown if a blank final instance variable is not assigned a value before the end of each constructor. A field can not be declared both final and volatile. | Static and non-static field variables may be declared final. All final fields must be definitely assigned a value once and only once. If the declaration of a final variable does not include an initializer then the variable is called a blank final. All blank, final, static variables must be assigned in a static initializer. All blank final non-static variables must be assigned by the end of the instance construction process. A field is sometimes shared between threads. The volatile modifier is used to force threads to reconcile their own working copy of a field with the master copy in main memory. If a field is declared final then its value does not change and there is no need for threads to reconcile their own working copies of the variable with the master copy in main memory. |
| 9 | a b c d e f | 1 2 3 4 5 6 | A variable that is local to a method can not be accessed from outside of the class, so the access modifiers are not useful and not legal. A variable that is local to a method can not be part of the persistent state of an object, so the transient modifier is not useful and not legal. Local variables can not be shared between threads, so the volatile modifier is not useful and not legal. A local variable can be declared final to prevent its value from being assigned more than once. If the value of the variable needs to be accessed from a local class or an anonymous class, then the local variable or method parameter must be declared final. |
| 10 | c g | 3 7 | The abstract modifier can be applied to a class and a method but not a field. A field is sometimes shared between threads. The volatile modifier is used to force threads to reconcile their own working copy of a field with the master copy in main memory. If a field is declared final then its value does not change and there is no need for threads to reconcile their own working copies of the variable with the master copy in main memory. |
| 11 | e | 5 | The synchronized modifier is a method modifier and can not be applied to a field. |
| 12 | d f h | 4 6 8 |
The first letter of an identifier can be any
|