Tested
Compilation fails.
The code runs with no output.
An exception is thrown at runtime.
第1题:
interface A{
int x = 0;
}
class B{
int x =1;
}
class C extends B implements A {
public void pX(){
System.out.println(x);
}
public static void main(String[] args) {
new C().pX();
}
}
错误。在编译时会发生错误(错误描述不同的JVM 有不同的信息,意思就是未明确的
x 调用,两个x 都匹配(就象在同时import java.util 和java.sql 两个包时直接声明Date 一样)。
对于父类的变量,可以用super.x 来明确,而接口的属性默认隐含为 public static final.所以可
以通过A.x 来明确。
第2题:
10. class Foo { 11. static void alpha() { /* more code here */ } 12. void beta() { /* more code here */ } 13. } Which two are true?()
第3题:
下列程序的输出结果是 interface Inter{ public final static int A=100; } class My implements Inter{ public static void main (String args[ ]) {System.out.println(A) ; }
A.100
B.0
C.A
D.程序有错误
第4题:
interface Animal { void soundOff(); } class Elephant implements Animal { public void soundOff() { System.out.println(“Trumpet”); } } class Lion implements Animal { public void soundOff() { System.out.println(“Roar”); } } class Alpha1 { static Animal get( String choice ) { if ( choice.equalsIgnoreCase( “meat eater” )) { return new Lion(); } else { return new Elephant(); } } } Which compiles?()
第5题:
Which declarations will allow a class to be started as a standalone program?()
第6题:
1. import java.util.*; 2. class SubGen { 3. public static void main(String [] args) { 4. //insert code here 5. } 6. } class Alpha { } class Beta extends Alpha { } class Gamma extends Beta { } 和四段代码片段: s1. ArrayList〈? extends Alpha〉 list1 = new ArrayList〈Gamma〉(); s2. ArrayList〈Alpha〉 list2 = new ArrayList〈? extends Alpha〉(); s3. ArrayList〈? extends Alpha〉 list3 = new ArrayList〈? extends Beta〉(); s4. ArrayList〈? extends Beta〉 list4 = new ArrayList〈Gamma〉(); ArrayList〈? extends Alpha〉 list5 = list4; 哪些片段分别插入到第4行,可允许代码编译?()
第7题:
class Beta { public static void main(String [] args) { Integer x = new Integer(6) * 7; if (x != 42) { System.out.print("42 "); } else if (x 〈 new Integer(44-1)) { System.out.println("less"); } else { System.out.print("done"); } } } 结果是什么?()
第8题:
public class Something {
public static void main(String[] args) {
Something s = new Something();
System.out.println("s.doSomething() returns " + doSomething());
}
public String doSomething() {
return "Do something ...";
}
}
看上去很完美。
错。看上去在main 里call doSomething 没有什么问题,毕竟两个methods 都在同一个
class 里。但仔细看,main 是static 的。static method 不能直接call non-static methods。可改
成"System.out.println("s.doSomething() returns " + s.doSomething());"。同理,static method 不
能访问non-static instant variable。
第9题:
class Mineral { static String shiny() { return "1"; } } class Granite extends Mineral { public static void main(String [] args) { String s = shiny() + getShiny(); s = s + super.shiny(); System.out.println(s); } static String getShiny() { return shiny(); } } 结果为:()
第10题:
1. class Alpha { void m1() {} } 2. class Beta extends Alpha { void m2() { } } 3. class Gamma extends Beta { } 4. 5. class GreekTest { 6. public static void main(String [] args) { 7. a Alpha [] a = {new Alpha(), new Beta(), new Gamma() }; 8. for(Alpha a2 : a) { 9. a2.m1(); 10. if (a2 instanceof Beta || a2 instanceof Gamma) 11. //insert code here 12. } 13. } 14. } 哪一行代码插入到第11行,将编译但是会在运行时产生异常?()