网页功能: 加入收藏 设为首页 网站搜索  
SCJP认证套题解析之二
发表日期:2004-07-30作者:[转贴] 出处:  


21Which of the following assignment is not correct?

A. float f = 11.1;

B. double d = 5.3E12;

C. double d = 3.14159;

D. double d = 3.14D.

(a)

题目:下面的哪些赋值语句是对的。

浮点数的赋值是带有小数点的数字缺省是double型的,如果在浮点数后面加f或者F则是float,后面加d或者D则是double,科学计数法形式的浮点数也是double型的,而double的精度比float高,将一个高精度的double赋值给一个低精度的float时需要进行强制类型转换,反之则不需要。

22Given the uncompleted code of a class:

    class Person {

       String name, department;

       int age;

       public Person(String n){ name = n; }

       public Person(String n, int a){ name = n; age = a; }

       public Person(String n, String d, int a) {

         // doing the same as two arguments version of constructor

         // including assignment name=n,age=a

         department = d;

       }

     }

Which expression can be added at the "doing the same as..." part of the constructor?

A. Person(n,a);

B. this(Person(n,a));

C. this(n,a);

D. this(name,age).

(c)

题目:给出下面的不完整的类代码:

下面的哪些表达式可以加到构造方法中的"doing the same as..."处?

在同一个类的不同构造方法中调用该类的其它构造方法需要使用this(…)的形式,而且必须是在构造方法的第一行调用,这个和普通的方法重载调用的方式不同,普通的方法可以直接使用方法名加参数来调用,而且调用位置没有限制,因此答案A是不行的,B的语法就是错误的,D的错误在于在父类型的构造函数被调用前不能引用类的成员。构造方法是一个类对象实例化的起点(虽然严格来说首先执行的并不是构造方法的第一个语句,而是内存的分配),因此在构造方法中不能将成员作为参数引用。

 

23Which of the following statements about variables and their scopes are true?

 

A. Instance variables are member variables of a class.

B. Instance variables are declared with the static keyword.

C. Local variables defined inside a method are created when the method is executed.

D. Local variables must be initialized before they are used.

(acd)

题目:下面关于变量及其范围的陈述哪些是对的。

A.      实例变量是类的成员变量。

B.      实例变量用关键字static声明。

C.      在方法中定义的局部变量在该方法被执行时创建

D.     局部变量在使用前必须被初始化。

类中有几种变量,分别是:局部变量(英文可以为:localautomatic emporarystack variable)是定义在方法里的变量;实例变量(英文为:instance variable)是在方法外而在类声明内定义的变量,有时也叫成员变量;类变量(英文为:class variable)是用关键字static声明的实例变量,他们的生存期分别是:局部变量在定义该变量的方法被调用时被创建,而在该方法退出后被撤销;实例变量在使用new Xxxx()创建该类的实例时被创建,而其生存期和该类的实例对象的生存期相同;类变量在该类被加载时被创建,不一定要用new Xxxx()创建,所有该类的实例对象共享该类变量,其生存期是类的生存期。任何变量在使用前都必须初始化,但是需要指出的是局部变量必须显式初始化,而实例变量不必,原始类型的实例变量在该类的构造方法被调用时为它分配的缺省的值,整型是0,布尔型是false,而浮点型是0.0f,引用类型(类类型)的实例变量的缺省值是null(没有进行实际的初始化,对它的使用将引起NullPointException),类变量的规则和实例变量一样,不同的是类变量的初始化是在类被加载时。

 

24public void test() {

      try { oneMethod();

            System.out.println("condition 1");

      } catch (ArrayIndexOutOfBoundsException e) {

            System.out.println("condition 2");

     } catch(Exception e) {

           System.out.println("condition 3");

     } finally {

           System.out.println("finally");

     }

   }

Which will display if oneMethod run normally?

A. condition 1

B. condition 2

C. condition 3

D. finally

(ad)

题目:在oneMethod()方法运行正常的情况下将显示什么?

如果try块中的语句在执行时发生异常,则执行从该处中断而进入catch块,根据异常的类型进行匹配,最前面的优先进行匹配比较,只要该异常是catch中指定的异常的子类就匹配成功进而执行相应的catch中的内容,而finally块中的内容无论是否发生异常都将被执行。

 

25Given the following code:

public class Test {

   void printValue(int m){

     do { System.out.println("The value is"+m);

        }

     while( --m > 10 )

   }

   public static void main(String arg[]) {

     int i=10;

     Test t= new Test();

     t.printValue(i);

   }

}

