ocjp 考试题之十[通俗易懂]

ocjp 考试题之十

大家好,又见面了,我是你们的朋友全栈君。

QUESTION 230 Given:

  1. class One {

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

  3. }

  4. class Two extends One {

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

  6. }

  7. class Three extends Two {

  8. // insert method here

  9. }

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:

  1. class Payload {

  2. private int weight;

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

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

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

  6. }

  7. public class TestPayload {

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

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

  10. Payload p = new Payload(200);

  11. p.setWeight(1024);

  12. changePayload(p);

  13. System.out.println(“p is ” + p);

  14. } }

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:

  1. public void genNumbers() {

  2. ArrayList numbers = new ArrayList();

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

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

  5. Integer intObj = new Integer(value);

  6. numbers.add(intObj);

  7. }

  8. System.out.println(numbers);

  9. }

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:

  1. public static void test(String str) {

  2. int check = 4;

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

  4. System.out.print(str.charAt(check -= 1) +”, “);

  5. } else {

  6. System.out.print(str.charAt(0) + “, “);

  7. }

  8. } and the invocation:

  9. test(“four”);

  10. test(“tee”);

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

  1. public class MainClass {

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

  3. } 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:

  1. public class Batman {

  2. int squares = 81;

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

  4. new Batman().go();

  5. }

  6. void go() {

  7. incr(++squares);

  8. System.out.println(squares);

  9. }

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

  11. }

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.

  1. public class Repetition {

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

  3. } and given another class Demo:

  4. // insert codehere 2.

  5. public class Demo {

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

  7. System.out.println(twice(“pizza”));

  8. }

  9. }

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. interface DoMore {

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

7.abstract class DoAbstract implements DoStuff2, DoMore { }

8.

  1. class DoStuff implements DoStuff2 {

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

  3. 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:

  1. void go() throws Exception {

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

  3. t.start();

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

  5. //insert code here

  6. if(x%100 == 0) System.out.print(“g”);

  7. } }

  8. public void run() {

  9. try {

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

  11. // insert the same code here

  12. if(x%100 == 0) System.out.print(“t”);

  13. }

  14. } catch (Exception e) { }

  15. }

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:

  1. public abstract class Shape {

  2. private int x;

  3. private int y;

  4. public abstract void draw();

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

  6. this.x = x;

  7. this.y = y;

  8. }

  9. }

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:

  1. class Nav{

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

  3. }

  4. public class Sprite{

  5. // insert code here

  6. }

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:

  1. class Atom {

  2. Atom() { System.out.print(“atom “); }

  3. }

  4. class Rock extends Atom {

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

  6. }

  7. public class Mountain extends Rock {

  8. Mountain() {

  9. super(“granite “);

  10. new Rock(“granite “);

  11. }

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

  13. }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:

  1. abstract class C1 {

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

  3. }

  4. class C2 extends C1 {

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

  6. }

  7. class C3 extends C2 {

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

  9. }

  10. public class Ctest {

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

  12. }

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:

  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() { return rgb; }

  7. };

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

  9. // insert code here

  10. }

  11. }

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:

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

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

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

  4. RaceCar racer = new RaceCar();

  5. Car car = new RaceCar();

  6. Vehicle vehicle = new RaceCar();

  7. System.out.println(racer.speed() + “, ” +car.speed()

    • “, ” + 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.

  1. class Raccoon extends Mammal {

  2. Mammal m = new Mammal();

  3. }

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:

  1. public class SuperCalc {

  2. protected staticint multiply(int a, int b) { return a * b;} 12. } and:

  3. public class SubCalc extends SuperCalc{

  4. public static int multiply(int a, int b) {

  5. int c = super.multiply(a, b);

  6. return c;

  7. }

  8. }and:

  9. SubCalc sc = new SubCalc ();

  10. System.out.println(sc.multiply(3,4));

  11. 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:

  1. class Employee {

  2. String name; double baseSalary;

  3. Employee(String name, double baseSalary) {

  4. this.name = name;

  5. this.baseSalary = baseSalary;

  6. }

  7. }

  8. public class SalesPerson extends Employee {

  9. double commission;

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

  11. // insert code here

  12. }

  13. }

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:

  1. class A {

  2. public void process() { System.out.print(“A,”); }

  3. class B extends A {

  4. public void process() throws IOException {

  5. super.process();

  6. System.out.print(“B,”);

  7. throw new IOException();

  8. }

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

  10. try { new B().process(); }

  11. catch (IOException e) { System.out.println(“Exception”);}

  12. }

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:

  1. public void someMethod(Object value) {

  2. // check for null value …

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

  4. }

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:

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

  2. try {

  3. args = null;

  4. args[0] = “test”;

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

  6. } catch (Exception ex) {

  7. System.out.println(“Exception”);

  8. } catch (NullPointerException npe) {

  9. System.out.println(“NullPointerException”);

  10. }

  11. }

Whatis the result?

A. test

B. Exception

C. Compilation fails.

D. NullPointerException

Answer: C

Section: (none)

Explanation/Reference:Explanation:

QUESTION 265 Given:

  1. public static Iterator reverse(List list) {

  2. Collections.reverse(list);

  3. return list.iterator();

  4. }

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

  6. List list = new ArrayList();

  7. list.add(“1”); list.add(“2”);list.add(“3”);

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

  9. System.out.print(obj + “, “);

  10. }

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:

  1. public class Test {

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

  3. int x = 5;

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

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

  6. System.out.print(“1 “);

  7. System.out.print(“2 “);

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

  9. System.out.print(“3 “);

  10. }

  11. }

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.

  1. public class SubB extends X {

  2. public void foo() throws RuntimeException {

  3. super.foo();

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

  5. System.out.print(“B “);

  6. }

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

  8. new SubB().foo();

  9. }

  10. }

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:

  1. public static Collection get() {

  2. Collection sorted = new LinkedList();

  3. sorted.add(“B”); sorted.add(“C”);sorted.add(“A”);

  4. return sorted;

  5. }

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

  7. for (Object obj: get()) {

  8. System.out.print(obj + “, “);

  9. }

  10. }

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:

  1. public void testIfA() {

  2. if (testIfB(“True”)) {

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

  4. } else {

  5. System.out.println(“Not true”);

  6. }

  7. }

  8. public Boolean testIfB(String str) {

  9. return Boolean.valueOf(str);

  10. }

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:

  1. import java.util.*;

  2. public class GetInLine {

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

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

  5. pq.add(“banana”);

  6. pq.add(“pear”);

  7. pq.add(“apple”);

  8. System.out.println(pq.poll() + ” ” + pq.peek());

  9. }

  10. }

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:

  1. public class Person {

  2. private String name, comment;

  3. private int age;

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

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

  6. }

  7. public boolean equals(Object o) {

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

19,Person p = (Person)o;

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

  2. }

  3. }

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:

  1. import java.util.*;

  2. public class G1 {

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

  4. // insert code here

  5. }

  6. }

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:

  1. Drink one = new Drink();

  2. Drink two = new Drink();

  3. one.name= “Coffee”;

  4. two.name= “Tea”;

  5. TreeSet set = new TreeSet();

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

转载于:https://blog.51cto.com/2096101/2136486

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/107438.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号