4.Which of the following is incorrect?
A.A constructor may be static.
B.A constructor may be private.
C.A constructor may invoke a static method.
D.A constructor may invoke an overloaded constructor.
E.A constructor invokes its superclass no-arg constructor by default if a constructor does not invoke an overloaded constructor or its superclass‘ constructor.
10.Which of the following statements is false?
A.You can always pass an instance of a subclass to a parameter of its superclass type. This feature is known as polymorphism.
B.The compiler finds a matching method according to parameter type, number of parameters, and order of the parameters at compile time.
C.A method may be implemented in several subclasses. The Java Virtual Machine dynamically binds the implementation of the method at runtime.
D.Dynamic binding can apply to static methods.
E.Dynamic binding can apply to instance methods.
13.Which of the following are Java keywords?
A.instanceOf
B.instanceof
C.cast
D.casting
16.Given the following classes and their objects:
class C1 {};
class C2 extends C1 {};
class C3 extends C1 {};
C2 c2 = new C2();
C3 c3 = new C3();
Analyze the following statement:
c2 = (C2)((C1)c3);
A.c3 is cast into c2 successfully.
B.You will get a runtime error because you cannot cast objects from sibling classes.
C.You will get a runtime error because the Java runtime system cannot perform multiple casting in nested form.
D.The statement is correct.
18.
Analyze the following code:
public class Test {
public static void main(String[] args) {
String s = new String("Welcome to Java");
Object o = s;
String d = (String)o;
}
}
A.When assigning s to o in Object o = s, a new object is created.
B.When casting o to s in String d = (String)o, a new object is created.
C.When casting o to s in String d = (String)o, the contents of o is changed.
D.s, o, and d reference the same String object.
19.The equals method is defined in the Object class. Which of the following is correct to override it in the String class?
A.public boolean equals(String other)
B.public boolean equals(Object other)
C.public static boolean equals(String other)
D.public static boolean equals(Object other)
20.
What is the output of the following code:
public class Test {
public static void main(String[] args) {
Object o1 = new Object();
Object o2 = new Object();
System.out.print((o1 == o2) + " " + (o1.equals(o2)));
}
}
A.false false
B.true true
C.false true
D.true false
21.What is the output of the following code:
public class Test {
public static void main(String[] args) {
String s1 = new String("Java");
String s2 = new String("Java");
System.out.print((s1 == s2) + " " + (s1.equals(s2)));
}
}
A.false false
B.true true
C.false true
D.true false
25.Analyze the following code.
// Program 1:
public class Test {
public static void main(String[] args) {
Object a1 = new A();
Object a2 = new A();
System.out.println(a1.equals(a2));
}
}
class A {
int x;
public boolean equals(Object a) {
return this.x == ((A)a).x;
}
}
// Program 2:
public class Test {
public static void main(String[] args) {
Object a1 = new A();
Object a2 = new A();
System.out.println(a1.equals(a2));
}
}
class A {
int x;
public boolean equals(A a) {
return this.x == a.x;
}
}
A.Program 1 displays true and Program 2 displays true
B.Program 1 displays false and Program 2 displays true
C.Program 1 displays true and Program 2 displays false
D.Program 1 displays false and Program 2 displays false
26.Analyze the following code.
// Program 1:
public class Test {
public static void main(String[] args) {
Object a1 = new A();
Object a2 = new A();
System.out.println(a1.equals(a2));
}
}
class A {
int x;
public boolean equals(A a) {
return this.x == a.x;
}
}
// Program 2:
public class Test {
public static void main(String[] args) {
A a1 = new A();
A a2 = new A();
System.out.println(a1.equals(a2));
}
}
class A {
int x;
public boolean equals(A a) {
return this.x == a.x;
}
}
A.Program 1 displays true and Program 2 displays true
B.Program 1 displays false and Program 2 displays true
C.Program 1 displays true and Program 2 displays false
D.Program 1 displays false and Program 2 displays false
27.Analyze the following code.
// Program 1
public class Test {
public static void main(String[] args) {
Object a1 = new A();
Object a2 = new A();
System.out.println(((A)a1).equals((A)a2));
}
}
class A {
int x;
public boolean equals(A a) {
return this.x == a.x;
}
}
// Program 2
public class Test {
public static void main(String[] args) {
A a1 = new A();
A a2 = new A();
System.out.println(a1.equals(a2));
}
}
class A {
int x;
public boolean equals(A a) {
return this.x == a.x;
}
}
A.Program 1 displays true and Program 2 displays true
B.Program 1 displays false and Program 2 displays true
C.Program 1 displays true and Program 2 displays false
D.Program 1 displays false and Program 2 displays false
30.Polymorphism means ______________.
A.that data fields should be declared private
B.that a class can extend another class
C.that a variable of supertype can refer to a subtype object
D.that a class can contain another class
31.
Encapsulation means ______________.
A.that data fields should be declared private
B.that a class can extend another class
C.that a variable of supertype can refer to a subtype object
D.that a class can contain another class
Which statements are most accurate regarding the following classes?
class A {
private int i;
protected int j;
}
class B extends A {
private int k;
protected int m;
}
A.An object of B contains data fields i, j, k, m.
B.An object of B contains data fields j, k, m.
C.An object of B contains data fields j, m.
D.An object of B contains data fields k, m.
43.Analyze the following code:
double[] array = {1, 2, 3};
ArrayList<Double> list = new ArrayList<>(Arrays.asList(array));
System.out.println(list);
A.The code is correct and displays [1, 2, 3].
B.The code is correct and displays [1.0, 2.0, 3.0].
C.The code has a compile error because an integer such as 1 is automatically converted into an Integer object, but the array element type is Double.
D.The code has a compile error because asList(array) requires that the array elements are objects.
44.Analyze the following code:
Double[] array = {1, 2, 3};
ArrayList<Double> list = new ArrayList<>(Arrays.asList(array));
System.out.println(list);
A.The code is correct and displays [1, 2, 3].
B.The code is correct and displays [1.0, 2.0, 3.0].
C.The code has a compile error becaue an integer such as 1 is automatically converted into an Integer object, but the array element type is Double.
D.The code has a compile error because asList(array) requires that the array elements are objects.
多选:
2.Analyze the following code:
ArrayList<String> list = new ArrayList<String>();
list.add("Beijing");
list.add("Tokyo");
list.add("Shanghai");
list.set(3, "Hong Kong");
A.The last line in the code causes a runtime error because there is no element at index 3 in the array list.
B.The last line in the code has a compile error because there is no element at index 3 in the array list.
C.If you replace the last line by list.add(3, "Hong Kong"), the code will compile and run fine.
D.If you replace the last line by list.add(4, "Hong Kong"), the code will compile and run fine.
5.
Which of the following statements are true?
A.Override the methods equals and toString defined in the Object class whenever possible.
B.Override the hashCode method whenever the equals method is overridden. By contract, two equal objects must have the same hash code.
C.A public default no-arg constructor is assumed if no constructors are defined explicitly.
D.You should follow standard Java programming style and naming conventions. Choose informative names for classes, data fields, and methods.