ocjp 考试题之十

OCJP视频课堂,具体讲解:https://edu.csdn.net/course/detail/7811

QUESTION 230 Given:

10. class One {

11. public One foo() { return this; }

12. }

13. class Two extends One {

14. public One foo() { return this; }

15. }

16. class Three extends Two {

17. // insert method here

18. }

Whichtwo methods, inserted individually, correctly complete the Three class? (Choosetwo.)

A.   public void foo() {}

B.   public int foo() { return 3; }

C.   public Two foo() { return this; }D. public One foo() {return this; }

E.public Object foo() { return this; }

Answer: CD

 

QUESTION 235 Given:

5.     class Payload {

6.     private int weight;

7.     public Payload (int w) { weight = w; }

8.     public void setWeight(int w) { weight = w; }

9.     public String toString() { return Integer.toString(weight);}

10. }

11. public class TestPayload {

12. static void changePayload(Payload p) { /* insert code */ }

13. public static void main(String[] args) {

14. Payload p = new Payload(200);

15. p.setWeight(1024);

16. changePayload(p);

17. System.out.println("p is " + p);

18. } }

Whichcode fragment, inserted at the end of line 12, produces the output p is 420?

A.   p.setWeight(420);

B.   p.changePayload(420);

C.   p = new Payload(420);

D.   Payload.setWeight(420);

E.   p = Payload.setWeight(420);

Answer: A

Section: (none)

Explanation/Reference:Explanation:

QUESTION 236 Given:

11. public void genNumbers() {

12. ArrayList numbers = new ArrayList();

13. for (int i=0; i<10; i++) {

14. int value = i * ((int) Math.random());

15. Integer intObj = new Integer(value);

16. numbers.add(intObj);

17. }

18. System.out.println(numbers);

19. }

Whichline of code marks the earliest point that an object referenced by intObjbecomes a candidate for garbage collection?

A.Line 16 B. Line 17 C. Line 18

D.   Line 19

E.   The object is NOT a candidate for garbage collection.

Answer: D

Section: (none)

Explanation/Reference:Explanation:

QUESTION 237 Given a correctlycompiled class whose source code is:

1. package com.sun.sjcp;

2. public class Commander {

3. public static void main(String[] args) {

4. // more code here

5. }

6. }

Assumethat the class file is located in /foo/com/sun/sjcp/, the current directory is/foo/, and that the classpath contains "." (current directory). Whichcommand line correctly runs Commander?

A.   java Commander

B.   java com.sun.sjcp.CommanderC. javacom/sun/sjcp/Commander

D. java -cp com.sun.sjcp Commander E.java -cp com/sun/sjcp Commander

Answer: B

Section: (none)

Explanation/Reference:Explanation:

QUESTION 238 Given:

11. public static void test(String str) {

12. int check = 4;

13. if (check = str.length()) {

14. System.out.print(str.charAt(check -= 1) +", ");

15. } else {

16. System.out.print(str.charAt(0) + ", ");

17. }

18. } and the invocation:

21. test("four");

22. test("tee");

23. test("to");

Whatis the result?

A.   r, t, t,

B.   r, e, o,

C.   Compilation fails.

D.   An exception is thrown at runtime.

Answer: C

Section: (none)

Explanation/Reference:Explanation:

QUESTION 239

Adeveloper is creating a class Book, that needs to access class Paper. The Paperclass is deployed in a

JARnamed myLib.jar. Which three, taken independently, will allow the developer touse the Paper class while compiling the Book class? (Choose three.)

A.   The JAR file is located at $JAVA_HOME/jre/classes/myLib.jar.

B.   The JAR file is located at$JAVA_HOME/jre/lib/ext/myLib.jar..

C.   The JAR file is located at /foo/myLib.jar and a classpathenvironment variable is set that includes /foo/myLib.jar/Paper.class.

D.   The JAR file is located at /foo/myLib.jar and a classpathenvironment variable is set that includes /foo/myLib.jar.

