考试1 (Java)

来源:互联网 发布:mysql show 编辑:程序博客网 时间:2024/04/27 16:16

                       Exam1 (Java)

Question 1:

Given:

1 public class Test{

2 public static void main(String args[]){

3 class Foo{

4 public int i=3;}

5 Object o = (Object)new Foo();//类型转换变成object

6 Foo foo = (Foo)o;

7 System.out.println("i="+foo.i);}}

what is the result?

A i =3

B Compilation fails

C A ClassCastException is thrown at line 6.

D A ClassCastException is thrown at line 7.

Answer 1:A

Question 2:

Which two cause a compiler 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 2:A,B

Question 3

Given:

class A{

 A(){}

}

class B extends A{

}

which two statements are true?(choose two)

A Class B’s constructor is public.

B. Class B’s constructor has no arguments.

C Class B’s constructor includes a call to this().

D. Class B’s constructor includes a call to super().

Answer 3:B,D

Question 4

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 4:E

Question 5

Given:

interface Animal{

void sound off();}

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?

A.     new Animal().soundOff();

B.     Elephant e = new Alpha1();

C.     Lion 1 = Alpha1.get(“meat eater”);

D.    New Alpha1().get(“veggie”).soundOff();

Answer 5:D

Question 6

Which statement is true?

A.     Memory is reclaimed by calling Runtime.gc().

B.     Objects are not collected if they are accessible from live threads.

C.     Objects that have finalize() methods are never garbage collected.

D.    Objects that have finalize() methods always have their finalize() methods called before the program ends.

E.     An OutOfMemory error is only thrown if a single block of memory cannot be found that is large enough for a particular requirements.

Answer 6:B

Question 7

int i=1,j=10;

do{

if (i>j){

break;

}

j--;

}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=4

D.    i = 5 and j=6

E.     i=6 and j=6

Answer 7:D

Question 8

Given:

int x=3;

int y=1;

if(x=y){

System.out.println(“x=”+x);

}

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 8:C

Question 9:

Given:

public class Test{

public static void aMethod() throws Exception{

try{throw new Exception();

} finally{System.out.println(“finally”);}}

public static void main(String args[]){

try{aMethod();}catch(Exception c){System.out.println(“exception”);}

System.out.println(“finished”);}}

What is the result?

A.     finally

B.     exception finished

C.     finally exception finished

D.    compilation fails.

Answer 9: C

Question 10:

1 public interface Foo{

2 int k=4;}

which three are equivalent to line 2?(choose three)

A.     final int k=4;

B.     public int k=4;

C.     static int k=4;

D.    abstract int k=4;

E.     volatile int k=4;

F.     protected int k=4;

Answer 10: A,B,C

Question 11:

Which interface does java.util.Hashtable implement?

A.     java.util.Map

B.     java.util.List

C.     java.util.Hashtable

D.    java.util.Collection

Answer 11:A

Question 12:

Which two demonstrate encapsulation of data?(choose two)

A Member data have no access modifiers.

B Member data can be modified directly.

C The access modifier for methods is protected.

D The access modifier to member data is private.

E Methods provide for access and modification of data.

Answer 12: DE

Question 13:

Given:

1 public class Foo implements Runnable{

2 public void run(Thread t){

3 System.out.println(“Running”);

4 }

5 public static void main(String[] args){

6 new Thread(new Foo()).start();

7 }

8}

what is the result?

A.     An exception is thrown.

B.     The program exists without printing anything.

C.     An error at line 2 causes compilation to fail.

D.    An error at line 6 causes the compilation to fail.

E.     “Running” is printed and the program exits.

Answer 13: C

Question 14:

Which two can directly cause a thread to stop executing? (choose two)

A.     Exiting from a synchronized block.

B.     Calling the wait method on an object.

C.     Calling the notify method on an object.

D.    Calling the notifyAll method on an object.

