每周10题 NO.1

来源:互联网 发布:大数据产业园怎么样 编辑:程序博客网 时间:2024/05/16 16:04
  1. 根据下面的代码,
    String s = null;
    会抛出NullPointerException异常的有( )
      A:if( (s!=null) & (s.length()>0) )
      B:if( (s!=null) & & (s.length()>0) )
      C:if( (s==null) | (s.length()==0) )
      D:if( (s==null) || (s.length()==0) )
    答案:A C
    [1]”双”会短路!!!
    String s;有声明
    String s=null;有声明有引用,但是引用不指向对象
    String s=”“;有声明有引用,且引用指向对象
    String s=null引用不指向对象的表现就是,该对象s不能调用方法

  2. 以下关于子类和父类的叙述中,错误的是( )。
      A:代码中使用父类对象的地方,都可以使用子类对象替换
      B:代码中使用子类对象的地方,都可以使用父类对象替换
      C:父类定义的对象与子类定义的对象在属性和方法上一定相同
      D:父类定义的对象与子类定义的对象在属性和方法上一定不同
    答案:B C D
    疑问:A中当子类对象重写父类方法的时候,运行效果肯定不一样啊?(题的本意是说父类对象用子类对象替换,程序不会报错,不报错即能替换)

  3. “我是大神”.getBytes().length 输出 结果是什么?
      A:4
      B:8
      C:12
      D: 可能是8也可能是12
      答案:D.解释:结果取决于项目的编码方式,
      
    str.length:4
    str.getBytes().length:8
    str.getBytes(‘utf-8’).length:12//u8编码中文是3个字节
    str.getBytes(‘GBK’).length:8//GBK编码中文是2个字节
    若不指定编码方式,结果取决于工程人工指定的编码方式
    详情请看http://blog.csdn.net/sundenskyqq/article/details/10382815

  4. 写出下面代码的运行结果

public class Test {    public static void main(String[] args)  {        Shape shape = new Circle();        System.out.println(shape.name);        shape.printType();        shape.printName();    }}class Shape {    public String name = "shape";    public Shape(){        System.out.println("shape constructor");    }    public void printType() {        System.out.println("this is shape");    }    public static void printName() {        System.out.println("shape");    }}class Circle extends Shape {    public String name = "circle";    public Circle() {        System.out.println("circle constructor");    }    public void printType() {        System.out.println("this is circle");    }    public static void printName() {        System.out.println("circle");    }}

答案:

shape constructorcircle constructorshapethis is circleshape

*[1]创建对象过程:先用类加载器加载静态方法和静态代码块(只加载一次),在去创建对象(包括初始化成员变量和构造方法)
[2]在继承中:父类的构造器调用以及成员变量的初始化过程一定在子类的前面
[3]在多态中:成员变量和静态方法还用自己的,会对子类重写的父类方法进行动态绑定(即多态用的是子类重写的方法)
还不懂得话,看这里http://www.cnblogs.com/dolphin0520/p/3803432.html*

0 0