最近要参加SCJP考试 ,于是把一些题目进行了整理。在试题的下面 有答案
试题:
1.Given:
1. public class TestFive{
2. private int x;
3. public void foo(){
4. int current=x;
5. x=current+1;
6. }
7. public void go(){
8. for(int i=0;i<5;i++){
9. new Thread(){
10. public void run(){
11. foo();
12. System.out.print(x+”,”);;
13. }}.start();
14. }}
Which two change taken together would guarantee the outut 1,2,3,4,5,?(Choose two)
A. move the line 12 print statement into the foo() method
B. change line 7 to public sychronized void go(){
C. change the variable declaration on line 2 to private volatile int x;
D. wrap the code inside the foo() method with a synchronized (this) block
E. wrap the foo loop code inside the go() method with a synchronized block synchronized(this){//for loop code here}
2. Given:
foo and bar are public references available to many other threads. foo refers to a Thread and bar is an Object . The thread foo is currently executing bar.wait().
From another thread ,what provides the most reliable way to ensure that foo will stop executing wait()?
A. foo.notify()
B. bar.notify()
C. foo.notifyAll()
D. Thread.notify()
E. bar.notifyAll()
F. Object.notify()
3.Given:
11. static void test() throws RuntimeException{
12. try{
13. System.out.print(“test”);
14. throw new RuntimeException();
15. }
16. catch(Exception ex){System.out.print(“exception ”);}
17. }
18. public static void main(String[] args){
19. try{test();}
20. catch(RuntimeException ex){System.out.print(“runtime ”);}
21. System.out.print(“end ”);
22. }
What is the result?
A. test end
B. Compilation fails
C. Test runtime end
D. Test exception end
E. A Throwable is thrown by main at runtime
4.Given:
11. Float pi=new Float(3.14f);
12. if(pi>3){
13. System.out.print(“pi is bigger than 3”);
14. }
15. else{
16. System.out.print(“pi is not bigger than 3”);
17. }
18. finally{
19. System.out.print(“Have a nice day”);
20. }
What is the result?
A. Compilation fails
B. Pi is bigger than 3
C. An exception occurs at runtime
D. Pi is bigger than 3 Have a nice day
E. Pi is not bigger than 3 Have a nice day
5.Given:
11. classA{
12. public void process(){System.out.print(“A,”);}
13. class B extends A{
14. public void process() throws IOException{
15. super.process();
16. System.out.print(“B,”);
17. throw new IOException();
18. }
19. public static void main(String [] args){
20. try{new B().process();}
21. catch(IOException e){System.out.println(“Exception”);}
what is thw result:
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
6. Given:
33. Try{
34. //some code here
35. }catch (NullPointerException e1){
36. System.out.print(“a”);
37. } catch (RuntimeException e2){
38. System.out.print(“b”);
39. }finally{
40. System.out.print(“C”)’
41. }
What is the result if a NullpointerException occurs on line 34?
A. c
B. a
C. ab
D. ac
E. bc
F. abc
7. Given:
11. public static Collection get(){
12. Collection sorted =new LinkedList();
13. sorted.add(“B”); sorted.add(“C”); sorted.add(“A”);
14. return sorted;
15. }
16. public static void main(String[] args){
17. for(Object obj:get()){
18. System.out.print(obj+”,”);
19. }
20. }
What is the result:
A. A,B,C
B. B,C,A
C. Compilation fails
D. The code runs with no output
E. An exception is thrown at runtime
8.
23. int z=5;
24. public void stuff1(int x){
25. assert(x>0);
26. switch(x){
27. case 2:x=3;
28. default assert false;}}
29.
30. private void stuff2(int y){assert(y<0);}
31.
32. private void stuff3{assert(stuff4());}
33.
34. private boolean stuff4(){z=6;return false;}
which statement is true:
A. All of the assert statements are used appropriately
B. Only the assert statement on line 31 is used appropriately
C. The assert statements on lines on lines29 and 31 are used appropritely
D. The assert statements on lines on lines26 and 29 are used appropritely
E. The assert statements on lines on lines29 and 33 are used appropritely
F. The assert statements on lines on lines29,31 and 33 are used appropritely
G. The assert statements on lines on lines26,29 and 31 are used appropritely
9 ClassA a=new ClassA();
a.methodA();
what is the result?
public class ClassA
{
public void methodA()
{
ClassB classB=new ClassB();
classB.getValue();
}
}
Class ClassB
{
public ClassC classC;
public String getValue()
{
return classC.getValue();
}
}
Class ClassC
{
public String value;
public String getValue()
{
Value=”ClassB”;
Return value;
}
}
A Compilation fails
B ClassC is displayed;
C The Code runs with no output
D An exception is thrown at runtime
10.
31 public void method()
{
32 A a=new A();
33 a.method1();
34 }
Which statement is true if a TestException is thrown on line 3 of class B?
1. public classA{
2. public void method1(){
3. try{
4. B b=new B();
5. b.method2();
6. //more code here
7. }catch (TestException te){
8. throw new RuntimeException(te);
9. }
10. }
11. }
1.public class B{
2. public void method2()throws TestException{
3. //more code here
4. }
5.}
1.public class TestException extends Exception{
2.}
A Line 33 must be called within a try block
B The exception thrown by method1 in class A is not required to be caught
C The method declaredon line 31 must be declared to throw a RuntimeException
D On line 5 of class A the call to method2 of class B does not nedd to be placed a try/catch block
11. public class CreditCard{
private String cardID;
private integer limit;
public String ownerName;
public void setCardinformation(String cardID,String ownerName,Integer limit)
{
This.cardID=cardID;
This.ownerName=ownerName;
This.limit=limit;
}
}
Which statement is true?
A The class is fully encapsulated
B The code demonstrates polymorphism
C The ownerName variable breaks encapsulation
D The cardID and limit variables break polymorphism
E The setCardinformation method breaks encapsulation
12. Given:
10.abstract class A{
11. abstract void a1();
12. void a2() {}
13.}
14. class B extends A{
15. void a1() {}
16. void a2() {}
17. }
18. class C extends B { void c1() {} }
And :
A x=new B(); C y=new C(); A z=new C();
What are four valid examples of polymorphic method calls?(Choose four)
A. x.a2();
B. z.a2();
C. z.c1();
D. z.a1();
E. x.a1();
13.Given:
10. interface Jumper { public void jump(); }
…
20. class Animal {}
…
30. class Dog extends Animal{
31. Tail tail;
32. }
…
40. class Beagle extends Dog implements Jumper{
41. public void jump() {}
…
50. class Cat implements Jumper{
51. public void jump() {}
52. }
Which three are true?(Choose three)
A. Cat is-a Animal
B. Cat is-a Jumper
C. Dog is-a Animal
D. Dog is-a Jumper
E. Cat has-a Animal
F. Beagle has-a Tail
G. Beagle has-a Jumper
14.Given:
1. public class Blip{
2. protected int blipvert(int x) {return 0;}
3. }
4. class Vert extends Blip{
5.//insert code here
6.}
Which five methods, inserted independdently at line 5, will compile?(Choose five)
A. public int blipvert(int x ) {return 0;}
B. private int blipvert (int x) {return 0;}
C. private int blipvert(long x) {return 0;}
D. protected long blipvert(int x) {return 0;}
E. protected int blipvert(long x) {return 0;}
F. protected long blipvert(long x) {return 0;}
G. protected long blipvert(int x, int y) {return 0;}
15.Given:
10. public class SuperCalc{
11. protected static int multiply(int a, int b) {return a*b;}
12.}
And:
20.public class SubCalc extends SuperCalc{
21. public static int multiply(int a, int b){
22. int c=super.multiply(a,b);
23. return c;
24. }
25. }
And :
30. SubCalc sc=new SubCalc();
31. System.out.println(sc.multiply(3,4));
32. System.out.println(SubCalc.multiply(2,2));
What is the result?
A. 12
4
B. The code runs with no output
C. An exception is thrown at runtime.
D. Compliation fails because of an error in line 21.
E. Compilation fails because of an error in line 22.
F. Compilation fails because of an error in line 31.
16.Given:
1. class Pizza{
2. java.util.ArrayList toppings;
3.public final void addTopping(String topping){
4. toppings.add(topping);
5. }
6.}
7. public class PepperoniPizza extends Pizza{
8.public void addTopping(String topping){
9. System.out.println(“Cannot add Toppings”);
10.}
11.public static void main(String[] args){
12. Pizza pizza=new PepperoniPizza();
13. pizza.addTopping(“Mushrooms”);
14. }
15.}
What is the result?
Given:
10. public class Pizza{
11. ArrayList toppings;
12.
13. public final void addTopping(String topping){
14. toppings.add(topping);
15. }
16.
17. public void removeTopping(String topping){
18. toppings.remove(topping);
19. }
20.}
And:
30. class PepperoniPizza extends Pizza{
31. public void addTopping(String topping){
32. System.out.println(“Cannot add Toppings”);
33. }
34.
35. public void removeTopping(String topping){
36. System.out.println(“Cannot remove Pepperoni”);
37. }
38.}
And:
50. Pizza pizza=new PepperoniPizza();
51. Pizza.addTopping(“Mushrooms”);
52. pizza.removeTopping(“Pepperoni”);
A. compilation fails
B. Cannot add Toppings
C. The code runs with no output.
D. A NullPointerException is thrown in Line 4.
17.Given a valid DateFormat object named df, and
16. Date d=new date(OL);
17. String ds=”December 15, 2004”;
18. //insert code here
What updates d’s value with the date reresented by ds?
A. d =df.parse(ds);
B. d=df.getDate(ds);
C. try{
d=df.parse(ds);
}catch(ParseException e){};
D. try{
d=df.getDate(ds);
}catch(ParseException e){};
18.Given
1. public class MyLogger{
2. private StringBuilder logger=new StringBuilder();
3. public void log(String message, String user){
4. logger.append(message);
5. logger.append(user);
6. }
7.}
The programmer must guarantee that a single MyLogger object works properly for a multi-threaded system.
How must this code be changed to be thread-safe?
A. synchronize the log method
B. replace Stringbuilder with StringBuffer
C. replace StringBuilder with just a String object and use the string concatenation(+=)within the log method
D. No change is necessary, the current MyLogger code is already thread-safe.
19.Click the Exhibit button
Which code,inserted at line 14,will allow this class to correctly serialize and deserialize?
1.import java.io;
2.public class Foo implements Serializable
{
3 public int x, y;
4 public Foo(int x,int y)
5 { this.x=x; this.y=y; }
6 Private void writeObject(ObjectOutputStream s)
7 throws IOException
8 { s.writeInt(x);s.writeInt(y);
9 }
10
11 Private void readObject(ObjectInputStream s)t
12 throws IOException,
13 ClassNotFoundException{
14 //insert code here
15 }
16}
A s.defaultReadObject();
B.this=s.defaultReadObject();
C.y=s.readInt();x=s.readInt();
D.x=s.readInt();y=s.readInt();
20. Given this method in a class:
public String toString(){
StringBuffer buffer=new StringBuffer();
buffer.append(‘<’);
buffer.append(this.name);
buffer.append(‘>’);
return buffer.toString();
}
Which statement is true?
A This code is NOT thread-safe
B The programmer can replace StringBuffer with StringBuilder with no other changes
C This code will perform poorly.For better performance.the code should be rewritten: return “<”+this.name+”>”;
D This code will perform well and converting the code to use StringBuilder will not enhance the performance
21. Given
public class Score implements Comparable<Score>{
private int wins,losses;
public Score(int w,int l){wins=w;losses=l;}
public int getWins(){return wins;}
public int getLosses(){return losses;}
public String toString(){
return “<”+wins+”,”+losses+”>”;
}
//insert code here
}
Which method will complete this class?
A public int compareTo(Object o){/*more code here*/}
B public int compareTo(Score other){/*more code here*/}
C public int compareTo(Score s1,Score s2){/*more code here*/}
D public int compareTo(Object o1,Object o2){/*more code here*/}
22. Given the command line java Pass2 and:
15. public class Pass2{
16. public void main(String[] args){
17. int x=6;
18. Pass2 p=new Pass2();
19. p.doStuff(x);
20. System.out.print(" main x= " + x);
21. }
22. void doStuff(int x)
23.{
24. System.out.print(" doStuff x= " + x++);
25.}
26. }
What is the result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. doStuff x = 6 main x = 6.
D. doStuff x = 6 main x = 7.
E. doStuff x = 7 main x = 6.
F. doStuff x = 7 main x = 7.
23.public class Pass
{
public static void main(String[] args)
{
int x=5;
Pass p=new Pass();
p.doStuff(x);
System.out.print(" main x = " + x);
}
void doStuff(int x)
{
System.out.print(" doStuff x = " + x++);
}
}
What is the result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. doStuff x = 6 main x = 6.
D. doStuff x = 5 main x = 5.
E. doStuff x = 5 main x = 6.
F. doStuff x = 6 main x = 5.
24.Given:
11 class Cup{}
12 class PoisonCup extends Cup{}
21 public void takeCup(Cup c) {
22 if(c instanceof PoisonCup) {
23 System.out.println("Inconceivabie!"); }
24 else if(c instanceof Cup) {
25 System.out.println("Dizzying intellect!"); }
26 else
27 { System.exit(0);
28 }
29 }
And the execution of the statements:
Cup cup = new PoisonCup();
takecup(cup);
Whit is the output?
A. Inconceivabie!
B. Dizzying intellect!
C. The code runs with no output.
D. An exception is thrown at runtime.
E. Compilation fails because of an error in line 22.
25.Given
1 public class GC{
2 private Object o;
3 private void doSomethingElse(Object obj){o=obj;}
4 public void doSomething() {
5 Object o=new Object();
6 doSomethingElse(o);
7 o=new Object();
8 doSomethingElse(null);
9 o=null;
10 }
11 }
When the doSometing method is called, after which line does the Object created in line 5 become available for garbage collection?
A. Line5
B. Line6
C. Line7
D. Line8
E. Line9
F. Line10
26.Click the Exhibit button.
Given this code from Class B:
25. A a1=new A();
26. A a2=new A();
27. A a3=new A();
28. System.out.println(A.getInstanceCount());
1 public class A{
2
3 private int counter=0;
4
5 public static int getInstanceCount() {
5
6 return counter;
7 }
8
9 public A(){
10 counter++;
11 }
12
13 }
What is the result?
A. Compilation of class A fails.
B. Line 28 prints the value 3 to System.out.
C. Line 28 prints the value 1 to System.out.
D. A runtime error occurs when line 25 executes.
E. Compilation fails because of an error on line 28.
27.Click the Exhibit button.
1 public class GoTest{
2 public static void main(String[] args)
{
3 Sente a=new Sente();a.go();
4 Goban b=new Goban();b.go();
5 Stone c=new Stone();c.go();
6 }
7 }
9 class Sente implements Go{
10 public void go() {System.out.println("go in Sente.");
11 }
12 }
13 class Goban extends Sente{
14 public void go() { System.out.println("go in Goban");
15 }
16}
17 class Stone extends Goban implements Go{
}
18 interface Go{
19 public void go();}
What is the result?
A. go in Goban
go in Sente
go in Sente
B. go in Sente
go in Goban
go in Goban
C. go in Goban
go in Sente
go in Sente
D. Compilation fails because of an error in line 17.
28.Given
class TestA
{
public void start()
{
System.out.println("TestA");
}
}
public class TestB extends TestA
{
public void start()
{
System.out.println("TestB");
}
public static void main(String[] args)
{
((TestA)new TestB()).start();
}
}
What is the result?
A. TestA
B. TestB
C. Compilation fails.
D. An exception is thrown at runtime.
29.Given:
35. String #name="Jane Doe";
36. int $age=24;
37. Double _height=123.5;
38. double ~temp=37.5;
Whilch two statements are true? (Choose two)
A. Line 35 will not compile.
B. Line 36 will not compile.
C. Line 37 will not compile.
D. Line 38 will not compile.
30.Click the Exhibit button
public class Bootchy
{
int bootch;
String snootch;
public Bootchy()
{
this("snootchy");
System.out.print("first ");
}
public Bootchy(String snootch)
{
this(420,"snootchy");
System.out.print("second");
}
public Bootchy(int bootch,String snootch)
{
this.bootch=bootch;
this.snootch=snootch;
System.out.print("third");
}
public static void main(String[] args)
{
Bootchy b=new Bootchy();
System.out.print(b.snootch + " " + b.bootch);
}
}
What is the result?
A. snootchy 420 third second first
B. snootchy 420 first second third
C. first second third snootchy 420
D. third second first snootchy 420
E. third first second snootchy 420
F. first second first third snoothchy 420
31 Given:
1.interface TestA{String toString();}
2.public class Test{
3. public static void main(String[] args){
4. System.out.println(new TestA(){
5. public String toString(){return “test”;}
6. });
7. }
8.}
What is the result?
A test
B null
C An exception is thrown at runtime.
D Compilation fails because of an error in line1.
E Compilation fails because of an error in line4.
F Compilation fails because of an error in line5.
32.55. int [] x = {1,2,3,4,5};
56. int y[] = x ;
57. System.out.println(y[2]);
Which statement is true?
A- Line 57 will print the value 2.
B- Line 57 will print the value 3.
C- Compilation will fail because of an error in line 55.
D- Compilation will fail because of an error in line 56.
33.A programmer needs to create a logging method that can accept an arbitrary number of arguments. For example, it may be called in these ways:
Loglt(“log message1”);
Loglt(“log message2”,”log message3”);
Loglt(“log message4”,”log message5” ,”log message6”);
Which declaration satisfies this requirement?
A public void loglt(String *msgs)
B public void loglt(String [] msgs)
C public void loglt(String … msgs)
D public void loglt(String msg1,String msg2,String msg3)
34.Given:
1. public class TestString1{
2. public static void main(String[] args){
3. String str=”420”;
4. str+=42;
5. System.out.println(str);
6. }
7. }
What is the output?
A- 42
B- 420
C- 462
D- 42042
E- Compilation fails.
F- An exception is thrown at runtime.
35.
11. class Converter{
12. public static void main(String[] args){
13. Integer i=args[0];
14. int j=12;
15. System.out.println(“it is”+(j==i)+”that j==i.”);
16. }
17. }
What is the result when the programmer attempts to compile the code and run it with the command 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.
36. Given:
11.String test=”a1b2c3”:
12.String[] tokens=test.split(“""d”);
13.for(String s:tokens) System.out.print(s+” ”);
What is the result?
A- a b c
B- 1 2 3
C- a1b2c3
D- a1 b2 c3
E- Comapilation fails.
F- The code runs with no output.
G- An exception is thrown at runtime.
37.Given:
10.class One{
11.public One() {System.out.print(1);}
12. }
13.class Two extends One{
14. public Two() {System.out.print(2);}
15. }
16.class Three extends Two{
17. public Three() {System.out.print(3);}
18. }
19.public class Numbers{
20. public static void main(String[] argv) {new Three();}
21. }
What is the result when this code is executed?
A- 1
B- 3
C- 123
D- 321
E- The code runs with no output.
38.Given:
1. public interface A{
2. String DEFAULT_GREETING=”Hello World”;
3. public void method1();
4. }
A programmer wants to create an interface called B that has A as its parent.Which interface declaration is correct?
A- public interface B extends A{}
B- public interface B implements A{}
C- public interface B instanceOf A{}
D- public interface B inheritsFrom A{}
39.
interface Data{ public void load()}
abstract class Info{ public abstract void load(); }
Which class correctly uses the Data interface and Info class?
A public class Employee extends Info implements Data{
public void load(){/*do something*/}
}
B public class Employee implements Info extends Data{
public void load(){/*do something*/}
}
C public class Employee extends Info implements Data{
public void load(){/*do something*/}
public void Info. load(){/*do something*/}
}
D public class Employee implements Info extends Data{
public void Data. load(){/*do something*/}
public void load(){/*do something*/}
}
E public class Employee implements Info extends Data{
public void load(){/*do something*/}
public void Info. load(){/*do something*/}
}
F public class Employee extends Info implements Data{
public void Data.loda(){/**/}
public void Info. loda(){/**/}
}
40.
11.public class Ball{
12.public enum Color{RED, GREEN, BLUE}
13.public void foo(){
14.//insert code here
15.{System.out.println(c);}
16.}
17.}
Which code insert at line 14 cause the foo menthod to print RED, GREEN and BLUE?
A for(Color c: Color.values())
B for(Color c = RED; c<BLUE; c++)
C for(Color c;c.hasNext(); c.next())
D for(Color c=Color[0]; c<=Color[2] ;c++)
E for(Color c=Color.RED; c<Color.BLUE;c++)
41.Given:
12. String csv=”Sue,5,true,3”;
13. Scanner scanner=new Scanner(csv);
14. scanner.useDelimiter(“,”);
15.int age=scanner.nextlnt();
What is the result?
A- Compilation fails.
B- After line 15,the value of age is 5.
C- After line 15,the value of age is 3.
D- An exception is thrown at runtime.
42.Given
class B{
public int i=10;
}
public class A{
public void method(B obj) {
obj.i=20;
B obj1 = new B();
obj = obj1;
System.out.println(obj.i);
}
public static void main(String[] args){
B obj=new B();
A object = new A();
object.method(obj);
System.out.println(obj.i);
}
}
What is the output?
A- 10
10
B- 10
20
C- 20
20
D- Compilation fails.
E- The code runs with no output.
43. 11.public static void main(String[] args){
12.Integer i = new Integer(1)+new Integer(2);
13.switch(i)
14. case 3: System.out.println(“three”); break;
15. default System.out.println(“other”);break;
16.}
}
what is the result
A three
B other
C An exception is thrown at runtime
D Compliation fails because of an error on line 12
E Compliation fails because of an error on line 13
F Compliation fails because of an error on line 15
44. public class ClassA{
public void count(int i){
count(++i);
}
}
And:
ClassA a = new ClassA();
a.count(3);
Which exception or error should be thrown by the virtual machine
A StackOverflowError
B NullPointException
C NumberFormatException
D lllEgalArgumentException
E ExceptionInInitializerError
45.
10 interface Foo{}
11 class Alpha implements Foo{}
12 class Beta extends Alpha{}
13 Class Delta extends Beta{
14 public static void main(String[] args){
15 Beta x = new Beta();
16 //insert code here
17 }
18}
which code inerted at line 16, will cause a
java.lang.ClassCastException?
A Alpha a = x;
B Foo f = (Delta)x;
C Foo f = (Alpha)x;
D Beta b = (Beta)(Alpha)x;
46. 1.public class Target{
2. public static void main(String [] args) {
3. public int addOne(){
4 return ++I;
5 }
6 }
And
1 public class Client{
2 public static void main(String[] args)
3 System.out.println(new Target().addOne());
4. }
5.}
Which change can you make to Target without affecting Client?
A. Line 4 of class Target can be change to return i++;
B. Line 2 of class Target can be change to private int i=1;
C. Line 3 of class Target can be change to private int addOne();
D. Line 2 of class Target can be change to private Integer i=0;
47.1. class SuperClass {
2. public A getA() {
3. return new A();
4. }
5. }
6. class SubClass extends SuperClass{
7. public B getA() {
8. return new B();
9. }
10. }
Which statement is true?
A. Compiltion will success if A extends B.
B. Compiltion will success if B extends A.
C. Compilation will fails because of an error in line 7.
D. Compilation will fails because of an error in line 8.
48.10. public class Hello{
11.String title;
12.int value;
13.public Hello(){
14. title +=”world”;
15. }
16. public Hello (int value) {
17.this.value=value;
18.title =”Hello”;
19.Hello();
20.}
21. }
And
30. Hello c= new Hello(5);
31. System.out.println(c.title);
What is the result?
A. Hello
B. Hello world
C. Complition fails
D. Hello world 5
E. The code runs with no output
F. An exception is thrown at runtime
49.12. public class Test {
13. public enum Dogs (collie , harrier);
14. public static void main(String [] args){
15. Dogs myDogs=Dogs.collie;
16. switch (myDogs){
17. case collie:
18 System.out.print(“collie”);
19. case harrier:
20. System.out.print(“harrier”);
21. }
22. }
23.}
What is the result?
A.collie.
B.harrier
C.Compilation falls
D.collie harrier
E.An exception is thrown at runtime
50.10. int x=0;
11. int y=10;
12. do{
13. y--;
14. ++x;
15. }while (x <5);
16. System .out .print(x +”.”+ y);
What is the result?
A. 5.6
B. 5.5
C. 6.5
D. 6.6
51.1. public class Boxer1 {
2. Integer i;
3. int x;
4. public Boxer1 (int y){
5. x= i+y;
6. System .out .println (x);
7. }
8. public static void main (String [] args) {
9. new Boxer1 (new Integer(4));
10. }
11. }
What is the result?
A. The value “4” is printed at the command line
B. Compilation fails because of an error in line 5
C. Compilation fails because of an error in line 9
D. A NullPointerException occurs at runtime
E. A NumberFormatException occurs at runtime
F. An lllegalStateException occurs at runtime
52.Given;
35. int x=10;
36.do {
37. x--;
38. }while (x<10);
How many times will line 37 be executde?
A. ten times
B. zero times
C. one to nine times
D. more than ten times
53.11. public static void main (String [] args) {
12.String str=”null”;
13.if (str = =null) {
14. System .out .println (“null”);
15.}else (str .lenght () = =0) {
16. System.out. println (“zero”);
17.}else {
18. System. Out .println (“some”);
19. }
20.}
What is the result?
A. null
B. zero
C. some
D. Compiltion fails
E. An exception is thrown at runtime
54 Given
class ClassA{}
class ClassB extends ClassA{}
class ClassC extends ClassA{}
and:
ClassA p0=new ClassA();
ClassB p1=new ClassB();
ClassC p2=new ClassC();
ClassA p3=new ClassB();
ClassA p4=new ClassC();
Which three are valid?(Choose three)
A p0=p1;
B p1=p2;
C p2=p4;
D p2=(ClassB)p1;
E p1=(ClassB)p3;
F p2=(ClassC)p4;
55.Given
public class Foo
{
public int a;
public Foo(){a=3;}
public void addFive(){a+=5;}
}
and:20 .
public class Bar extends Foo{
public int a;
public Bar(){a=8;}
public void addFive(){this.a +=5;}
}
invoked with:
Foo foo=new Bar();
foo.addFive();
System.out.println(“Value:”+foo.a);
What is the result?
A Value :3;
B Value:8
C Value:13
D Compilation fails
E The code runs with no output
56.Given
public class MyClass{
public Integer startingl;
public void methodA(){
Integer i=new Intege(25);
startingl =i;
methodB(i);
}
private void methodB(Integer i2)
i2=i2.intValue();
}
}(choose two)
A i2==startingl returns true.
B i2==startingl returns false.
C i2.equals(startingl) returns true.
D i2.equals(startingl) returns false.
57. class TestA{
public void start(){System.out.println(“TestA”);}
}
public class TestB extends TestA{
public void start(){System.out.println(“TestB”);}
public static void main(String [ ]args){
((TestA)new TestB()).start();
}
}
What is the result?
A.TestA
B.TestB
C.Complilation fails
D.An exception is thrown at runtime
58. 355
Given:
11.public static void append(List list){list.add(“0042”);}
12.public static void main(String[] args){
13. List<Integer> intList=new ArrayList<Integer>();
14. append(intList);
15. System.out.println(intList.get(0));
16.}
What is the result?
A.42
B.0042
C.An exception is thrown at runtime
D.Compilation fails because of an error in line 13
E.Compilation fails because of an error in line 14
59. Given a pre-generics implementation of a method
11.public static int sum(List list){
12.int sum=0;
13.for(iterator iter=list.iterator();iter.hasNext();){
14. int i=((Integer)iter.next()).intValue();
15. sum+=i;
16.}
17.return sum;
18.}
Which three changes must be made to the method sum to use generics?(Choose three.)
A. remove line 14
B. replace line 14 with “int i=iter.next();”
C. replace line 13 with “for (int i;intList){”
D. replace line 13 with “for (Iterator iter :intList){”
E. replace the method declaration with “sum(List<int> intList)”
F. replace the method declaration with “sum(List<Integer> intList)”
60. Given:
abstract public class Employee{
protected abstract double getSalesAmount();
public double getCommision(){
return getSalesAmount()*0.15;
}
}
class Sales extends Employee{
//insert method here
}
Which two methods, insertded independently at line 17, correctly complete the Sales class?(Choose Two)
A.double getSalesAmount(){return 1230.45;}
B.public double getSalesAmount(){return 1230.45;}
C.private double getSalesAmount(){return 1230.45;}
D.protected double getSalesAmount(){return 1230.45;}
答案:
1.本题正确答案是A、D.
2.本题正确答案是E.
3.D
4.本题正确答案是A.
5.本题正确答案是D.
6.本题正确答案是D.
7.本题正确答案是B.
8.本题正确答案是C.
9. 答案:D
10.答案:B
11.答案:C
12.ABDE
13.BCF
14.答案:A,C,E,F,G
15 E
16.答案:A
17.C
18.答案:A
19.答案:D
20.答案:B
21.本题的正确答案是:B
22.B主函数必须是静态的
23.D,
24.A
25.D
26 A
27.B
28 B
29.答案:AD
30.答案:D
31.答案:A
32.答案:B
33.答案:C
34.本题正确答案是D
35.本题答案是D
36.答案:A
37.本题正确答案是C
38.本题正确答案是A
39.
本题正确答案是 A
40.本题答案是A
41.本题答案是D
42本题答案:B
43.本题的答案是A
44. 本题的答案是A
45.本题的答案是B
46. 本题的答案是D
47.本题的答案是B
48.本题的答案是C
49.本题的答案为D
50.本题的答案是B
51.本题的答案为D
52本题的答案为D
53.本题的答案为D
54.本题正确答案是AEF
55.本题正确答案是A
56.本题正确答案是BC
57 B
58. 本题的答案是B
59. 本题的正确答案是:A C F
60. B D
1.本题正确答案是A、D.
2.本题正确答案是E.
3.D
4.本题正确答案是A.
5.本题正确答案是D.
6.本题正确答案是D.
7.本题正确答案是B.
8.本题正确答案是C.
9. 答案:D
10.答案:B
11.答案:C
12.ABDE
13.BCF
14.答案:A,C,E,F,G
15 E
16.答案:A
17.C
18.答案:A
19.答案:D
20.答案:B
21.本题的正确答案是:B
22.B主函数必须是静态的
23.D,
24.A
25.D
26 A
27.B
28 B
29.答案:AD
30.答案:D
31.答案:A
32.答案:B
33.答案:C
34.本题正确答案是D
35.本题答案是D
36.答案:A
37.本题正确答案是C
38.本题正确答案是A
39.
本题正确答案是 A
40.本题答案是A
41.本题答案是D
42本题答案:B
43.本题的答案是A
44. 本题的答案是A
45.本题的答案是B
46. 本题的答案是D
47.本题的答案是B
48.本题的答案是C
49.本题的答案为D
50.本题的答案是B
51.本题的答案为D
52本题的答案为D
53.本题的答案为D
54.本题正确答案是AEF
55.本题正确答案是A
56.本题正确答案是BC
57 B
58. 本题的答案是B
59. 本题的正确答案是:A C F
60. B D
1.本题正确答案是A、D.
2.本题正确答案是E.
3.D
4.本题正确答案是A.
5.本题正确答案是D.
6.本题正确答案是D.
7.本题正确答案是B.
8.本题正确答案是C.
9. 答案:D
10.答案:B
11.答案:C
12.ABDE
13.BCF
14.答案:A,C,E,F,G
15 E
16.答案:A
17.C
18.答案:A
19.答案:D
20.答案:B
21.本题的正确答案是:B
22.B主函数必须是静态的
23.D,
24.A
25.D
26 A
27.B
28 B
29.答案:AD
30.答案:D
31.答案:A
32.答案:B
33.答案:C
34.本题正确答案是D
35.本题答案是D
36.答案:A
37.本题正确答案是C
38.本题正确答案是A
39.
本题正确答案是 A
40.本题答案是A
41.本题答案是D
42本题答案:B
43.本题的答案是A
44. 本题的答案是A
45.本题的答案是B
46. 本题的答案是D
47.本题的答案是B
48.本题的答案是C
49.本题的答案为D
50.本题的答案是B
51.本题的答案为D
52本题的答案为D
53.本题的答案为D
54.本题正确答案是AEF
55.本题正确答案是A
56.本题正确答案是BC
57 B
58. 本题的答案是B
59. 本题的正确答案是:A C F
60. B D