150) 1. public class KungFu {
public static void main(String[] args) {
Integer x = 400;
Integer y = x;
x++;
StringBuilder sb1 = new StringBuilder(“123”);
StringBuilder sb2 = sb1;
sb1.append(“5″);
System.out.println((x==y) + ” ” + (sb1==sb2)); 10. } 11. }运行结果是( )
a) true true
b) false true
c) true false
d) false false
e) Compilation fails.
f) An exception is thrown at runtime.
151) 11. class Converter {
public static void main(String[] args) {
Integer i = args[0];
int j = 12;
System.out.println(“It is ” + (j==i) + ” that j==i.”); 16. } 17. }
当运行java Converter 12命令时,会有什么结果( )
a) It is true that j==i
b) It is false that j==i.
c) An exception is thrown at runtime.
d) Compilation fails because of an error in line 13.
152) 10. class Foo {
static void alpha() { /* more code here */ }
void beta() { /* more code here */ }
}下边的哪句描述是正确的,选择2个()
a) Foo.beta() is a valid invocation of beta().
b) Foo.alpha() is a valid invocation of alpha().
c) Method beta() can directly call method alpha().
d) Method alpha() can directly call method beta().
153) public class A{
public String doit(int x,int y){
return “a”;
}
public String doit(int…vals){
return “b”;
}
}
在主方法里调用
A a = new A();
System.out.println(a.doit(4, 5)); 运行结果是( )
a) Line 26 prints “a” to System.out.
b) Line 26 prints “b” to System.out.
c) An exception is thrown at line 26 at runtime.
d) Compilation of class A will fail due to an error in line 6.
154) 下边哪句代码是正确的初始化了一个静态整型数组,选择2个( )
a) static final int[] a = { 100,200 };
b) static final int[] a; static { a=new int[2]; a[0]=100; a[1]=200; }
c) static final int[] a = new int[2]{ 100,200 };
d) static final int[] a;
static void init() { a = new int[3]; a[0]=100; a[1]=200; }
155) 1. public class Plant {
private String name;
public Plant(String name) { this.name = name; }
public String getName() { return name; } 5. }
public class Tree extends Plant {
public void growFruit() { }
public void dropLeaves() { } 4. }下列哪句是正确的( )
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.
156) public class GoTest{
public static void main(String[] args){
Sente a = new Sente(); a.go();
Goban b = new Goban(); b.go();
Stone c = new Stone(); c.go();
}
}
class Sente implements Go{
public void go(){
System.out.println(“go in Sente”);
}
}
class Goban extends Sente{
public void go(){
System.out.println(“go in Goban”);
}
}
class Stone extends Goban implements Go{}
interface Go{public void go();}
运行结果是( )
a) go in Goban go in Sente go in Sente
b) go in Sente go in Sente go in Goban
c) go in Sente go in Goban go in Goban
d) go in Goban go in Goban go in Sente
157) 哪两个类可以正确的实现java.lang.Runnable 和java.lang. Cloneable接口选择2个( )
a) public class Session implements Runnable, Cloneable { public void run(); public Object clone(); }
b) public class Session extends Runnable, Cloneable { public void run() { /* do something */ } public Object clone() { /* make a copy */ }
c) public class Session implements Runnable, Cloneable { public void run() { /* do something */ } public Object clone() { /* make a copy */ }
d) public abstract class Session implements Runnable, Cloneable { public void run() { /* do something */ } public Object clone() { /*make a copy */ }
e) public class Session implements Runnable, implements Cloneable { public void run() { /* do something */ } public Object clone() { /* make a copy */ }
158) 11. class Mud { 12. // insert code here
System.out.println(“hi”); 14. } 15. }
And the following five fragments:
public static void main(String…a) {
public static void main(String.* a) {
public static void main(String… a) {
public static void main(String[]… a) {
public static void main(String…[] a) {
上边有几个方法可以放到第12行,是程序正确编译,( )
a) 0 b) 1 c) 2 d) 3 e) 4 f) 5
159) 11. public interface A111 {
String s = “yo”;
public void method1(); 14. }
interface B { }
interface C extends A111, B {
public void method1();
public void method1(int x); 23. }运行结果是( )
a) Compilation succeeds.
b) Compilation fails due to multiple errors.
c) Compilation fails due to an error only on line 20.
d) Compilation fails due to an error only on line 21.
e) Compilation fails due to an error only on line 22
160) interface Foo{int bar();}
public class Beta{
class A implements Foo{
public int bar(){return 1;}
}
public int fubar(Foo foo){
return foo.bar();
}
public void testFoo(){
class A implements Foo{
public int bar(){return 2;}
}
System.out.println(fubar(new A()));
}
public static void main(String[] args){
new Beta().testFoo();
}
}下边哪个结果是正确的,选择3个( )
a) Compilation fails.
b) The code compiles and the output is 2.
c) If lines 16, 17 and 18 were removed, compilation would fail.
d) If lines 24, 25 and 26 were removed, compilation would fail.
e) If lines 16, 17 and 18 were removed, the code would compile and the output would be 2.
f) If lines 24, 25 and 26 were removed, the code would compile and the output would be 1.
161) 11. class Alpha {
public void foo() { System.out.print(“Afoo “); } 13. }
public class Beta extends Alpha {
public void foo() { System.out.print(“Bfoo “); }
public static void main(String[] args) {
Alpha a = new Beta();
Beta b = (Beta)a;
a.foo(); 20. b.foo(); 21. } 22. }运行结果是( )
a) Afoo Afoo
b) Afoo Bfoo
c) Bfoo Afoo
d) Bfoo Bfoo
e) Compilation fails.
f) An exception is thrown at runtime.
162) 1. public class TestOne {
public static void main (String[] args) throws Exception {
Thread.sleep(3000);
System.out.println(“sleep”); 5. } 6. }运行结果是( )
a) Compilation fails.
b) An exception is thrown at runtime.
c) The code executes normally and prints “sleep”
d) The code executes normally, but nothing is printed.
163) 1. public class Threads4 {
public static void main (String[] args) {
new Threads4().go(); 4. }
public void go() {
Runnable r = new Runnable() {
public void run() {
System.out.print(“foo”); 9. } 10. };
Thread t = new Thread(r);
t.start(); 13. t.start(); 14. } 15. } 运行结果是( )
a) Compilation fails.
b) An exception is thrown at runtime
c) The code executes normally and prints “foo”
d) The code executes normally, but nothing is printed
164) 1. public class Threads3 implements Runnable {
public void run() {
System.out.print(“running”); 4. }
public static void main(String[] args) {
Thread t = new Thread(new Threads3());
t.run(); 8. t.run(); 9. t.start(); 10. } 11. }运行结果是( )
a) Compilation fails.
b) An exception is thrown at runtime.
c) The code executes and prints “running”.
d) The code executes and prints “runningrunning”
e) The code executes and prints “runningrunningrunning”
165) public class NamedCounter
{ private final String name;
private int count;
public NamedCounter(String name) { this.name = name;}
public String getName() { return name; }
public void increment() { count++; }
public int getCount() { return count; }
public void reset() { count = 0; }
哪些改变可以使这个类被多线程安全的访问,选择3个( )
a) declare reset() using the synchronized keyword
b) declare getName() using the synchronized keyword
c) declare getCount() using the synchronized keyword
d) declare the constructor using the synchronized keyword
e) declare increment() using the synchronized keyword
166) 1. public class TestSeven extends Thread {
private static int x;
public synchronized void doThings() {
int current = x;
current++;
x = current; 7. }
public void run() {
doThings(); 10. } 11.} 下边哪句是正确的( )
a) Compilation fails.
b) An exception is thrown at runtime.
c) Synchronizing the run() method would make the class thread-safe.
d) The data in variable “x” are protected from concurrent access problems.
e) Declaring the doThings() method as static would make the class thread-safe.
f) Wrapping the statements within doThings() in a synchronized(new Object()) { } block would make the class thread-safe.
167) 11. public class Yikes { 12.
public static void go(Long n) {System.out.print(“Long “);}
public static void go(Short n) {System.out.print(“Short “);}
public static void go(int n) {System.out.print(“int “);}
public static void main(String [] args) {
short y = 6;
long z = 7;
go(y); 20. go(z); 21. } 22. } 运行结果是( )
a) int Long
b) Short Long
c) Compilation fails.
d) An exception is thrown at runtime.
168) 12. Date date = new Date();
df.setLocale(Locale.ITALY);
String s = df.format(date);
df是一个DateFormat的对象,并且已经被实例化,当这段代码在
December 14, 2000运行的时候,结果是什么( )
a) The value of s is 14-dic-2000.
b) The value of s is Dec 14, 2000.
c) An exception is thrown at runtime.
d) Compilation fails because of an error in line 13
169) 当我们用StringBuilder 对象替换StringBuffer 时,哪两种情况是不安全的,选择2个( )
a) When using versions of Java technology earlier than 5.0.
b) When sharing a StringBuffer among multiple threads.
c) When using the java.io class StringBufferInputStream.
d) When you plan to reuse the StringBuffer to build more than one string.
170) 11. String test = “This is a test”;
String[] tokens = test.split(“s”);
System.out.println(tokens.length); 代码的运行结果是( )
a) 0
b) 1
c) 4
d) Compilation fails.
e) An exception is thrown at runtime.
171) 10. import java.io.*;
class Animal {
Animal() { System.out.print(“a”); } 13. }
class Dog extends Animal implements Serializable {
Dog() { System.out.print(“d”); } 16. }
public class Beagle extends Dog { }
如果类Beagle的一个实例被创建,经过序列化和反序列化,程序结果是( )
a) ad b) ada c) add d) adad
172) 11. double input = 314159.26;
NumberFormat nf = NumberFormat.getInstance(Locale.ITALIAN);
String b;
//insert code here
哪句代码放到14行,可以保证程序输出结果为b=314.159,26( )
a) b = nf.parse( input );
b) b = nf.format( input );
c) b = nf.equals( input );
d) b = nf.parseObject( input );
173) 11. class Animal { public String noise() { return “peep”; } } . class Dog extends Animal {
public String noise() { return “bark”; } 14. }
class Cat extends Animal {
public String noise() { return “meow”; } 17. } …
Animal animal = new Dog();
Cat cat = (Cat)animal;
System.out.println(cat.noise()); 运行结果是( )
a) peep
b) bark
c) meow
d) Compilation fails
e) An exception is thrown at runtime.
174) 10. abstract class A {
abstract void a1();
void a2() { } 13. }
class B extends A {
void a1() { }
void a2() { } 17. }
class C extends B { void c1() { } } and:
A x = new B(); C y = new C(); A z = new C();
下边代码哪些是正确的方法调用,选择4个( )
a) x.a2();
b) z.a2();
c) z.c1();
d) z.a1();
e) y.c1();
f) x.a1();
175) 3. class Employee {
String name; double baseSalary;
Employee(String name, double baseSalary) {
. this.name = name;
this.baseSalary = baseSalary; 8. } 9. }
public class SalesPerson extends Employee {
double commission;
public SalesPerson(String name, double baseSalary, double commission) {
// insert code here 14. } 15. }
哪些代码放到第13行,程序可以运行,选择2个( )
a) super(name, baseSalary);
b) this.commission = commission;
c) super(); this.commission = commission;
d) this.commission = commission; super();
e) super(name, baseSalary); this.commission = commission;
f) this.commission = commission; super(name, baseSalary);
g) super(name, baseSalary, commission);
176) 11. public class Person {
private String name, comment;
private int age;
public Person(String n, int a, String c) {
name = n; age = a; comment = c; 16. }
public boolean equals(Object o) {
if (! (o instanceof Person)) return false;
Person p = (Person)o;
return age == p.age && name.equals(p.name); 21. } 22. }
What is the appropriate definition of the hashCode method in class Person?( )
a) return super.hashCode();
b) return name.hashCode() + age * 7;
c) return name.hashCode() + comment.hashCode() / 2;
d) return name.hashCode() + comment.hashCode() / 2 – age * 3;
177) 12. import java.util.*;
public class Explorer3 {
public static void main(String[] args) {
TreeSet s = new TreeSet();
TreeSet subs = new TreeSet();
for(int i = 606; i < 613; i++)
if(i%2 == 0) s.add(i);
subs = (TreeSet)s.subSet(608, true, 611, true);
subs.add(629); 21. System.out.println(s + " " + subs); 22. } 23. }
运行结果是( )
a) Compilation fails.
b) An exception is thrown at runtime.
c) [608, 610, 612, 629] [608, 610]
d) [608, 610, 612, 629] [608, 610, 629]
e) [606, 608, 610, 612, 629] [608, 610]
f) [606, 608, 610, 612, 629] [608, 610, 629]
178) 1. import java.util.*; 2.
public class LetterASort{
public static void main(String[] args) {
ArrayList strings = new ArrayList();
strings.add("aAaA");
strings.add("AaA");
strings.add("aAa");
strings.add("AAaa");
Collections.sort(strings);
for (String s : strings) { System.out.print(s + " "); } 12. } 13. }
运行结果是( )
a) Compilation fails.
b) aAaA aAa AAaa AaA
c) AAaa AaA aAa aAaA
d) AaA AAaa aAaA aAa
e) aAa AaA aAaA AAaa
f) An exception is thrown at runtime.
179) 5. class A {
void foo() throws Exception { throw new Exception(); } 7. }
class SubB2 extends A {
void foo() { System.out.println("B "); }
}
class Tester {
public static void main(String[] args) {
A a = new SubB2();
a.foo(); 15. } 16. } 运行结果是( )
a) B
b) B, followed by an Exception.
c) Compilation fails due to an error on line 9.
d) Compilation fails due to an error on line 14.
e) An Exception is thrown with no other output.
180) 11. public void someMethod(Object value) {
// check for null value ...
System.out.println(value.getClass()); 21. }
哪行代码放到12行,可以正确的处理一个null值( )
a) assert value == null;
b) assert value != null, "value is null";
c) if (value == null) { throw new AssertionException("value is null"); }
d) if (value == null) { throw new IllegalArgumentException("value is null"); }
181) 1. public class Mule {
public static void main(String[] args) {
boolean assert = true;
if(assert) {
System.out.println("assert is true"); 6. } 7. } 8. }
下边哪个命令可以让程序正常编译( )
a) javac Mule.java
b) javac -source 1.3 Mule.java
c) javac -source 1.4 Mule.java
d) javac -source 1.5 Mule.java
182) 1. public class Venus {
public static void main(String[] args) {
int [] x = {1,2,3};
int y[] = {4,5,6};
new Venus().go(x,y);
}
void go(int[]... z) {
for(int[] a : z)
System.out.print(a[0]);
}
} 运行结果是( )
a) 1
b) 12
c) 14
d) 123
e) Compilation fails.
f) An exception is thrown at runtime.
183) 11. public class Test {
public enum Dogs {collie, harrier, shepherd};
public static void main(String [] args) {
Dogs myDog = Dogs.shepherd;
switch (myDog) {
case collie:
System.out.print("collie ");
case default:
System.out.print("retriever ");
case harrier:
System.out.print("harrier "); 22. } 23. } 24. } 运行结果是( )
a) harrier
b) shepherd
c) retriever
d) Compilation fails.
e) retriever harrier
f) An exception is thrown at runtime.
184) 11. static void test() {
try {
String x = null;
System.out.print(x.toString() + " "); 15. }
finally { System.out.print("finally "); } 17. }
public static void main(String[] args) {
try { test(); }
catch (Exception ex) { System.out.print("exception "); } 21. }
运行结果是( )
a) null
b) finally
c) null finally
d) Compilation fails.
e) finally exception
185) 1. public class Breaker2 {
static String o = "";
public static void main(String[] args) {
z:
for(int x = 2; x < 7; x++) {
if(x==3) continue;
if(x==5) break z;
o = o + x; 9. }
System.out.println(o);
} 12. } 运行结果是( )
a) 2
b) 24
c) 234
d) 246
186) 11. public static void main(String[] args) {
String str = "null";
if (str == null) {
System.out.println("null");
} else (str.length() == 0) {
System.out.println("zero");
} else {
System.out.println("some"); 19. } 20. } 运行结果是( )
a) null
b) zero
c) some
d) Compilation fails
e) An exception is thrown at runtime
187) 11. class A {
public void process() { System.out.print("A,"); }
class B extends A {
public void process() throws IOException {
super.process();
throw new IOException(); 18. }
public static void main(String[] args) {
try { new B().process(); }
catch (IOException e) { System.out.println("Exception"); } 22. }
运行结果是( )
a) Exception
b) A,B,Exception
c) Compilation fails because of an error in line 20
d) Compilation fails because of an error in line 14
e) A NullPointerException is thrown at runtime.
188) 15. public class Pass2 {
public void main(String [] args) {
int x = 6;
Pass2 p = new Pass2();
p.doStuff(x);
System.out.print(" main x = " + x); 21. } 22.
void doStuff(int x) {
System.out.print(" doStuff x = " + x++); 25. } 26. }
在控制台依次输入命令
javac Pass2.java
java Pass2 5 运行结果是( )
a) Compilation fails.
b) An exception is thrown at runtime.
c) doStuff x = 6 main x = 6
d) doStuff x = 6 main x = 7
189) 11. interface DeclareStuff {
public static final int EASY = 3;
void doStuff(int t); }
public class TestDeclare implements DeclareStuff {
public static void main(String [] args) {
int x = 5;
new TestDeclare().doStuff(++x); 18. }
void doStuff(int s) {
. s += EASY + ++s;
System.out.println("s " + s); 22. } 23. } 运行结果是( )
a) s 14
b) s 16
c) s 10
d) Compilation fails.
190) 1. interface DoStuff2 {
float getRange(int low, int high); } 3.
interface DoMore {
float getAvg(int a, int b, int c); } 6.
abstract class DoAbstract implements DoStuff2, DoMore { } 8.
class DoStuff implements DoStuff2 {
public float getRange(int x, int y) { return 3.14f; } } 11.
interface DoAll extends DoMore {
float getAvg(int a, int b, int c, int d); } 运行结果是( )
a) The file will compile without error.
b) Compilation fails. Only line 7 contains an error.
c) Compilation fails. Only line 12 contains an error.
d) Compilation fails. Only line 13 contains an error.
e) Compilation fails. Only lines 7 and 12 contain errors.
191) 3. public class Spock {
public static void main(String[] args) {
Long tail = 2000L;
Long distance = 1999L;
Long story = 1000L;
if((tail > distance) ^ ((story * 2) == tail))
System.out.print(“1”);
if((distance + 1 != tail) ^ ((story * 2) == distance))
System.out.print(“2”); 12. } 13. } 运行结果是( )
a) 1
b) 2
c) 12
d) Compilation fails.
e) No output is produced.
f) An exception is thrown at runtime.
192) 5. class Payload {
private int weight;
public Payload (int w) { weight = w; }
public void setWeight(int w) { weight = w; }
public String toString() { return Integer.toString(weight); }
}
public class TestPayload {
. static void changePayload(Payload p) { /* insert code */ }
public static void main(String[] args) {
Payload p = new Payload(200);
p.setWeight(1024);
changePayload(p);
System.out.println(“p is ” + p);
} }下边哪行代码放到12行,可以保证输出420( )
a) p.setWeight(420);
b) p.changePayload(420);
c) p = new Payload(420);
d) Payload.setWeight(420);
e) p = Payload.setWeight(420);
评论列表
文章目录