E.   The JAR file is located at /foo/myLib.jar and the Book classis compiled using javac -cp /foo/myLib.jar/Paper Book.java.

F.    The JAR file is located at /foo/myLib.jar and the Book classis compiled using javac -d /foo/myLib.jarBook.java

G.   The JAR file is located at /foo/myLib.jar and the Book classis compiled using javac -classpath /foo/myLib.jar Book.java

Answer: BDG

Section: (none)

Explanation/Reference:Explanation:

QUESTION 240 Given:

1.package com.company.application;

2.

3. public class MainClass {

4. public static void main(String[] args) {}

5. } And MainClass exists in the /apps/com/company/applicationdirectory. Assume the CLASSPATHenvironment variable is set to "."(current directory).

Whichtwo java commands entered at the command line will run MainClass? (Choose two.)

A.   java MainClass if run from the /apps directory

B.   java com.company.application.MainClass if run from the /appsdirectory

C.   java -classpath /apps com.company.application.MainClass ifrun from any directory

D.   java -classpath . MainClass if run from the/apps/com/company/application directory

E.   java -classpath /apps/com/company/application:. MainClass ifrun from the /apps directory

F.    java com.company.application.MainClass if run from the/apps/com/company/application directory

Answer: BC

Section: (none)

Explanation/Reference:Explanation:

QUESTION 241 Given:

3.     public class Batman {

4.     int squares = 81;

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

6.     new Batman().go();

7.     }

8.     void go() {

9.     incr(++squares);

10. System.out.println(squares);

11. }

12. void incr(int squares) { squares += 10; }

13. }

Whatis the result?

A.81 B. 82 C. 91

D.   92

E.   Compilation fails.

F.    An exception is thrown at runtime.

Answer: B

Section: (none)

Explanation/Reference:Explanation:

QUESTION 242 Given a cla***epetition:

1.package utils;

2.

3. public class Repetition {

4. public static String twice(String s) { return s + s; }

5. } and given another class Demo:

1. // insert codehere 2.

3. public class Demo {

4. public static void main(String[] args) {

5. System.out.println(twice("pizza"));

6. }

7. }

Whichcode should be inserted at line 1 of Demo.java to compile and run Demo to print"pizzapizza"?

A.   import utils.*;

B.   static import utils.*;

C.   import utils.Repetition.*;

D.   static import utils.Repetition.*;

E.   import utils.Repetition.twice();

F.    import static utils.Repetition.twice;

G.   static import utils.Repetition.twice;

Answer: F

Section: (none)

Explanation/Reference:Explanation:

QUESTION 243 Given:

1. interface DoStuff2 {

2. float getRange(int low, int high); }

3.

4. interface DoMore {

5. float getAvg(int a, int b, int c); }

6.

7.abstract class DoAbstract implements DoStuff2, DoMore { }

8.

9.     class DoStuff implements DoStuff2 {

10. public float getRange(int x, int y) { return 3.14f; } }

11.

12. interface DoAll extends DoMore { 13. float getAvg(int a,int b, int c, int d); } What is the result?

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.F.Compilation fails. Only lines 7 and 13 contain errors.

G.Compilation fails. Lines 7, 12, and 13 contain errors.

Answer: A

Section: (none)

Explanation/Reference:Explanation:

QUESTION 244 Given thatTriangle implements Runnable, and:

31. void go() throws Exception {

32. Thread t = new Thread(new Triangle());

33. t.start();

34. for(int x = 1; x < 100000; x++) {

35. //insert code here

36. if(x%100 == 0) System.out.print("g");

37. } }

38. public void run() {

39. try {

40. for(int x = 1; x < 100000; x++) {

41. // insert the same code here

42. if(x%100 == 0) System.out.print("t");

43. }

44. } catch (Exception e) { }

45. }

Whichtwo statements, inserted independently at both lines 35 and 41, tend to allowboth threads to temporarily pause and allow the other thread to execute?(Choose two.)