Which will be output?

A. The value is 8

B. The value is 9

C. The value is 10

D. The value is 11

(c)

题目:给出下面的代码:

输出将是什么?

此题考察的是do… while循环和 -- 操作符的知识,do…while最少被执行一次,在执行完do中的内容后判断while中的条件是否为true,如果为true的话就再执行do中的内容,然后再进行判断,以此类推直到while的判断为false时退出循环执行循环后面的内容,而操作符的规则是在变量右边的 -- 将先进行运算,然后才是使变量的值减一,而在变量左边的是先将变量的值减一再运算。

 

26Which of the following statements about declaration are true?

A. Declaration of primitive types such as boolean, byte and so on does not allocate memory space for the variable.

B. Declaration of primitive types such as boolean, byte and so on allocates memory space for the variable.

C. Declaration of nonprimitive types such as String, Vector and so on does not allocate memory space for the object.

D. Declaration of nonprimitive types such as String, Vector ans so on allocates memory space for the object.

(ad)

题目:下面的有关声明的哪些叙述是对的。

A.      对原始数据类型例如boolean,byte的变量的声明不会为该变量分配内存空间。

B.      对原始数据类型例如boolean,byte的变量的声明将为之分配内存空间。

C.      非原始数据类型例如String,Vector的变量的声明不会为该对象分配内存。

D.     非原始数据类型例如String,Vector的变量的声明会为该对象分配内存。

对原始数据类型的变量的声明将为之分配内存并赋予一个缺省值,参见23题的叙述,而非原始数据类型的变量必须用new Xxxx()分配内存及初始化。但是严格来讲此题的答案有待确定,因为只有原始类型的实例变量和类变量的声明在类对象被创建/类被加载时完成内存的自动分配,而原始类型的局部变量必须显式初始化,从这点来看原始类型的局部变量没有被自动分配内存,SL275中只提出了非原始数据类型的变量必须使用new Xxxx()完成内存的分配而没有指出原始数据类型的变量是否在声明时即自动进行内存分配,而从局部变量不能在显式初始化前使用这点来看在声明时没有进行内存分配。因此答案a的正确性还有待官方的确定。

 

27In the Java API documentation which sections are included in a class document?

A. The description of the class and its purpose

B. A list of methods in its super class

C. A list of member variable

D. The class hierarchy

(acd)

题目:在Java API文档中下面的哪些部分被包括在内

A.      类及用途的描述

B.      父类的方法的列表

C.      成员变量的列表

D.     类层次

类文档的内容主要是:类层次、类及用途描述、成员变量列表、构造方法列表、成员方法列表、从类层次上继承的方法列表、成员变量的详细说明、构造方法详细说明、成员方法详细说明。

 

28Given the following code:

     1) public void modify() {

     2)   int i, j, k;

     3)   i = 100;

     4)   while ( i > 0 ) {

     5)      j = i * 2;

     6)      System.out.println (" The value of j is " + j );

     7)      k = k + 1;

     8)      i--;

     9)   }

     10) }

Which line might cause an error during compilation?

A. line 4

B. line 6

C. line 7

D. line 8

(c)

题目:给出下面的代码:

哪些行在编译时可能产生错误。

这个问题在前面有关变量的类型及其作用域的问题中讨论过,局部变量在使用前必须显式初始化,而代码中的变量k在使用前没有。

 

29Which of the following statements about variables and scope are true?

A. Local variables defined inside a method are destroyed when the method is exited.

B. Local variables are also called automatic variables.

C. Variables defined outside a method are created when the object is constructed.

D. A method parameter variable continues to exist for as long as the object is needed in which the method is defined.

(abc)

题目:下面有关变量及其作用域的陈述哪些是对的。

A.      在方法里面定义的局部变量在方法退出的时候被撤销。

B.      局部变量也叫自动变量。

C.      在方法外面定义的变量(译注:即实例变量)在对象被构造时创建。

D.     在方法中定义的方法的参变量只要该对象被需要就一直存在。

本题还是讨论变量的类型及作用域,参看前面的叙述。

 

30A class design requires that a member variable cannot be accessible directly outside the class. Which modifier should be used to obtain the access control?

A. public

B. no modifier

C. protected

D. private

(d)

题目:类的设计要求它的某个成员变量不能被外部类直接访问。应该使用下面的哪些修饰符获得需要的访问控制。