E.     Calling the setPriority method on a thread object.//可以实现腿出来

Answer 14: BE

Question 15:

Which two declarations prevent the overriding of a method? (choose two)

A.     final void methoda(){}

B.     void final methoda(){}

C.     static void methods(){}

D.    static final void methods(){}

E.     final abstract void methoda(){}

Answer 15: AD

Question 16:

Which two statements are true regarding the creation of a default constructor? ( choose two)

A The default constructor initializes method variables.

B The default constructor invoked the no-parameter constructor of the super class.

C  The default constructor initializes the instance variables declared in the class.

D If a class lacks a no-parameter constructor, but has other constructors, the compiler creates a default constructor.

F.     The compiler creates a default constructor only when there are no other constructors for the class

G.     

Answer 16:C,F

Question 17

How can you create a listener class that receives events when the mouse is moved?

A.     By extending Mouse Listener.

B.     By implementing Mouse Listener.

C.     By extending Mouse Motion Listener.

D.    By implementing Mouse Motion Listener.

E.     Either by extending Mouse Motion Listener or extending Mouse Listener.

F.     Either by implementing Mouse Motion Listener or implementing Mouse Listener.

Answer 17:D

Question 18

Exhibit:

import java.awt.*;

public class X extends Frame{

      public static void main(String[] args){

             X x= new X();

             x.pack();

             x.setVisible(true);

      }

      public X(){

             setLayout(new BorderLayout());

             Panel p = new Panel();

             add(p,BorderLayout.NORTH);

             Button b=new Button("North");

             p.add(b);

             Button b1 = new Button("SOUTH");

             add(b1,BorderLayout.SOUTH);

      }

}

which two statements are true? (choose two)

A.     The buttons labeled “North” and “South” will have the same width.

B.     The buttons labeled “North” an “South” will have the same height.

C.     The height of the button labeled “North” can very if the Frame is resized.

D.    The height of the button labeled “South” can very if the Frame is resized.

E.     The width of the button labeled “North” can very if the Frame is resized.

F.     The width of the button labeled “South” can constant if the Frame is resized.

Answer 18:B,E

Question 19

Which will declare a method that is available to all members of the same package and can be referenced without an instance of the class?

A abstract public void methoda();

B public abstract double methoda();

C static void methoda(double d1){};

D public native double methoda(){};

E protected void methoda(double d1){};

Answer 19:C

Question 20

public class foo{

public static void main(String[] args) throws Exception{

java.io.PrintWriter out=new java.io.PrintWriter(

new java.io.outputStreamWriter(System.out,true));

out.println(“Hello”);

}

}

which statement an PointX on line 1 allows this code to compile and run?

A import java.io.*;

B include java.io.*;

C import java.io.PrintWriter;

D include java.io.PrintWriter

E. No statement is needed.

Answer 20: E

Question 21

public class Test3

{

      public static void replaceJ(String text)

      {

             text.replace('J','1');

      }

      public static void main(String[] args)

      {

             String text = new String("Java");

             replaceJ(text);

             System.out.println(text);

      }

}

what is the result?

A The program prints “lava”

B The program prints “Java”

C An error at line 7 causes compilation to fail

D compilation succeeds but the program throws an exception.

Answer 21:B

Question 22

public class Foo{

public static void main(String[] args){

StringBuffer a = new StringBuffer(“A”);

StringBuffer b = new StringBuffer(“B”);

Operate(a,b);

System.out.println(a+”,”+b);

)

static void operate(StringBuffer x,StringBuffer y){

y.append(x);

y=x;

)

}

what is the result?

A.     The code compiles and prints “A,B”

B.     The code compiles and prints “A,BA”

C.    The code compiles and prints “AB,B”

D.    The code compiles and prints “AB,AB”

E.     The code compiles and prints “BA,BA”

F.     The code does not compile because “+” cannot be overloaded for StringBuffer.

Answer 22: B

Question 23

1 package foo;

2 import java.util.Vector;

3 private class MyVector extends Vector{

4    int i =1 ;

5    public MyVector()  {

6           i = 2;}

7 }

8 public class MyNewVector extends MyVector{

9    public MyNewVector(){

10          i = 4;

      }

11   public static void main(String args[]){

12          MyVector v = new MyNewVector();}

13 };

The file MyNewVector.java is shown in the exhibit.

What is the result?

A.     Compilation will succeed.

B.     Compilation will fail at line 3

C.     Compilation will fail at line 5

D.    Compilation will fail at line 10

E.     Compilation will fail at line 12

Answer 23:B

Question 24

1 public abstract class Test{

2 public abstract void methodA();

3 public abstract void methodB()

4 {

5 System.out.println(“Hello”);

6 }

7 }

which three changes(made independently) allow the code to compile?(choose three)

A Add a method body to methodA.

B Replace line 5-7 with a semicolon(“;”)

C Remove the abstract qualifier from the declaration of Test.

D Remove the abstract qualifier from the declaration of methodB

E Remove the abstract qualifier from the declaration of methodA.

F Remove methodB in its entirely and change class to interface in line 1

Answer 24: F

Question 25

Exhibit:

1 class Super{

2 public int getLength(){return 4;}

3 }

4

5 public class Sub extends Super{

6 public long getLength() {return 5;}

7

8 public static void main(String[] args){

9 Super sooper = new Super();

10 Sub sub = new Sub();

11 System.out.println(

12

12 sooper.getLength()+”,”+sub.getLength());

13}

14}

what is the ouput?

A 4,4

B 4,5

C 5,4

D 5,5

E The code will not compile.

Answer 25:E

Question 26 Exhibit:

public class Mycircle{

public double radius;

public double diameter;

public void setRadius(double radius){

      this.radius = radius;

      this.diameter = radius*2;

}

public double getRadius(){

      return radius;

}

}

which statement is true?

A The Mycircle class is fully encapsulated.

B The diameter of a given Mycircle is guaranteed to be twice its radius

C Line 6 and 7 should be in a synchronized block to ensure encapsulation

D The radius of a Mycircle object can be set without affecting its diameter.

Answer 26: B

Question 27

Given an ActionEvent, which method allows you to identify the affected Component?//给了一个事件,什么方法可以允许和确认这个主件

A public class getClass()

B public Object getSource()

C public Component getSource()

D public Component getTarget()

E public Component getComponent()

F public Component getTargetComponent()

Answer 27:B

Question 28

 

1 class A{

2 public int getNumber(int a){

3 return a+1;

4 }

5 }

6

7 class B extends A{

8 public int getNumber(int a){

9 return a+2;

10 }

11

12 public static void main(String[] args){

13 A b=new B();

14 System.out.println(b.getNumber());

15}

16}

what is the result?

A Compilation succeeds and 1 is printed.

B Compilation succeeds and 2 is printed.

C An error at line8 causes compilation to fail.

D An error at line 14 causes compilation to fail.

E Compilation succeeds but an exception is thrown at line 14.

Answer 28:D

Question 29 Given

1 class Super{

2 public float getNum(){return 3.0f;}}

3 public class Sub extends Super{}

which method, placed at line 3, causes compilation to fail?

A.     public void getNum(){}

B.     public void getNum(double d){}

C.     public float getNum(){return 4.0f;}

D.    public double getNum(float d){return 4.0d;}

Answer 29:A

Question 30

1 class Base{

 

2 Base(){System.out.print(“Base”);}

3 }

4 public class Alpha extends Base{

5 public static void main(String[] args){

6 new Alpha();

7 new Base();

8}

9 }

what is the result?

A.     Base

B.     Base Base

C.     Compilation fails

D.    The code runs with no output.

E.     An exception is thrown at runtime.

Answer 30:B

 

原创粉丝点击