| No. | Answer | Remark | |
|---|---|---|---|
| 1 | b | Prints: 1,2 | Primitive arguments are passed by value. The method m1 increments the parameter x, and the result is returned. The local variable x of main remains unchanged. |
| 2 | a d e | Encapsulation is a form of data hiding. Encapsulation helps to protect data from corruption. Encapsulation allows for changes to the internal design of a class while the public interface remains unchanged. | A tightly encapsulated class does not allow direct public access to the internal data model. Instead, access is permitted only through accessor (i.e. get) and mutator (i.e. set) methods. The additional time required to work through the accessor and mutator methods typically slows execution speed. Encapsulation is a form of data hiding. A tightly encapsulated class does not allow public access to any data member that can be changed in any way; so encapsulation helps to protect internal data from the possibility of corruption from external influences. The mutator methods can impose contraints on the argument values. If an argument falls outside of the acceptable range, then a mutator method could throw an IllegalArgumentException. The internal design of a tightly encapsulated class can change while the public interface remains unchanged. An immutable class is always tightly encapsulated, but not every tightly encapsulation class is immutable. |
| 3 | b c h | notify notifyAll wait | |
| 4 | e | Compile-time error |
There are two versions of the
|
| 5 | e | Compile-time error at 3 | The Byte class has only two constructors: one accepts a primitive byte; the other accepts a String. The argument that appears in the class instance creation expression new Byte(1) is of type int, but the compiler will not implicitly narrow the int to a byte. At line 1, the assignment expression primitiveByte = 1 causes the compiler to implicitly narrow a literal of type int to type byte. The compiler will implicitly do a narrowing conversion for an assignment expression if the right hand operand is a compile-time constant of type byte, short, char or int and the value falls within the range of the variable on the left and the variable is of type byte, short, or char. For that reason, a constant int expression that is equal to 1 may be assigned to a byte without an explicit cast. The same is not true for the parameters of a method or constructor. The designers of the Java programming language felt that implicit narrowing conversions of method and constructor arguments would add unnecessary complexities to the process of resolving overloaded method calls. |
| 6 | a | Prints: 5 | The Integer class has only two constructors: one accepts a primitive int, and the other accepts a String. If the argument is of type byte, short or char, then it is implicitly promoted to type int. |
| 7 | c | Compile-time error | Long has two constructors: one accepts an argument of type primitive long; the other accepts an argument of type String. The class instance creation expression new Long(primitiveFloat) generates a compile-time error, because the compiler will not implicitly apply a primitive narrowing conversion to the argument. |
| 8 | b | Prints: 4.0 | The Double constructor is overloaded: one accepts an argument of type primitive double; the other accepts an argument of type String. |
| 9 | c d | 3 4 | The boolean literals true and false are written with lower case letters; upper case letters produce a compile-time error. A String representation of a boolean literal is acceptable in both upper and lower case letters. |
| 10 | k | None of the above | The Float constructor is overloaded: one version accepts a primitive of type float; one accepts a primitive of type double; one accepts a String representation of a floating-point literal. |
| 11 | a b c d | new Short(1) new Short('1') new Short('b' - 'a') new Short((short)1 - (short)2) | The Short class has only two constructors: one accepts a primitive short; the other accepts a String. The argument that appears in the class instance creation expression new Short(1) is of type int, but the compiler will not implicitly narrow the int to a short. The argument that appears in the class instance creation expression new Short('1') is of type char, but the compiler will not implicitly convert the char to a short. The argument that appears in the class instance creation expression new Short('b' - 'a') is of type int, but the compiler will not implicitly narrow the int to a short. The argument that appears in the class instance creation expression new Short((short)1 - (short)2) is of type int, but the compiler will not implicitly narrow the int to a short. If both operands of a binary arithmetic expression are of type byte, char or short; then both are implicitly widened to type int, and the result of the expression is of type int. |
| 12 | c | LinkedList | ArrayList and Vector both use an array to store the elements of the list; so access to any element using an index is very fast. A LinkedList is implemented using a doubly linked list; so access to an element requires the list to be traversed using the links. |
| 13 | g | Prints: true,true,false | The List and Set interfaces extend the Collection interface; so both List and Set could be cast to type Collection without generating a ClassCastException. Therefore, the first two of the three relational expressions return true. The Map interface does not extend Collection. The reference variable c1 refers to an instance of TreeMap; so the relational expression c1 instanceof Collection returns the value false. |
| 14 | b | true | |
| 15 | b | Prints: 0,0,0.0,null | Each array contains the default value for its type. The default value of a primitive byte or a primitive long is printed as 0. The default value of a float primitive is printed as 0.0. The default value of an Object is null and is printed as null. |
| 16 | e | Prints: 2,5,9 | The declarations of the array variables, a1, a2 and a3, are different in terms of the position of the square brackets, but each is of type int[][]. The array access expression, a1[0][1] = a[1st subarray][2nd component] = 2. Each of the three array variable declarations has an array initializer. The same initializer, {{1,2},{3,4,5},{6,7,8,9}}, is used in each of the three cases. The initializer specifies three components of type int[], so each component is a reference to an array object containing components of type int. The size of each of the subarrays is different: The size of the first is 2, the second is 3, and the third is 4. |
| 17 | d | Declare all constructors using the private access modifier. | If no constructors are declared explicitly; then the compiler will create one implicitly, and it will have the same access modifier as the class. The explicit declaration of any constructor will prevent the creation of a default constructor. If all constructors are declared private, then code outside of the class will not have access to the constructors and will not have the ability to create an instance of the class. Constructors do not return a value and constructor declarations do not include a return type, so the keyword void is not applicable to a constructor declaration. |
| 18 | b d e f | Instance variables declared in this class or any superclass. Instance methods declared in this class or any superclass. The keyword this. The keyword super. | If the superclass constructor invocation, "super(argumentListopt);", appears explicitly or implicitly, then it must be the first statement in the body of the constructor. Until the superclass constructor invocation runs to completion, no other statements are processed within the body of the constructor. The same is true of the constructors of any superclass. (Note: The primordial class, Object, does not have a superclass, so the constructors do not include a superclass constructor invocation statement.) Suppose class B is a subclass of A. The process of creating and initializing an instance of B includes the creation and initialization of an instance of the superclass A and an instance of the superclass Object. The superclass constructor invocation statement appearing in a constructor of B is invoked before the completion of the constructors of the superclasses A and Object. A superclass constructor invocation statement appearing in B can not refer to the non-static members of the superclasses, because the process of initializing those non-static superclass members is not complete when the superclass constructor invocation occurs in B. The same is true of the non-static members of B. |
| 19 | d | Prints: 127 -128 -1 0 | Bytes are stored as 8 bit two's complement signed integers. When an int primitive is cast to a byte, the three most significant bytes are discarded and only the least significant byte remains. The most significant bit of the remaining byte becomes the new sign bit. byte a = (byte)127; // 01111111. byte b = (byte)128; // 10000000. byte c = (byte)255; // 11111111. byte d = (byte)256; // 00000000. |
| 20 | e | None of the above | Line 4 does not generate a compile-time error. The reference named base actually refers to an instance of type Sub, so the reference may be cast to type Sub. |
| 21 | g | None of the above | The null literal is converted to an int array type with the value null. All array types implement the Cloneable interface, so any array reference can be assigned to a reference of type Cloneable. The int array object referenced by the Cloneable reference, c, can be assigned to a reference of the int array type, int[]. |
| 22 | a | Prints: float,float | A method invocation conversion can widen an argument of type float to match a method parameter of type double, so any argument that can be passed to m(float i) can also be passed to m(double i) without generating a compile-time type error. For that reason, we can say that m(float i) is more specific than m(double i). Since both methods are applicable, the more specific of the two, m(float i), is chosen over the less specific, m(double i). The arguments of the method invocation expressions, m(a1) and m(b1), are of types int and long respectively. A method invocation conversion can widen an argument of type int or long to match either of the two method parameter types float or double; so both methods, m(float i) and m(double i), are applicable to the two method invocation expressions. Since both methods are applicable, the more specific of the two, m(float i) is chosen rather than the less specific, m(double i). |
| 23 | b | Prints: B.s1 A.s2 A.s1 A.s2 | The variables of a subclass can hide the variables of a superclass or interface. The variable that is accessed is determined at compile-time based on the type of the reference--not the run-time type of the object. The two references x and y refer to the same instance of type B. The name x.s1 uses a reference of type B; so it refers to the variable s1 declared in class B. The name y.s1 uses a reference of type A; so it refers to the variable s1 declared in class A. |
| 24 | g | None of the above | Please note that this question asks which variable can NOT be substituted. Class Z is a non-static member class of class A; therefore, all of the fields and methods of class A (static, non-static, and private) are available to class Z. |
| 25 | b | Prints: ABC | The method invocation expression d1.m1(a1) uses reference d1 of type D to invoke method m1. Since the reference d1 is of type D, the class D is searched for an applicable implementation of m1. The methods inherited from the superclasses, C, B and A, are included in the search. The argument, a1, is a variable declared with the type A; so method A.m1(A a) is invoked. |
| 26 | a | Prints: false,false |
String
instances are
immutable.
String
methods such as
|
| 27 | a | Prints: false,false |
|
| 28 | b d | An anonymous class is implicitly final. A static reference variable can reference an instance of an anonymous class. | An anonymous class can extend Object and implement an interface, or the anonymous class can extend a named class including Object. An anonymous class can not be extended; so it can not be abstract. An anonymous class declaration always creates an instance of a class; so it is not surprising that an anonymous class can not be declared static. Even so, a static reference variable can refer to an anonymous class. A constructor shares the same name as the class in which it is declared, but an anonymous class has no name. For that reason, it is not surprising that an anonymous class declaration can not contain an explicit constructor declaration. Instead, an anonymous class can contain an instance initializer. |
| 29 | c | Prints: Bird,Cat | Although the reference variable r2 is assigned the value of reference variable r1 in method m1, the reference pet2 remains unchanged in the main method. Object references are passed by value: the invoked method gets a copy of the object reference. The reference parameter r1 can be used to modify the state of the instance referenced by pet1, but r2 can not be used to force pet2 to reference a different instance. |