每天几条java(4)——补周四

Question: 11
Given:
10. package com.sun.scjp;
11. public class Geodetics {
12. public static final double DIAMETER = 12756.32; // kilometers
13. }
Which two correctly access the DIAMETER member of the Geodetics class? (Choose two.)
A. import com.sun.scjp.Geodetics;
public class TerraCarta {
public double halfway()
{ return Geodetics.DIAMETER/2.0; }
B. import static com.sun.scjp.Geodetics;
public class TerraCarta{
public double halfway() { return DIAMETER/2.0; } }
C. import static com.sun.scjp.Geodetics.*;
public class TerraCarta {
public double halfway() { return DIAMETER/2.0; } }
D. package com.sun.scjp;
public class TerraCarta {
public double halfway() { return DIAMETER/2.0; } }

Answer: A, C

关于importimport static

静态导入是JDK1.5中的新特性。一般我们导入一个类都用 import com.....ClassName;而静态导入是这样:import static com.....ClassName.*;这里的多了个static,还有就是类名ClassName后面多了个 .* ,意思是导入这个类里的静态方法。当然,也可以只导入某个静态方法,只要把 .* 换成静态方法名就行了。然后在这个类中,就可以直接用方法名调用静态方法,而不必用ClassName.方法名 的方式来调用。

 

这种方法的好处就是可以简化一些操作,例如打印操作System.out.println(...);就可以将其写入一个静态方法print(...),在使用时直接print(...)就可以了。

 

但是这种方法建议在有很多重复调用的时候使用,如果仅有一到两次调用,不如直接写来的方便

 

 



Question: 12
Given:
10. public class Bar {
11. static void foo( int... x ) {
12. // insert code here
13. }
14. }
Which two code fragments, inserted independently at line 12, will allow the class to compile? (Choose two.)
A. foreach( x ) System.out.println(z);
B. for( int z : x ) System.out.println(z);
C. while( x.hasNext() ) System.out.println( x.next() );
D. for( int i=0; i< x.length; i++ ) System.out.println(x[i]);

Answer: B, D

 

foreach并不是一个关键字,习惯上将这种特殊的for语句格式称之为“foreach”语句。从英文字面意思理解foreach也就是“for 每一个”的意思。实际上也就是这个意思。

数组没有hasNext

B1.5(都老掉牙了)的特性foreach

 



Question: 13
Given:
1. public class Plant {
2. private String name;
3. public Plant(String name) { this.name = name; }
4. public String getName() { return name; }
5. }
1. public class Tree extends Plant {
2. public void growFruit() { }
3. public void dropLeaves() { }
4. }
Which statement is true?
A. The code will compile without changes.
B. The code will compile if public Tree() { Plant(); } is added to the Tree class.
C. The code will compile if public Plant() { Tree(); } is added to the Plant class.
D. The code will compile if public Plant() { this("fern"); } is added to the Plant class.
E. The code will compile if public Plant() { Plant("fern"); } is added to the Plant class.

Answer: D

子类必须调用父类的构造方法,如果父类的构造函数没有无参构造函数,则我们可以不在子类调用父类的构造方法,默认有一个super();

但父类所有构造方法都有参,那么你只能在子类中写一个来调用父类的构造方法。

构造方法应该用this()进行调用,而普通方法则是this.method()这好像在Q10的内部类说过。

 



Question: 14
Which two classes correctly implement both the java.lang.Runnable and the
java.lang.Clonable interfaces? (Choose two.)
A. public class Session
implements Runnable, Clonable {
public void run();
public Object clone();
}
B. public class Session
extends Runnable, Clonable {
public void run() { /* do something */ }
public Object clone() { /* make a copy */ }
C. public class Session
implements Runnable, Clonable {
public void run() { /* do something */ }
public Object clone() { /* make a copy */ }
D. public abstract class Session
implements Runnable, Clonable {
public void run() { /* do something */ }
public Object clone() { /*make a copy */ }
E. public class Session
implements Runnable, implements Clonable {
public void run() { /* do something */ }
public Object clone() { /* make a copy */ }

Answer: C, D

接口可以实现多个,只要一个implements,那么排除BE,另外只有在接口里面有方法的声明,实现接口(implements了那个接口)就必须实现那个方法,就是有实现语句。

最后抽象类也是可以实现接口的,所以A错,CD对。

 



Question: 15
Given:
1. public class Threads2 implements Runnable {
2.
3. public void run() {
4. System.out.println("run.");
5. throw new RuntimeException("Problem");
6. }
7. public static void main(String[] args) {
8. Thread t = new Thread(new Threads2());
9. t.start();
10. System.out.println("End of method.");
11. }
12. }
Which two can be results? (Choose two.)
A. java.lang.RuntimeException: Problem 
B. run.
java.lang.RuntimeException: Problem
C. End of method. 
java.lang.RuntimeException: Problem
D. End of method.
run.
java.lang.RuntimeException: Problem
E. run.
java.lang.RuntimeException: Problem
End of method.

Answer: D, E

……这题,好像前几天比赛时候告诉过师妹,话说那题好像改过的吧

我也把题放一下,再一起分析。

 

分析:

我们可以分为这几项:run problem end...

然后run problem 肯定了一个先后顺序,因为他们是一个线程的,但是它只是个普通线程,不是守护线程,也没有join,那么,另一个线程,也就是main线程的发生时间可以在其前,其间,其后。

并且exception并不会中断程序,只会中断线程。

所以很容易知道,前面的是DE,后面那题ACF

 

posted @ 2014-04-18 16:17  Fresher_Z  阅读(229)  评论(0编辑  收藏  举报