JAVA第九章:类与对象

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.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值