12.Analyze the following code.
public class Test {
int x;
public Test(String t) {
System.out.println("Test");
}
public static void main(String[] args) {
Test test = null;
System.out.println(test.x);
}
}
A.The program has a compile error because test is not initialized.
B.The program has a compile error because x has not been initialized.
C.The program has a compile error because you cannot create an object from the class that defines the object.
D.The program has a compile error because Test does not have a default constructor.
E.The program has a runtime NullPointerException because test is null while executing test.x.
17.Analyze the following code.
public class Test {
int x;
public Test(String t) {
System.out.println("Test");
}
public static void main(String[] args) {
Test test = new Test();
System.out.println(test.x);
}
}
A.The program has a compile error because System.out.println method cannot be invoked from the constructor.
B.The program has a compile error because x has not been initialized.
C.The program has a compile error because you cannot create an object from the class that defines the object.
D.The program has a compile error because Test does not have a default constructor.
23.Analyze the following code.
public class Test {
public static void main(String[] args) {
int n = 2;
xMethod(n);
System.out.println("n is " + n);
}
void xMethod(int n) {
n++;
}
}
A.The code has a compile error because xMethod does not return a value.
B.The code has a compile error because xMethod is not declared static.
C.The code prints n is 1.
D.The code prints n is 2.
E.The code prints n is 3.
24.What code may be filled in the blank without causing syntax or runtime errors:
public class Test {
java.util.Date date;
public static void main(String[] args) {
Test test = new Test();
System.out.println(_________________);
}
}
A.test.date
B.date
C.test.date.toString()
D.date.toString()
31.Suppose the xMethod() is invoked in the following constructor in a class, xMethod() is _________ in the class.
public MyClass() {
xMethod();
}
A.a static method
B.an instance method
C.a static method or an instance method
多选题
2.Analyze the following code:
class Test {
private double i;
public Test(double i) {
this.t();
this.i = i;
}
public Test() {
System.out.println("Default constructor");
this(1);
}
public void t() {
System.out.println("Invoking t");
}
}
A.this.t() may be replaced by t().
B.this.i may be replaced by i.
C.this(1) must be called before System.out.println("Default constructor").
D.this(1) must be replaced by this(1.0).
4.Which of the following statements are true?
A.Multiple constructors can be defined in a class.
B.Constructors do not have a return type, not even void.
C.Constructors must have the same name as the class itself.
D.Constructors are invoked using the new operator when an object is created.