首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 计算机考试 > 认证考试 > JAVA认证 >

SCJP认证套题解析之七

2008-10-05 
31、given the following code fragment:    1) string str null    2) if ((str ...

31、given 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,而普通方法有。

输入Java uridemo1命令后,列表1的输出结果如下:

query://jeff@books.com:9000/public/manuals/appliances?stove#ge:

authority = jeff@books.com:9000

fragment = ge

host = books.com

path = /public/manuals/appliances

port = 9000

query = stove

scheme = query

//jeff@books.com:9000/public/manuals/appliances?stove

user info = jeff

uri is absolute: true

uri is opaque: false

上面的输出显示该uri是绝对的,因为它指定了一个大纲(query),并且该uri是分层的,因为query后面有/符号。

技巧

你应该调用uri的compareto(object o)和equals(object o)来决定uri的次序(为了排序目的)和等同性。你可以参考sdk文档,查阅这些方法的更多信息。

uri类支持基本的uri操作,包括标准化(normalization)、分解(resolution)和相对化(relativization)。标准化是通过uri的normalize()方法支持的。调用normalize()时,它返回对新uri对象的引用,该对象包含调用的uri对象的uri的标准的表现。

列表2演示了normalize()方法。它把uri作为程序的唯一的参数,uridemo2打印出标准的相等的uri。

列表2: uridemo2.java

// uridemo2.java

import java.net.*;

class uridemo2

{

ublic static void main (string [] args) throws exception

{

if (args.length != 1)

{

system.err.println ('usage: java uridemo2 uri');

return;

}

uri uri = new uri (args [0]);

system.out.println ('normalized uri = '

uri.normalize ().tostring ());

}