SCJP认证套题解析之二

类别:Java 点击:0 评论:0 推荐:

21、Which 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时需要进行强制类型转换,反之则不需要。

22、Given 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的错误在于在父类型的构造函数被调用前不能引用类的成员。构造方法是一个类对象实例化的起点(虽然严格来说首先执行的并不是构造方法的第一个语句,而是内存的分配),因此在构造方法中不能将成员作为参数引用。

 

23、Which 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.     局部变量在使用前必须被初始化。

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

 

24、public 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块中的内容无论是否发生异常都将被执行。

 

25、Given 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时退出循环执行循环后面的内容,而—操作符的规则是在变量右边的 -- 将先进行运算,然后才是使变量的值减一,而在变量左边的是先将变量的值减一再运算。

 

26、Which 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的正确性还有待官方的确定。

 

27、In 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.     类层次

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

 

28、Given 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在使用前没有。

 

29、Which 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.     在方法中定义的方法的参变量只要该对象被需要就一直存在。

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

 

30、A 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)值。

 

32、Which 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中的原话。

 

33、Given 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。

34、Given the following code:

    public class Person{

       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.

(a)

给出下面的代码:

哪些叙述是对的。

A.      编译时出错。

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

C.      输出0。

D.     输出null。

实例变量在类的一个实例构造时完成初始化,而且在类的静态方法中不能直接访问类的非静态成员而只能访问类成员(像上题中一样),类的普通方法可以访问类的所有成员和方法,而静态方法只能访问类的静态成员和方法,因为静态方法属于类,而普通方法及成员变量属于类的实例,类方法(静态方法)不能使用属于某个不确定的类的实例的方法和变量,在静态方法里面没有隐含的this,而普通方法有。

 

35、public class Parent {

     public int addValue( int a, int b) {

         int s;

         s = a+b;

         return s;

     }

   }

   class Child extends Parent {

  

   }

Which methods can be added into class Child?

A. int addValue( int a, int b ){// do something...}

B. public void addValue (){// do something...}

C. public int addValue( int a ){// do something...}

D. public int addValue( int a, int b )throws MyException {//do something...}

(bc)

题目:哪些方法可以加入类Child中。

此题涉及方法重载(overload),方法重写(override)以及类派生时方法重写的规则。方法重载的规则是:一、参数列表必须不同,个数的不同完全可以,如果个数相同则参数类型的不同不能引起歧意,例如int 和long,float和double就不能作为唯一的类型不同;二、返回值可以不同,但是不能是重载时唯一的不同点(这点和c++中不同,c++中返回类型必须一致)。方法重写发生在类继承时,子类可以重写一个父类中已有的方法,必须在返回类型和参数列表一样时才能说是重写,否则就是重载,java中方法重写的一个重要而且容易被忽略的规则是重写的方法的访问权限不能比被重写的方法的访问权限低!重写的另一个规则是重写的方法不能比被重写的方法抛弃(throws)更多种类的异常,其抛弃的异常只能少,或者是其子类,不能以抛弃异常的个数来判断种类,而应该是异常类层次结果上的种类。此题中答案a的错误就是重写的访问权限比被重写的方法的低,而b,c都属于重载,d的错误在于比被重写的方法抛弃了更多种类的异常。

 

36、A member variable defined in a class can be accessed only by the classes in the same package. Which modifier should be used to obtain the access control?

A. private

B. no modifier

C. public

D. protected

(b)

题目:一个类中定义的成员变量只能被同一包中的类访问。下面的哪些修饰符可以获得需要的访问控制。

参看前面的题目中的叙述。

 

37、A public member vairable called MAX_LENGTH which is int type, the value of the variable remains constant value 100. Use a short statement to define the variable.

A. public int MAX_LENGTH=100;

B. final int MAX_LENGTH=100;

C. final public int MAX_LENGTH=100;

D. public final int MAX_LENGTH=100.

(d)

题目:共有成员变量MAX_LENGTH是一个int型值,变量的值保持常数值100。使用一个短声明定义这个变量。

Java中共有变量使用public定义,常量变量使用final,另外注意的是修饰符的顺序,一个最完整的修饰是public static final int varial_a=100;这个顺序不能错,这和c++中也是不同的。而答案c恰恰错在修饰符的顺序上。

 

38、Which expressions are correct to declare an array of 10 String objects?

A. char str[];

B. char str[][];

C. String str[];

D. String str[10];

(c)

题目:哪些表达式是声明一个含有10个String对象的数组。

严格来说这个题目没有给出一个正确的答案,唯一比较正确的是c,而完全满足题目要求的应该是:String str[]=new String[10];

注意答案d的形式是不对的,这和c++也是不同的。

 

39、Which fragments are correct in Java source file?

A. package testpackage;

public class Test{//do something...}

B. import java.io.*;

package testpackage;

public class Test{// do something...}

C. import java.io.*;

class Person{// do something...}

public class Test{// do something...}

D. import java.io.*;

import java.awt.*;

public class Test{// do something...}

(acd)

题目:下面的那个java源文件代码片断是对的。

Java中的package语句必须是源文件中除去说明以外的第一条语句,导入包语句可以有几个,但是必须位于package语句之后,其它类定义之前,一个源文件中可以有几个类,但最多只能有一个是public的,如果有,则源文件的文件名必须和该类的类名相同。

 

40:

String s= "hello ";

String t = "hello";

char c[] = {'h','e','l','l','o'} ;

Which return true?

A. s.equals(t);

B. t.equals(c);

C. s==t;

D. t.equals(new String("hello"));

E. t==c.

(ad)

题目:哪些返回true。

这个在前面第10题的equals()方法和==操作符的讨论中论述过。==操作符比较的是操作符两端的操作数是否是同一个对象,而String的equals()方法比较的是两个String对象的内容是否一样,其参数是一个String对象时才有可能返回true,其它对象都返回假。

 

本文地址:http://com.8s8s.com/it/it18507.htm