A.   Thread.wait();

B.   Thread.join();

C.   Thread.yield();

D.   Thread.sleep(1);

E.   Thread.notify();

Answer: CD

Section: (none)

Explanation/Reference:Explanation:

QUESTION 245

Whichtwo code fragments will execute the method doStuff() in a separate thread?(Choose two.)

A.   new Thread() {

publicvoid run() { doStuff(); }

};

B.   new Thread() {

publicvoid start() { doStuff(); }

};

C.   new Thread() {

publicvoid start() { doStuff(); }

}.run();

D.   new Thread() {

publicvoid run() { doStuff(); }

}.start();

E.   new Thread(new Runnable() {public void run() { doStuff(); }

}).run();

F.    new Thread(new Runnable() {public void run() { doStuff(); }

}).start();

Answer: DF

Section: (none)

Explanation/Reference:Explanation:

QUESTION246 Given: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; }

}

Whichthree changes should be made to adapt this class to be used safely by multiplethreads? (Choose three.)

A.   declare reset() using the synchronized keyword

B.   declare getName() using the synchronized keywordC. declaregetCount() using the synchronized keyword

D.   declare the constructor using the synchronized keyword

E.   declare increment() using the synchronized keyword

Answer: ACE

Section: (none)

Explanation/Reference:Explanation:

QUESTION 247 Given that t1 is areference to a live thread, which is true?

A.   The Thread.sleep() method can take t1 as an argument.

B.   The Object.notify() method can take t1 as an argument.

C.   The Thread.yield() method can take t1 as an argument.

D.   The Thread.setPriority() method can take t1 as an argument.

E.   The Object.notify() method arbitrarily chooses which threadto notify.

Answer: E

QUESTION 249 Given:

1. class TestA {

2. public void start() { System.out.println("TestA");}

3. }

4. public class TestB extends TestA {

5. public void start() { System.out.println("TestB");}

6. public static void main(String[] args) {

7. ((TestA)new TestB()).start();

8. }

9. }

Whatis the result?

A.   TestA

B.   TestB

C.   Compilation fails.

D.   An exception is thrown at runtime.

Answer: B

Section: (none)

Explanation/Reference:Explanation:

QUESTION 250 Which two codefragments correctly create and initialize a static array of int elements?(Choose two.)

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. staticfinal int[] a; static void init() { a = new int[3]; a[0]=100; a[1]=200; }

Answer: AB

Section: (none)

Explanation/Reference:Explanation:

QUESTION 251 Given:

11. public abstract class Shape {

12. private int x;

13. private int y;

14. public abstract void draw();

15.  public void setAnchor(int x, int y) {

16. this.x = x;

17. this.y = y;

18. }

19. }

Whichtwo classes use the Shape class correctly? (Choose two.)

A.   public class Circle implements Shape {private int radius;

}

B.   public abstract class Circle extends Shape {private intradius;

}

C.   public class Circle extends Shape {private int radius;

publicvoid draw();

}

D.   public abstract class Circle implements Shape {private intradius;

publicvoid draw();

}

E.   public class Circle extends Shape {private int radius;public void draw() {/* code here */}

F.    public abstract class Circle implements Shape {private intradius; public void draw() { /* code here */ }

Answer: BE

Section: (none)

Explanation/Reference:Explanation:

QUESTION 252 Given:

10. class Nav{

11. public enum Direction { NORTH, SOUTH, EAST, WEST }

12. }

13. public class Sprite{

14. // insert code here

15. }

Whichcode, inserted at line 14, allows the Sprite class to compile?

A.   Direction d = NORTH;

B.   Nav.Direction d = NORTH;

C.   Direction d = Direction.NORTH;

D.   Nav.Direction d = Nav.Direction.NORTH;

Answer: D

Section: (none)

Explanation/Reference:Explanation:

QUESTION 253 Given:

5.     class Atom {

6.     Atom() { System.out.print("atom "); }

7.     }

8.     class Rock extends Atom {

9.     Rock(String type) { System.out.print(type); }

10. }

11. public class Mountain extends Rock {

12. Mountain() {

13. super("granite ");

14. new Rock("granite ");

15. }

16. public static void main(String[] a) { new Mountain(); }

17. }What is the result?

A.   Compilation fails.

B.   atom granite

C.   granite granite

D.   atom granite granite

E.   An exception is thrown at runtime.

F.    atom granite atom granite

Answer: F

Section: (none)

Explanation/Reference:Explanation:

QUESTION 254 Given:

1.     public class A {

2.     public void doit() {

3.     }

4.     public String doit() {

5.     return "a";

6.     }

7.      public double doit(int x) {

8.     return 1.0;

9.     }

10. }

Whatis the result?

A.   An exception is thrown at runtime.

B.   Compilation fails because of an error in line 7.C.Compilation fails because of an error in line 4.

D.Compilation succeeds and no runtime errors with class A occur.

Answer: C

Section: (none)

Explanation/Reference:Explanation:

QUESTION255 Given:

21. abstract class C1 {

22. public C1() { System.out.print(1); }

23. }

24. class C2 extends C1 {

25. public C2() { System.out.print(2); }

26. }

27. class C3 extends C2 {

28. public C3() { System.out.println(3); }

29. }

30. public class Ctest {

31. public static void main(String[] a) { new C3(); }

32. }

Whatis the result?

A.   3

B.   23C. 32

D.   123

E.   321

F.    Compilation fails.

G.   An exception is thrown at runtime.

Answer: D

Section: (none)

Explanation/Reference:Explanation:

QUESTION 256 Given:

11. public class Rainbow {

12. public enum MyColor {

13. RED(0xff0000), GREEN(0x00ff00), BLUE(0x0000ff);

14. private final int rgb;

15. MyColor(int rgb) { this.rgb = rgb; }

16. public int getRGB() { return rgb; }

17. };

18. public static void main(String[] args) {

19. // insert code here

20. }

21. }

Whichcode 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 other error(s) in the code.

E.   MyColor purple = new MyColor(0xff00ff);

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

Answer: B

Section: (none)

Explanation/Reference:Explanation:

QUESTION 257

Acompany that makes Computer Assisted Design (CAD) software has, within itsapplication, some utility classes that are used to perform 3D rendering tasks.The company's chief scientist has just improved the performance of one of theutility classes' key rendering algorithms, and has assigned a programmer toreplace the old algorithm with the new algorithm. When the programmer beginsresearching the utility classes, she is happy to discover that the algorithm tobe replaced exists in only one class. The programmer reviews that class's API,and replaces the old algorithm with the new algorithm, being careful that herchanges adhere strictly to the class's API. Once testing has begun, theprogrammer discovers that other classes that use the class she changed are nolonger working properly. What design flaw is most likely the cause of these newbugs?

A.   Inheritance

B.   Tight coupling

C.   Low cohesion

D.   High cohesion

E.   Loose coupling

F.    Object immutability

Answer: B

Section: (none)

Explanation/Reference:Explanation:

QUESTION 258 Given:

11. abstract class Vehicle { public int speed() { return 0; }

12. class Car extends Vehicle { public int speed() { return 60;}

13. class RaceCar extends Car { public int speed() { return 150;} ...

21. RaceCar racer = new RaceCar();

22. Car car = new RaceCar();

23. Vehicle vehicle = new RaceCar();

24. System.out.println(racer.speed() + ", " +car.speed()

25. + ", " + vehicle.speed());

Whatis the result?

A.   0, 0, 0

B.   150, 60, 0

C.   Compilation fails.

D.   150, 150, 150

E.   An exception is thrown at runtime.

Answer: D

Section: (none)

Explanation/Reference:Explanation:

QUESTION 259 Given:

11.class Mammal { }

12.

13. class Raccoon extends Mammal {

14. Mammal m = new Mammal();

15. }

16.

17.class BabyRaccoon extends Mammal { } Which four statements are true? (Choosefour.)

A.   Raccoon is-a Mammal.

B.   Raccoon has-a Mammal.

C.   BabyRaccoon is-a Mammal.

D.   BabyRaccoon is-a Raccoon.

E.   BabyRaccoon has-a Mammal.

F.    BabyRaccoon is-a BabyRaccoon.

Answer: ABCF

Section: (none)

Explanation/Reference:Explanation:

QUESTION 260 Given:

10. public class SuperCalc {

11.  protected staticint 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 theresult?

A.   12

4

B.   The code runs with no output.

C.   An exception is thrown at runtime.

D.   Compilation fails because of an error in line 21.E.Compilation fails because of an error in line 22. F. Compilation fails becauseof an error in line 31.

Answer: E

Section: (none)

Explanation/Reference:Explanation:

QUESTION 261 Given:

3.     class Employee {

4.     String name; double baseSalary;

5.     Employee(String name, double baseSalary) {

6.     this.name = name;

7.     this.baseSalary = baseSalary;

8.     }

9.     }

10. public class SalesPerson extends Employee {

11. double commission;

12. public SalesPerson(String name, double baseSalary, doublecommission) {

13. // insert code here

14. }

15. }

Whichtwo code fragments, inserted independently at line 13, will compile? (Choosetwo.)

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);

Answer: AE

Section: (none)

Explanation/Reference:

Explanation:

QUESTION 262 Given:

11. class A {

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");}

22. }

Whatis the 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.

Answer: D

Section: (none)

Explanation/Reference:Explanation:

QUESTION 263 Given a methodthat must ensure that its parameter is not null:

11. public void someMethod(Object value) {

12. // check for null value ...

20. System.out.println(value.getClass());

21. }

What,inserted at line 12, is the appropriate way to handle a null value?

A.   assert value == null;

B.   assert value != null, "value is null";

C.   if (value == null) {throw new AssertionException("valueis null");

}

D.   if (value == null) {throw new IllegalArgumentException("valueis null"); }

Answer: D

Section: (none)

Explanation/Reference:Explanation:

QUESTION 264 Given:

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

12. try {

13. args = null;

14. args[0] = "test";

15. System.out.println(args[0]);

16. } catch (Exception ex) {

17. System.out.println("Exception");

18. } catch (NullPointerException npe) {

19. System.out.println("NullPointerException");

20. }

21. }

Whatis the result?

A.   test

B.   Exception

C.   Compilation fails.

D.   NullPointerException

Answer: C

Section: (none)

Explanation/Reference:Explanation:

QUESTION 265 Given:

11. public static Iterator reverse(List list) {

12. Collections.reverse(list);

13. return list.iterator();

14. }

15. public static void main(String[] args) {

16. List list = new ArrayList();

17. list.add("1"); list.add("2");list.add("3");

18. for (Object obj: reverse(list))

19. System.out.print(obj + ", ");

20. }

Whatis the result?

A.   3, 2, 1,

B.   1, 2, 3,

C.   Compilation fails.

D.   The code runs with no output.

E.   An exception is thrown at runtime.

Answer: C

Section: (none)

Explanation/Reference:Explanation:

QUESTION 266 Given:

11. public class Test {

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

13. int x = 5;

14.  boolean b1 =true;15. boolean b2 = false; 16.

17. if ((x == 4) && !b2 )

18. System.out.print("1 ");

19. System.out.print("2 ");

20. if ((b2 = true) && b1 )

21. System.out.print("3 ");

22. }

23. }

Whatis the result?

A.   2

B.   3

C.   1 2

D.   2 3

E.   1 2 3

F.    Compilation fails.

G.   An exception is thrown at runtime.

Answer: D

Section: (none)

Explanation/Reference:Explanation:

QUESTION 267 Given:

11.class X { public void foo() { System.out.print("X "); } }

12.

13. public class SubB extends X {

14. public void foo() throws RuntimeException {

15. super.foo();

16. if (true) throw new RuntimeException();

17. System.out.print("B ");

18. }

19. public static void main(String[] args) {

20. new SubB().foo();

21. }

22. }

Whatis the result?

A.   X, followed by an Exception.

B.   No output, and an Exception is thrown.

C.   Compilation fails due to an error on line 14.D. Compilationfails due to an error on line 16. E. Compilation fails due to an error on line17. F. X, followed by an Exception, followed by B.

Answer: A

Section: (none)

Explanation/Reference:Explanation:

QUESTION 268

Given:

1. public class Mule {

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

3. boolean assert = true;

4. if(assert) {

5. System.out.println("assert is true");

6. }

7. }

8. }

Whichcommand-line invocations will compile?

A.   javac Mule.java

B.   javac -source 1.3 Mule.javaC. javac-source 1.4 Mule.java

D. javac -source 1.5 Mule.java

Answer: B

Section: (none)

Explanation/Reference:Explanation:

QUESTION 269 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. }

Whatis 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.

Answer: B

Section: (none)

Explanation/Reference:Explanation:

QUESTION 270 Given:

11. public void testIfA() {

12. if (testIfB("True")) {

13. System.out.println("True");

14. } else {

15. System.out.println("Not true");

16. }

17. }

18. public Boolean testIfB(String str) {

19. return Boolean.valueOf(str);

20. }

Whatis the result when method testIfA is invoked?

A.   True

B.   Not true

C.   An exception is thrown at runtime.

D.   Compilation fails because of an error at line 12.E.Compilation fails because of an error at line 19.

Answer: A

QUESTION 273 Given that theelements of a PriorityQueue are ordered according to natural ordering, and:

2.     import java.util.*;

3.     public class GetInLine {

4.     public static void main(String[] args) {

5.     PriorityQueue<String> pq = newPriorityQueue<String>();

6.     pq.add("banana");

7.     pq.add("pear");

8.     pq.add("apple");

9.     System.out.println(pq.poll() + " " + pq.peek());

10. }

11. }

Whatis the result?

A.   apple pear

B.   banana pear

C.   apple apple

D.   apple banana

E.   banana banana

Answer: D

Section: (none)

Explanation/Reference:Explanation:

QUESTION 274 Given:

11. public class Person {

12. private String name, comment;

13. private int age;

14. public Person(String n, int a, String c) {

15. name = n; age = a; comment = c;

16. }

17. public boolean equals(Object o) {

18. if (! (o instanceof Person)) return false;

19,Person p = (Person)o;

20. return age == p.age && name.equals(p.name);

21. }

22. }

Whatis 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;

Answer: B

Section: (none)

Explanation/Reference:Explanation:

QUESTION 275

Aprogrammer must create a generic class MinMax and the type parameter of MinMaxmust implement Comparable. Which implementation of MinMax will compile?

A.   class MinMax<E extends Comparable<E>> {

E            min = null;E max = null; publicMinMax() {}

publicvoid put(E value) { /* store min or max */ }

B.   class MinMax<E implements Comparable<E>> {

E            min = null;E max = null; publicMinMax() {}

publicvoid put(E value) { /* store min or max */ } C. class MinMax<E extendsComparable<E>> {

<E> E min = null;<E> E max = null; public MinMax() {}

public<E> void put(E value) { /* store min or max */ } D. class MinMax<Eimplements Comparable<E>> {

<E> E min = null; <E> E max = null; publicMinMax() {} public <E> void put(E value) { /* store min or max */ }

Answer: A

Section: (none)

Explanation/Reference:Explanation:

QUESTION 276 Given:

3. import java.util.*;

4. public class G1 {

5. public void takeList(List<? extends String> list) {

6. // insert code here

7. }

8. }

Whichthree code fragments, inserted independently at line 6, will compile? (Choosethree.)

A.   list.add("foo");

B.   Object o = list;

C.   String s = list.get(0);

D.   list = new ArrayList<String>();

E.   list = new ArrayList<Object>();

Answer: BCD

Section: (none)

Explanation/Reference:Explanation:

QUESTION 277 Given:

1. public class Drink implements Comparable {

2. public String name;

3. public int compareTo(Object o) {

4. return 0;

5. }

6. }

and:

20. Drink one = new Drink();

21. Drink two = new Drink();

22. one.name= "Coffee";

23. two.name= "Tea";

24. TreeSet set = new TreeSet();

25.  set.add(one);26.set.add(two);

Aprogrammer iterates over the TreeSet and prints the name of each Drink object.What is the result?

A.   Tea

B.   Coffee

C.   CoffeeTea

D.   Compilation fails.

E.   The code runs with no output.

F.    An exception is thrown at runtime.

Answer: B

Section: (none)

Explanation/Reference:Explanation:

QUESTION 278

Whichtwo scenarios are NOT safe to replace a StringBuffer object with aStringBuilder object? (Choose two.)

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 thanone string.

Answer: AB

Section: (none)

Explanation/Reference:Explanation:

QUESTION 279 Given:

1. public class LineUp {

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

3. double d = 12.345;

4. // insert code here

5. }

6. }

Whichcode fragment, inserted at line 4, produces the output | 12.345|?

A.   System.out.printf("|%7d| \n", d);

B.   System.out.printf("|%7f| \n", d);

C.   System.out.printf("|%3.7d| \n", d);D.System.out.printf("|%3.7f| \n", d);

E.   System.out.printf("|%7.3d| \n", d);

F.    System.out.printf("|%7.3f| \n", d);

Answer: F

Section: (none)

Explanation/Reference:Explanation:

QUESTION 280

Giventhat the current directory is empty, and that the user has read and writeprivileges to the current directory, and the following:

1. import java.io.*;

2. public class Maker {

3. public static void main(String[] args) {

4. File dir = new File("dir");

5. File f = new File(dir, "f");

6. }

7. }

Whichstatement is true?

A.   Compilation fails.

B.   Nothing is added to the file system.

C.   Only a new file is created on the file system.

D.   Only a new directory is created on the file system.

E.   Both a new file and a new directory are created on the filesystem.

Answer: B

Section: (none)

Explanation/Reference:Explanation:

QUESTION 281 Given:

1. d is a valid, non-null Date object

2. df is a valid, non-null DateFormat object set to the currentlocale What outputs the current locale'scountry name and the appropriateversion of d's date?

A.   Locale loc = Locale.getLocale();

System.out.println(loc.getDisplayCountry()

+" " + df.format(d));

B.   Locale loc =Locale.getDefault();System.out.println(loc.getDisplayCountry()

+" " + df.format(d));

C.   Locale loc = Locale.getLocale();

System.out.println(loc.getDisplayCountry()

+" " + df.setDateFormat(d));

D.   Locale loc = Locale.getDefault();System.out.println(loc.getDisplayCountry()

+" " + df.setDateFormat(d));

Answer: B

Section: (none)

Explanation/Reference:Explanation:

QUESTION 282 Given:

1.     public class BuildStuff {

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

3.     Boolean test = new Boolean(true);

4.     Integer x = 343;

5.     Integer y = new BuildStuff().go(test, x);

6.     System.out.println(y);

7.     }

8.     int go(Boolean b, int i) {

9.     if(b) return (i/7);

10. return (i/49);

11. }

12. }

Whatis the result?

A.   7

B.   49

C.   343

D.   Compilation fails.

E.   An exception is thrown at runtime.

Answer: B

OCJP视频课堂,具体讲解:https://edu.csdn.net/course/detail/7811


上一篇:OCJP 考试题之九


下一篇:Leetcode之Power of Two