我思故我强

第一部分:声明和访问控制(练习题)

第一部分:声明和访问控制

  • 知道怎么样声明、创建和初始化各种类型的数组
  • 知道怎么样声明类、内部类、方法、成员变量、静态变量和方法内部变量,并会使用合法的修饰符(如public,final,static,abstract,等等)。了解这些修饰符单独使用或组合使用的含义,并且知道它们对于包作用域的影响。
  • 了解类的构造器(constructor)
  • 给定一个方法,能判断它的合法返回类型

§1.1.1    

In the following pieces of code, A and D will compile without any error. True/False?

A: StringBuffer sb1 = "abcd";

B: Boolean b = new Boolean("abcd");

C: byte b = 255;

D: int x = 0x1234;

E: float fl = 1.2;

 

Choices:

A. True

B. False

―――――――――――――――――――――――――――――――

答案 B. The code segments B and D will compile without any error. A is not a valid way to construct a StringBuffer, you need to creat a StringBuffer object using "new". B is a valid construction of a Boolean (any string other than "true" or "false" to the Boolean constructor will result in a Boolean with a value of "false"). C will fail to compile because the valid range for a byte is -128 to +127 (ie, 8 bits,signed). D is correct, 0x1234 is the hexadecimal representation in java. E fails to compile because the compiler interprets 1.2 as a double being assigned to a float (down-casting), which is not valid. You either need an explicit cast (as in "(float)1.2") or "1.2f", to indicate a float.

 

 

 

§1.1.2      

What will be the result of executing the following code?

Given that Test1 is a class.

1. Test1[] t1 = new Test1[10];

2. Test1[][] t2 = new Test1[5][];

3. if (t1[0] == null)

4. {

5.t2[0] = new Test1[10]

6.t2[1] = new Test1[10]

7.t2[2] = new Test1[10]

8.t2[3] = new Test1[10]

9.t2[4] = new Test1[10]

10. }

11. System.out.println(t1[0]);

12. System.out.println(t2[1][0]);

Choices:

a. The code will not compile because the array t2 is not initialized in an unconditional statement before use.

b. The code will compile but a runtime exception will be thrown at line 12.

c. The code will compile but a runtime exception will be thrown at line 11.

d. None of these.

―――――――――――――――――――――

D is correct. Though we cannot use local variables without initializing them (compilation error), there is an exception to it. In case of arrays initialization is supposed to be complete when we specify the leftmost dimension of the array. The problem occurs at runtime if we try to access an element of the array which has not been initialized (specification of size). In the question above the array t2 is initialized before use, therefore there will be no problem at runtime also and the lines 11 and 12 will both print null.

 

§1.1.3      

Which declarations of identifiers are legal?

 A. $persons

  B. TwoUsers

  C. *point

  D. this

    E. _endline

  

  答案 A,B,E

  

  解析 Java的标识符可以以一个Unicode字符,下滑线(_),美元符($)开始,后续字

符可以是前面的符号和数字,没有长度限制,大小写敏感,不能是保留字。

 

§1.1.4      

Which of the following answer is correct to express the value 8 in octal number?

  A. 010

  B. 0x10

  C. 08

  D. 0x8

 

  翻译

  下面的哪些答案可以用以表示八进制值8。

  答案 A

 

  解析  八进制值以0开头,以0x开头的为十六进制值,八进制中不能出现数字8,最大只有7。

 

§1.1.5      

Which are not Java keywords?

  A. TRUE

  B. sizeof

  C. const

  D. super

  E. void

 

  翻译

  哪些不是Java关键字。

 

  答案 A,B

 

  解析

   A:不是,Java中有true,但是这也不是关键字而是字面量(literal)。

 B:不是,Java中不需要这个操作符,所有的类型(原始类型)的大小都是固定的。

 C、D、E都是,需要说明的是const是java中未被使用的关键字。

 

§1.1.6      

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

 

§1.1.7      

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

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

 

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

 

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

 

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

 

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

§1.1.8      

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

 

§1.1.9      

Which of the following statements are legal?

  A. long l = 4990;

  B. int i = 4L;

  C. float f = 1.1;

  D. double d = 34.4;

  E. double t = 0.9F.

----------------------------

答案 ADE

 

posted on 2009-10-16 11:37 李云泽 阅读(272) 评论(0)  编辑  收藏 所属分类: 面试笔试相关的SCJP认证学习


只有注册用户登录后才能发表评论。


网站导航: