您的位置:首页 > 职场人生

JAVA程序员模拟考试题tk310-035V12.0(部分)中文翻译和解题分析

2007-06-20 12:43 423 查看
Section A

(1)
1.public class Test {
2.public static void main(String arg[]){
3.class Foo{
4.public int i = 3;
5.}
6.Object o = (Object)new Foo();
7.Foo foo = (Foo)o;
8.System.out.println(" i = " + foo.i);
9.}
10.}
What is the result?
A. i = 3
B. Compilation fails. 编译错误
C. A ClassCastException is thrown at line 6. 第6行抛出异常
D. A ClassCastException is thrown at line 7. 第7行抛出异常

Answer:A

将Foo对象向上塑形为object(on line 6),然后又把Foo向下塑形为Foo(on line 7),最后访问打印Foo中的public属性i.打印出i=3.

(2)
Which two cause a complier error?(Choose two)
哪两个可以引起编译器出错?
A. float[] = new float(3);
B. float f2[] = new float[];
C. float[] f1 = new float[3];
D. float f3[] = new float[3];
E. float f5[] = {1.0f, 2.0f, 2.0f};
F. float f4[] = new float[] {1.0f. 2.0f. 3.0f};

官方答案Answer:A,B
The F.statement is incorrect. The float numbers should be separated with commas and not dots.
F也是错误的。浮点数应该用","隔开而不是"."。
A答案没有变量。B答案没有指定数组维数。

(3)
int i = 1, j = 10;
do{
if(i++ > --j){
continue;
}
} while(i < 5);
System.out.println("i = " + i + " and j =" + j);
What is the result?
结果是什么?

A. i = 6 and j = 5
B. i = 5 and j = 5
C. i = 6 and j = 5
D. i = 5 and j = 6
E. i = 6 and j = 6

Answer: D

程序循环的经典问题,首先由if(i++ > --j){continue;}来看循环应该会无限的执行下去,但while(i < 5)限制了它的执行次数,再由i=1来看,循环执行执行了4次,所以i=1+4=5,j=10-4=6. D答案正确.

(4)
1. class text {
2. private Demo d;
3. void start() {
4. d = new Demo();
5. this.takeDemo(d);
6. }
7.
8. void takeDemo(Demo demo) {
9. demo = null;
10. demo = new Demo();
11. }
12. }

When is the Demo object, created on line 3 ,eligible for garbage collection?
第3行产生的Demo对象何时符合垃圾回收条件?

A. After line 5. 5行后
B. After line 9. 9行后
C. After the start() method completes. start()方法结束后
D. When the takeDemo() method completes. takeDemo()方法完成时
E. When the instance running this code is made eligible for garbage collection. 当代码中的实例执行的时候是垃圾回收最理想的情况。

Answer:E

这 道题算是比较有争议的,很多人选B。但是官方答案是E,我的想法是这样的,JAVA垃圾回收机制是通过后台线程对内存分配情况进行跟踪来实现的,而且是个 低优先级线程,所以当你把demo置空时,JAVA回收器并不一定立即就回收它,但是当代码中的实例都被执行的时候垃圾回收器就一定要将它收回了。

QUESTION NO: 5

Given:
1. interface Animal {
2. void soundOff();
3. }
4.
5. class Elephant implements Animal {
6. public void soundOff() {
7. System.out.println(“Trumpet”);
8. }
9. }
10.
11. class Lion implements Animal {
12. public void soundOff() {
13. System.out.println(“Roar”);
14. }
15. }
16.
17. class Alpha1 {
18. static Animal get( String choice ) {
19. if ( choice.equalsIgnoreCase( “meat eater” )) {
20. return new Lion();
21. } else {
22. return new Elephant();
23. }
24. }
25. }
Which compiles?
哪个能通过编译?
A. new Animal().soundOff();
B. Elephant e = new Alpha1();
C. Lion 1 = Alpha.get(“meat eater”);
D. new Alpha1().get(“veggie”).soundOff();

Answer: D
A答案,Animal是个接口是抽象的,它的方法无法调用。
B答案,对象与事例互不符合。
C答案,Lion 1 = Alpha.get(“meat eater”);这是什么东西?说它怎么错就怎么错!

QUESTION NO: 6
Which statement is true?
下面哪种说法是正确的?

A. Memory is reclaimed by calling Runtime.gc(). 调用Runtime.gc()方法时内存会被回收。
B. Objects are not collected if they are accessible from live threads. 存活的线程中的对象是不会被回收的。
C. Objects that have finalize() methods are never garbage collected. 对象调用finalize()方法绝对不被垃圾回收。
D. Objects that have finalize() methods always have their finalize() methods called before
the program ends. 类的finalize()方法总是在程序结束以前被调用。
E. An OutOfMemory error is only thrown if a single block of memory cannot be found
that is large enough for a particular requirement. 只有在一块独立的内存区域无法找到的情况下才会抛出内存溢出异常。

Answer: B
A答案,说法不正确,调用Runtime.gc()的时候只能是通知、建议虚拟机调用垃圾回收器来回收已被废弃的对象。
B答案正确。
C答案,显然错误,finalize()方法是用于清除对象(注意我说是清除不是销毁!)的。
D答案,这个答案我不确定,但是程序结束前finalize()方法不是一定被调用的,从这个角度看,D选项的说法是不正确的。
E答案,导致这个错误的原因有很多,例如我们在写程序的过程中有时候会犯一种逻辑错误,程序会无限的申请内存,并且没有可用的内存提供给垃圾回收器,导致Java 虚拟机无法分配一个对象,这时候也会抛出内存错误!

QUESTION NO: 7
Given:
1. class A {
2. A() { }
3. }
4.
5. class B extends A {
6. }
Which two statements are true? (Choose two)
哪两个说法是正确的?
A. Class B’s constructor is public. 类B的构造方法是公有的。
B. Class B’s constructor has no arguments. 类B的构造函数没有参数。
C. Class B’s constructor includes a call to this().
D. Class B’s constructor includes a call to super().

Answer: B, D
A,每个类必须都定义自己的构造函数,不能通过继承完成初始化,所以B的构造方法谈不到公有的说法。
B,当然没有参数了,正确。
C,子类调用父类构造方法需要使用super()语句。
D,正确。

QUESTION NO: 8
Given:
11. int i = 1,j = 10;
12. do {
13. if(i>j) {
14. break;
15. }
16. j--;
17. } while (++i <5);
18. System.out.println(“i =” +i+” and j = “+j);
What is the result?
答案是什么?
A. i = 6 and j = 5
B. i = 5 and j = 5
C. i = 6 and j = 4
D. i = 5 and j = 6
E. i = 6 and j = 6

Answer: D
此题类似第(3)题,只需要判断循环次数即可。

QUESTION NO: 9
Which statement is true?
哪一个说法是正确的?

A. Assertions can be enabled or disabled on a class-by-class basis. 运行时针对每一个类都可以开启或者关闭这个类中的assert语句
B. Conditional compilation is used to allow tested classes to run at full speed. 有条件的编辑常被用于允许测试的类全速运行。
C. Assertions are appropriate for checking the validity of arguments in a method. 断言适合检查方法参数的有效性。
D. The programmer can choose to execute a return statement or to throw an exception if
an assertion fails. 如果一个断言的检查失败程序员可以选择执行一个返回语句或者抛出一个异常。

Answer: A

A答案正确。
B答案,很难翻译,并且也不是很好理解,希望高手能给予指点。
C答案,assertions通常用来检查一些关键的值,而且不能让assert语句去处理一些程序必要的流程,因为你不知道别人在执行程序时是否会启用assertions功能。
D答案,当assert检查结果为false时,只会产生AssertionError.当assertion检查失败产生其他异常时,assert语句就会被中断,不会被执行下去,无论怎么样这都是程序员无法选择的。

Answer: A

QUESTION NO: 10
You want a class to have access to members of another class in the same package. Which
is the most restrictive access that accomplishes this objective?
你在一个包中让一个类去访问另一个类的成员。哪一种最能达到限制访问的目的?

A. public
B. private
C. protected
D. transient
E. default access

Answer: E
A答案,public是java中对访问限制最宽的修饰符,被public修饰的类、属性、方法不仅可以跨类访问,而且允许跨包访问。
B答案,private是java中对访问限制最窄的修饰符,被private修饰的属性、方法只能被该类的对象访问,子类无法访问,当然也就不能跨包。
C答案,protected的访问范围在public和private之间,protected修饰的类、属性、方法只能被类本身的方法及子类的方法访问,即使子类在不同的包中也可以访问。
D答案,transient只能对属性进行修饰。
E答案,是默认修饰权限,它界于private和protected,又因为我们要求去访问另一个类的成员,所以最能限制的应该是它了。

QUESTION NO: 11
Given:
11. int x = 3;
12. int y = 1;
13. if (x = y) {
14. System.out.println(“x = “ + x);
15. }
What is the result?
哪一个是正确的结果?
A. x = 1
B. x = 3
C. Compilation fails. 编译错误。
D. The code runs with no output. 代码执行没有输出。
E. An exception is thrown at runtime. 一个运行期异常被抛出。

Answer: C
在if(x=y)中的参数应该是BOOLEAN类型的,而x=y是一个int类型的赋值语句,显然类型不兼容,产生编译错误.

QUESTION NO: 12
Given:
1. public class Test {
2. public static void aMethod() throws Exception {
3. try {
4. throw new Exception();
5. } finally {
6. System.out.println(“finally”);
7. }
8. }
9. public static void main(String args[]) {
10. try {
11. aMethod();
12. } catch (Exception e) {
13. System.out.println(“exception”);
14. }
15. System.out.println(“finished”);
16. }
17. }
What is the result?
程序的运行结果是什么?

A. finally
B. exception
finished
C. finally
exception
finished
D. Compilation fails.

Answer: C
在try -catch-finally语句结构中,无论try中是否有异常,也不管是否catch到了异常,finally语句的块一定要被执行,所以此题输出一 定要有finally,排除B。在main方法中System.out.println(“finished”);语句一定要被执行,所以结果中也要包含 finished,所以排除A。由于程序无编译错误所以排除D,正确答案就是C选。

------此文写于2006年2月份,有不准确的地方欢迎大家和我一起讨论 by:yxyhack
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: