天天看點

OCJP認證考試之八

QUESTION 139

Giventhe following directory structure: bigProject |--source | |--Utils.java ||--classes |-- And the following command line invocation: javac -d classessource/Utils.java Assume the current directory is bigProject, what is theresult?

A. If the compile is successful, Utils.class is added to thesource directory.

B. The compiler returns an invalid flag error.

C. If the compile is successful, Utils.class is added to theclasses directory.

D. If the compile is successful, Utils.class is added to thebigProject directory.

Answer: C

Section: (none)

QUESTION 155

Given:

  1. interface Data { public voidload(); }
  2. abstract class Info { publicabstract void load(); }

Which class correctly uses the Datainterface and Info class?

A. public class Employee extendsInfo implements Data { public void load() { /do something/ }

}

B. public class Employeeimplements Info extends Data { public void load() { /do something/ }

C. public class Employee extendsInfo implements Data { public void load(){ /do something/ }public voidInfo.load(){ /do something/ }

D. public class Employeeimplements Info extends Data { public void Data.load(){ /do something/}public void load(){ /do something/ }

E. public class Employeeimplements Info extends Data { public void load(){ /do something/ }publicvoid Info.load(){ /do something/ }

F. public class Employee extendsInfo implements Data{public void Data.load() { /do something/ }

public void Info.load() { /do something/}

Answer: A

QUESTION 156 Given:

  1. public class Rainbow {
  2. public enum MyColor {
  3. RED(0xff0000), GREEN(0x00ff00),BLUE(0x0000ff);
  4. private final int rgb;
  5. MyColor(int rgb) { this.rgb =rgb; }
  6. public int getRGB() { returnrgb; }
  7. };
  8. public static voidmain(String[] args) {
  9. // insert code here

Which code fragment, inserted at line 19,allows the Rainbow class to compile?

A. MyColor skyColor = BLUE;

B. MyColor treeColor =MyColor.GREEN;

C. if(RED.getRGB() <BLUE.getRGB()) { }

D. Compilation fails due to othererror(s) in the code.

E. MyColor purple = newMyColor(0xff00ff);

F. MyColor purple = MyColor.BLUE +MyColor.RED;

Answer: B枚舉常量不能執行個體化,隻能在定義的時候制定。

QUESTION 157 Given:

  1. class One {
  2. void foo() { }
  3. class Two extends One {
  4. //insert method here

Which three methods, inserted individuallyat line 14, will correctly complete class Two? (Choose three.)

A. int foo() { / more code here/ }

B. void foo() {/ more code here / }

C. public void foo(){ / more code here / }

D. private void foo() { / morecode here / }

E. protectedvoid foo() { / more code here / }

Answer: BCE

8pt;mso-list:l1 level1 lfo2'>B. MyColor treeColor =MyColor.GREEN;

Answer: B

Click the Exhibit button.

Which statement is true about the classes and interfaces in the exhibit?

A. Compilation will succeed for all classes and interfaces.

B. Compilation of class C will fail because of an error in line 2.

C. Compilation of class C will fail because of an error in line 6.

D. Compilation of class AImpl will fail because of an error in line 2.

考察多态性的動态綁定,如果方法的簽名相同,則傳回類型應該與超類的相同或是超類中傳回類型的子類型。上題中C類中有方法Object execute() 以及 String execute(),Object不是Strng類型的子類型,是以錯誤。

QUESTION 159 Given:

  1. public interface A { public void m1();}
  2. class B implements A { }
  3. class C implements A { publicvoid m1() { } }
  4. class Dimplements A { public void m1(int x) { } }
  5. abstract class E implements A {}
  6. abstract class F implements A {public void m1() { } }
  7. abstractclass G implements A { public void m1(int x) { } }

What is the result?

A. Compilation succeeds.

B. Exactly one class does NOT compile.

C. Exactly two classes do NOTcompile.

D. Exactly four classes do NOTcompile.

E. Exactly three classes do NOTcompile.

B類沒有實作接口A,C類無錯,D類沒有實作函數void m1();~~abstract抽象類可以不去實作接口

QUESTION 160 Given:

  1. class Alligator {
  2. int []x[] = {{1,2}, {3,4,5},{6,7,8,9}};
  3. int [][]y = x;
  4. System.out.println(y[2][1]);

A. 2 B. 3 C. 4 D. 6 E. 7

F. Compilation fails.

Answer: E

  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. class Sente implements Go {
  7. public void go() { System.out.println(”go in Sente.”); }
  8. class Goban extends Sente {
  9. public void go() { System.out.println(”go in Goban”); }
  10. class Stone extends Goban implements Go { }
  11. interface Go { public void go(); }

A. go in Goban

go in Sente

B. go in Sente

go in Goban

C. go in Sente

D. go in Goban

E. Compilation fails because of an error in line 17.

QUESTION 162 Given:

  1. NumberFormat nf =NumberFormat.getInstance();
  2. nf.setMaximumFractionDigits(4);
  3. nf.setMinimumFractionDigits(2);
  4. String a =nf.format(3.1415926);
  5. String b = nf.format(2);

Which two statements are true about theresult if the default locale is Locale.US? (Choose two.)

A. The value of b is 2.

B. The value of a is 3.14.

C. The value ofb is 2.00.

D. The value of a is 3.141.

E. The value of a is 3.1415.

F. F. The valueof a is 3.1416.

G. The value of b is 2.0000.

Answer: CF

setMaximumFractionDigits是設定小數點後的數字的最max個數~~

setMinimumFractionDigits是設定小數點後的數字的最min個數~~

setMinimumIntegerDigits是設定小數點前的數字的最min個數~~

QUESTION 163 Given:

  1. String test ="a1b2c3";
  2. String[] tokens =test.split("\d");
  3. for(String s: tokens)System.out.print(s + " ");

A. a b c

B. 1 2 3

C. a1b2c3

D. a1 b2 c3

E. Compilation fails.

F. The code runs with no output.

G. An exception is thrown atruntime.

.

d代表digital數字~~ 表示以數字進行拆分

QUESTION 164 Given:

  1. class Converter {
  2. Integer i = args[0];
  3. int j = 12;
  4. System.out.println("It is" + (j==i) + " that j==i.");

What is the result when the programmerattempts 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 atruntime.

D. Compilation fails because of anerror in line 13.

Answer: D

Integer i = args[0];,無法将String類型的對象付給Integer,應該是13. Integer i = Integer.parseInt( args[0]);

QUESTION 165 Given:

  1. public class BuildStuff {
  2. Boolean test = newBoolean(true);
  3. Integer x = 343;
  4. Integer y = newBuildStuff().go(test, x);
  5. System.out.println(y);
  6. int go(Boolean b, int i) {
  7. if(b) return (i/7);
  8. return (i/49);

A. 7

B. 49

C. 343

D. Compilation fails.

E. An exception is thrown atruntime.

Answer: B b為true,執行第九行,343/7 是 49~~

QUESTION 166 Given:

  1. String csv ="Sue,5,true,3";
  2. Scanner scanner = new Scanner(csv );
  3. scanner.useDelimiter(",");
  4. int age = scanner.nextInt();

A. Compilation fails.

B. After line 15, the value of ageis 5.C. After line 15, the value of age is 3.

D. An exception is thrown at runtime.

下一個讀取的是sue,String類型;

String csv="sue,5,true,3";

Scanner scanner=new Scanner(csv);
    scanner.useDelimiter(",");
    String s=scanner.next();
    System.out.println(s);           

QUESTION 167

  1. import java.util.*;
  2. public class WrappedString {
  3. private String s;
  4. public WrappedString(String s) { this.s = s; }
  5. HashSet<Object> hs = new HashSet<Object>();
  6. WrappedString ws1 = new WrappedString("aardvark");
  7. WrappedString ws2 = new WrappedString("aardvark");
  8. String s1 = new String("aardvark");
  9. String s2 = new String("aardvark");
  10. hs.add(ws1); hs.add(ws2); hs.add(s1); hs.add(s2);
  11. System.out.println(hs.size()); } }

    A. 0

    B. 1

    C. 2

    D. 3

    E. 4

    G. An exception is thrown at runtime.

    WrappedString 的hashCode()是繼承自Object類,而String的hashCode()時重寫過的僅與字元串的内容有關。ws1 和ws2存儲位址不同,是以hashCode()的傳回值不同,而s1和s2字元串的内容相同,哈希值相同。

QUESTION 168

Given a class whose instances, when found in a collection of objects, are sorted by using the compareTo()

method, which two statements are true? (Choose two.)

A. The class implements java.lang.Comparable.

B. The class implements java.util.Comparator.

C. The interface used to implement sorting allows this class to define only one sort sequence.

D. The interface used to implement

QUESTION 169

  1. public class Example {
  2. set.add(new Integer(2));
  3. set.add(new Integer(1));
  4. System.out.println(set);
  5. Which code, inserted at line 4, guarantees that this program will output [1, 2]?

    A. Set set = new TreeSet();

    B. Set set = new HashSet();

    C. Set set = new SortedSet();

    D. List set = new SortedList();

    E. Set set = new LinkedHashSet();

    必須是排好序的集合,是以TreeSet合适。

    而HashSet應該也可以的,Integer類型的hashCode()函數傳回的是相應的int類型的值。如果就隻有一個答案的話TreeSet是最好的選項。

    SortedSet是一個interface,java裡面沒有SortedList類。

  6. public class Person {
  7. private name;
  8. public Person(String name) {
  9. this.name = name;
  10. public int hashCode() {
  11. return 420;

Which statement is true?

A. The time to find the value from HashMap with a Person key depends on the

size of the map.

B. Deleting a Person key from a HashMap will delete all map entries for all keys of type Person.

C. Inserting a second Person object into a HashSet will cause the first Person object to be

removed as a duplicate.

D. The time to determine whether a Person object is contained in a HashSet is constant and does NOT

depend on the size of the map.

B選項:删除HashMap中一個Person對象對應的鍵将會删除這個散列映射表中Person類的全部條目。錯誤,HashMap中Person對象的鍵值不是由Person對象決定的,而是程式員給定的鍵,例如staff.add("123-345", bob),就是把鍵為123-456的bob對象添加到名為staff的HashMap中,因而HashMap允許添加相同的對象。是以說,删除一個鍵對應的Person對象并不會删除所有的條目,他們的key都不同嘛。