【Java】面向对象程序设计 错题本

单选题

期中小测错题

1. Which specifier essentially declares a variable a global variable?

A. protected

B. static

C. final

D. default

B

global variable 是全局变量,也就是用 static 修饰的静态变量,因为它被所有实例所共享;

final 修饰的是常量,不可被改变;

这里再提一下Java里各种变量的英文:

global variable全局变量
local variable局部变量
block variable方法内变量

另外,在类定义中出现的由 static 修饰的变量称为类变量或静态变量,

如果没有被 static 修饰,就是实例变量或非静态变量。

 2. 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()

A

错选为C,本以为需要转化成字符串才能输出,但是这里的date还未赋值,其值为null,此时不能使用toString()方法,只能原原本本地输出。

 3. 关于被保护访问控制符protected修饰的成员变量,以下说法正确的是:

A. 可以被三种类所引用:该类自身、与它在同一个包中的其他类、在其他包中的该类的子类

B. 可以被两种类访问和引用:该类本身、该类的所有子类

C. 只能被该类自身所访问和修改

D. 只能被同一个包中的类访问

A

同一个类中同一个包中不同包的子类不同包的无关类
public
protected
无(空着不写)
private

 4. Which of the following statements are not true ?

A. A default constructor is provided automatically if no constructors are explicitly declared in the class

B. At least one constructor must always be defined explicitly

C. The default constructor is a no-arg constructor

D. A default constructor always exist in class

B

B选项说,只要要有一个构造方法需要被明确定义,但其实不定义的话也会有默认的构造方法,所以该选项错;

误选的是D,默认的构造方法一直存在于类中,这是没问题的,因为如果类中没有明确定义构造方法,就已经有默认的了,如果定义了,那就以定义的构造方法为默认构造方法。

 5. What will happen when you attempt to compile and run the following code?

class Base{
Base(){
        System.out.println("Base");
        }
}

public class Checket extends Base{
public static void main(String argv[]){
        Checket c = new Checket();
        super();
        }

Checket(){
        System.out.println("Checket");  
        }       
}

A. Compile time error

B. Checket followed by Base

C. Base followed by Checket

D. runtime error

A

super() 调用必须是构造方法主体中的第一条语句

 6.  What is the output of running class C?

class A {
  public A() {
    System.out.println(
      "The default constructor of A is invoked");
  }
}

class B extends A {
  public B() {
    System.out.println(
      "The default constructor of B is invoked");
  }
}

public class C  {
  public static void main(String[] args) {
    B b = new B();
  }
}

A. Nothing displayed

B. "The default constructor of B is invoked"

C. "The default constructor of A is invoked""The default constructor of B is invoked"

D. "The default constructor of B is invoked""The default constructor of A is invoked"

C

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值