这个在前面也有叙述,java有四种访问类型,分别为:public,protected,default,private,其中public变量可以被所有的外部类访问,而pretected的可以被同一个包及该类的子类访问,default即没有任何修饰符的变量可以被同一个包中的类访问,而private变量只能在被该类内部被访问。题目中的外部类应该理解为除该类自身的所有其它类,因此只有使用private可以达到要求。

 

31Given the following code fragment:

    1) String str = null;

    2) if ((str != null) && (str.length() > 10)) {

    3)   System.out.println("more than 10");

    4) }

    5) else if ((str != null) & (str.length() < 5)) {

    6)        System.out.println("less than 5");

    7)      }

    8)      else { System.out.println("end"); }

Which line will cause error?

A. line 1

B. line 2

C. line 5

D. line 8

(c)

题目:给出下面的代码片断:

哪些行将导致错误。

此题需要将代码仔细看清楚,查询没有逻辑错误,if …else的使用没有问题,也没有拼写错误,错误在于第5行的“与”操作符的使用,逻辑操作符(logical operator)的“与”应该是&&,而&是位逻辑操作符(bitwise logical operator)的“与”,使用的对象不一样,逻辑操作符的“与”的左右操作数都应该是布尔型(logical boolan)的值,而位逻辑操作符的左右操作数都是整型(integral)值。

 

32Which statements about Java code security are true?

 

A. The bytecode verifier loads all classes needed for the execution of a program.

B. Executing code is performed by the runtime interpreter.

C. At runtime the bytecodes are loaded, checked and run in an interpreter.

D. The class loader adds security by separating the namespaces for the classes of the local file system from those imported from network sources.

(bcd)

题目:下面有关java代码安全性的叙述哪些是对的。

A.      字节码校验器加载查询执行需要的所有类。

B.      运行时解释器执行代码。

C.      在运行时,字节码被加载,验证然后在解释器里面运行。

D.     类加载器通过分离本机文件系统的类和从网络导入的类增加安全性。

SL275中描述的Java程序运行的过程是这样的:类加载器(class loader)加载程序运行所需要的所有类,它通过区分本机文件系统的类和网络系统导入的类增加安全性,这可以限制任何的特洛伊木马程序,因为本机类总是先被加载,一旦所有的类被加载完,执行文件的内存划分就固定了,在这个时候特定的内存地址被分配给对应的符号引用,查找表(lookuo table)也被建立,由于内存划分发生在运行时,解释器在受限制的代码区增加保护防止未授权的访问;然后字节码校验器(byte code verifier)进行校验,主要执行下面的检查:类符合JVM规范的类文件格式,没有违反访问限制,代码没有造成堆栈的上溢或者下溢,所有操作代码的参数类型都是正确的,没有非法的数据类型转换(例如将整型数转换成对象类型)发生;校验通过的字节码被解释器(interpreter)执行,解释器在必要时通过运行时系统执行对底层硬件的合适调用。后三个答案是SL275中的原话。

 

33Given the following code:

    public class Person{

       static int arr[] = new int[10];

       public static void main(String a[]) {

          System.out.println(arr[1];)

       }

    }

Which statement is correct?

A. When compilation some error will occur.

B. It is correct when compilation but will cause error when running.

C. The output is zero.

D. The output is null.

(c)

题目:给出下面的代码:

那个叙述是对的。

A.      编译时将发生错误。

B.      编译时正确但是运行时出错。

C.      输出为0

D.     输出为null

int型数组是类对象,它在类被加载时完成初始化,在前面题目中已经有叙述,由于是原始数据类型int,其初始值为0

34Given the following code:

我来说两句】 【加入收藏】 【返加顶部】 【打印本页】 【关闭窗口
中搜索 SCJP认证套题解析之二

本类热点文章
  Java读取文件中含有中文的解决办法
  Java读取文件中含有中文的解决办法
  简单加密/解密方法包装, 含encode(),de..
  EJB 3.0规范全新体验
  java简单的获取windows系统网卡mac地址
  让Java程序带着JRE一起上路
  抢先体验"野马"J2SE6.0
  Java连接各种数据库的实例
  Java连接各种数据库的实例
  JAVA的XML编程实例解析
  Java学习从入门到精通(附FAQ)
  新手必读:Java学习的捷径
最新分类信息我要发布 
最新招聘信息

关于我们 / 合作推广 / 给我留言 / 版权举报 / 意见建议 / 广告投放  
Copyright ©2003-2024 Lihuasoft.net webmaster(at)lihuasoft.net
网站编程QQ群   京ICP备05001064号 页面生成时间:0.01098