这道题目错在哪里啊,就高手讲解
1.What is wrong in the following code?class TempClass {
   int i;
   public TempClass(int j) {
     int i = j; 
   }
}public class C {
   public static void main(String[] args) {
     TempClass temp = new TempClass(2);
   }
}这道题目为什么选e呢?
2. Analyze the following code.  epublic 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 syntax error because test is not initialized.
b) The program has a syntax error because x has not been initialized.
c) The program has a syntax error because you cannot create an object from the class that defines the object.
d) The program has a syntax error because Test does not have a default constructor.
e) The program has a runtime NullPointerException because test is null while executing test.x

解决方案 »

  1.   

    能帮忙讲的详细点吗?谢谢,可第一题他的答案却是选b
    a) The program has a compilation error because TempClass does not have a default constructor.
    b) The program has a compilation error because TempClass does not have a constructor with an int argument.
    c) The program compiles fine, but it does not run because class C is not public.
    d) The program compiles and runs fine.
    e) The program has a run-time error because TempClass does not have a default constructor.
      

  2.   


    第一题的构造器是想把传过来的参数 int j 的值赋值给 TempClass类得属性 i吧,那应该这样写
    class TempClass {
      int i;
      public TempClass(int j) {
      i = j; //这里不能写 int i = j; 这样的话这个i是临时变量,作用域在这个构造方法内,
              //根本就不能对属性 i进行赋值
      }
    }
    第二题,a syntax error 的意思是 语法错误,这个是没有语法错误的,所以编译的时候不会报错,但
    是在运行的时候会抛出一个java.lang.NullPointerException异常,也就是空指针异常。
    null表示不存在,Test test = null;表示声明了一个 Test 类型的引用变量 test,但是 test 没有指
    向任何一个对象。
      

  3.   

    第一题应该没问题,是不是代码贴错了。
    第二题,因为Test test = null;只是定义一个test变量,让它指向一个null对象
    所以
    a)不对,这里没有语法错误,而且=null也是一种初始化
    b)不对,成员变量不初始化,系统会自动初始化,所以也没有语法错误
    c)不对,不能从一个定义对象的类中生成对象,没这回事,但实例模式就是很好的例子,所以没有语法错误
    d)不对,类可以没有默认构造器,只要new的时候,参数正确就行,这里也没法发生new,所以不存在这个问题
    e)正确,因为test为null,也就是没有分配内存,所以不能访问它的属性或方法,所以会报空指针异常
      

  4.   

    b选项中说的是TempClass没有含int类型参数的构造器。实际上题目中的构造器是:
    public TempClass(int j),是含int参数的,这个构造器是覆盖掉无参构造器,所以
    TempClass temp = new TempClass(2)是能创建TempClass的对象的。综上:第一题没有错误。
      

  5.   

    第二个的话就不说了!  咱们来说说第一个!
    对象new完之后的形式应该是什么样的呢?  就是把构造给去掉,然后构造方法里面的参数会返回到对象里面,所以这里在构造里面又定义了个int i,本来这个类就有一个变量I了,这里又创建了一个,呵呵!  其实错误就是重复,编译是可以的,就是执行的时候出错了,编译器是知道这个错误的,但是没法表达,所以就会出现B选项的那个错误,它其实是想说同一个int类型的变量i已经存在了,就好像在项目中连续定义两个一样名字的变量一样!   如果你换成K就不会有这个错误了!
      

  6.   

    The program has a runtime NullPointerException because test is null while executing test.x这句话的意思是说 “由于test中x变量 你的程序有一个运行时发生的空指针异常”。不能运行呀
      

  7.   

    这个重复定义不造成错误,因为变量有自己的作用范围,虽然在方法内局部变量会把属性覆盖,可能会导致运行结果和期望值不一致,但是程序可以编译,可运行,所以答案应该是d(当然d中的find不是指结果一定正确,因为这里没有任何结果检测,只是说程序能正常运行)
      

  8.   

    这句话的意思是,程序会引起空指针运行期异常当它执行test.x的时候,因为test是null
      

  9.   

    我也是新人,误导了新人,呵呵 sorry  楼上的专家都喷我啦
    重新说说吧
    应该是 由于‘test.x’这句话,你的程序有一个运行时发生的空指针异常
    不能运行出结果就发生了运行时错误 
    由于你的Test未定义而引起的
    完全答案 接分 嘿嘿
      

  10.   

    第二题
    Test test = null 说明Test类对象都没有在内存中创建
     在这里test只是引用  只有new Test();才会在内存中创建对象