OCJP 考试题之九

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

QUESTION 177 Given:

1.     class TestException extends Exception { }

2.     class A {

3.     public String sayHello(String name) throws TestException {

4.     if(name == null) throw new TestException();

5.     return "Hello " + name;

6.     }

7.     }

8.     public class TestA {

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

10. new A().sayHello("Aiko");

11. }

12. }

Whichstatement is true?

A.   Compilation succeeds.

B.   Class A does not compile.

C.   The method declared on line 9 cannot be modified to throwTestException.

D.   TestA compiles if line 10 is enclosed in a try/catch blockthat catches TestException.

Answer: D

Section: (none)

Explanation/Reference:Explanation:

QUESTION 178 Given:

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

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

13. if (i > 6) break;

14. }

15. System.out.println(i);

16. }

Whatis the result?

A.   6

B.   7

C.   10

D.   11

E.   Compilation fails.

F.    An exception is thrown at runtime.

Answer: E

Section: (none)

Explanation/Reference:Explanation:

QUESTION 179 Given:

3.     public class Breaker {

4.     static String o = "";

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

6.     z:

7.     o = o + 2;

8.     for(int x = 3; x < 8; x++) {

9.     if(x==4) break;

10. if(x==6) break z;

11. o = o + x;

12. }

13. System.out.println(o);

14. }

15. }

Whatis the result?

A.   23

B.   234C. 235

D. 2345 E. 2357

F.    23457

G.   Compilation fails.

Answer: G

Section: (none)

Explanation/Reference:Explanation:

QUESTION 180 Given:

5.     class A {

6.     void foo() throws Exception { throw new Exception(); }

7.     }

8.     class SubB2 extends A {

9.     void foo() { System.out.println("B "); }

10. }

11. class Tester {

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

13. A a = new SubB2();

14. a.foo();

15. }

16. }

Whatis the result?

A.   B

B.   B, followed by an Exception.

C.   Compilation fails due to an error on line 9.

D.   Compilation fails due to an error on line 14.

E.   An Exception is thrown with no other output.

Answer: D

Section: (none)

Explanation/Reference:Explanation:

QUESTION 181 Given:

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

12. String str = "null";

13. if (str == null) {

14. System.out.println("null");

15. } else (str.length() == 0) {

16. System.out.println("zero");

17. } else {

18. System.out.println("some");

19. }

20. }

Whatis the result?

A.   null

B.   zero

C.   some

D.   Compilation fails.

E.   An exception is thrown at runtime.

Answer: D

Section: (none)

Explanation/Reference:Explanation:

QUESTION182 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 183 Given:

11. static void test() {

12. try {

13. String x = null;

14. System.out.print(x.toString() + " ");

15. }

16. finally { System.out.print("finally "); }

17. }

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

19. try { test(); }

20. catch (Exception ex) { System.out.print("exception"); }

21. }

Whatis the result?

A.   null

B.   finally

C.   null finallyD. Compilation fails.

E.finally exception

Answer: E

Section: (none)

Explanation/Reference:Explanation:

QUESTION 184 Given:

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

Whatis 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 IllegalStateException occurs at runtime.

Answer: D

Section: (none)

Explanation/Reference:Explanation:

QUESTION 185 Which two codefragments are most likely to cause a *Error? (Choose two.)

A. int []x = {1,2,3,4,5}; for(int y = 0; y < 6; y++)System.out.println(x[y]); B. static int[] x = {7,6,5,4}; static { x[1] = 8;x[4] = 3; }

C.    for(int y = 10; y < 10; y++)doStuff(y);

D.    void doOne(int x) { doTwo(x); }void doTwo(int y) { doThree(y); } voiddoThree(int z) { doTwo(z); }

E.   for(int x = 0; x < 1000000000; x++)doStuff(x);

F.    void counter(int i) { counter(++i); }

Answer: DF

Section: (none)

Explanation/Reference:Explanation:

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

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

Answer: D

Section: (none)

Explanation/Reference:Explanation:

QUESTION 187 Given:

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

17. }

Whatis the result?

A.   three

B.   other

C.   An exception is thrown at runtime.

D.   Compilation fails because of an error on line 12.E.Compilation fails because of an error on line 13. F. Compilation fails becauseof an error on line 15.

Answer: A

Section: (none)

Explanation/Reference:Explanation:

QUESTION 188 Given:

21. class Money {

22. private String country = "Canada";

23. public String getC() { return country; }

24. }

25. class Yen extends Money {

26. public String getC() { return super.country; }

27. }

28. public class Euro extends Money {

29. public String getC(int x) { return super.getC(); }

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

31. System.out.print(new Yen().getC() + " " + newEuro().getC());

32. }

33. }

Whatis the result?

A.   Canada

B.   null CanadaC. Canada null

D.   Canada Canada

E.   Compilation fails due to an error on line 26.F. Compilationfails due to an error on line 29.

Answer: E

Section: (none)

Explanation/Reference:Explanation:

QUESTION 189 Given:

11. class ClassA {}

12.  class ClassBextends ClassA {} 13. class ClassC extends ClassA {} and:

21. ClassA p0 = new ClassA();

22. ClassB p1 = new ClassB();

23. ClassC p2 = new ClassC();

24. ClassA p3 = new ClassB();

25. ClassA p4 = new ClassC();

Whichthree are valid? (Choose three.)

A. p0 = p1; B. p1= p2; C. p2 = p4;

D.   p2 = (ClassC)p1;

E.   p1 = (ClassB)p3;

F.    p2 = (ClassC)p4;

Answer: AEF

Section: (none)

Explanation/Reference:Explanation:

QUESTION 190 Which threestatements are true? (Choose three.)

A.   A final method in class X can be abstract if and only if Xis abstract.

B.   A protected method in class X can be overridden by anysubclass of X.

C.   A private static method can be called only within otherstatic methods in class X.

D.   A non-static public final method in class X can beoverridden in any subclass of X.

E.   A public static method in class X can be called by a subclassof X without explicitly referencing theclass X.

F.    A method with the same signature as a private final methodin class X can be implemented in asubclass of X.

G.   A protected method in class X can be overridden by asubclass of X only if the subclass is in the samepackage as X.

Answer: BEF

Section: (none)

Explanation/Reference:Explanation:

QUESTION 191 Given:

10. interface A { void x(); }

11. class B implements A { public void x() {} public void y() {}}

12. class C extends B { public void x() {} }

And:

20. java.util.List<A> list = newjava.util.ArrayList<A>();

21. list.add(new B());

22. list.add(new C());

23. for (A a : list) {

24. a.x();

25. a.y();

26. }

Whatis the result?

A.   The code runs with no output.

B.   An exception is thrown at runtime.

C.   Compilation fails because of an error in line 20.D.Compilation fails because of an error in line 21. E. Compilation fails becauseof an error in line 23. F. Compilation fails because of an error in line 25.

Answer: F

Section: (none)

Explanation/Reference:Explanation:

QUESTION192 Given:

1. package test;2.

3. class Target {

4. public String name = "hello";

5. }

Whatcan directly access and change the value of the variable name?

A.   any class

B.   only the Target class

C.   any class in the test package

D.   any class that extends Target

Answer: C

Section: (none)

Explanation/Reference:Explanation:

 

 

QUESTION 194

Ateam of programmers is involved in reviewing a proposed design for a newutility class. After some discussion, they realize that the current designallows other classes to access methods in the utility class that should beaccessible only to methods within the utility class itself. What design issuehas the team discovered?

A.   Tight coupling

B.   Low cohesion

C.   High cohesion

D.   Loose coupling

E.   Weak encapsulation

F.    Strong encapsulation

Answer: E

Section: (none)

Explanation/Reference:Explanation:

QUESTION 195 Given:

5. class Thingy { Meter m = new Meter(); }

6. class Component { void go() {System.out.print("c"); } }

7. class Meter extends Component { void go() {System.out.print("m"); } }

8.

9.     class DeluxeThingy extends Thingy {

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

11. DeluxeThingy dt = new DeluxeThingy();

12. dt.m.go();

13. Thingy t = new DeluxeThingy();

14. t.m.go();

15. }

16. }

Whichtwo are true? (Choose two.)

A.   The output is mm.

B.   The output is mc.

C.   Component is-a Meter.

D.   Component has-a Meter.

E.   DeluxeThingy is-a Component.

F.    DeluxeThingy has-a Component.

Answer: AF

Section: (none)

Explanation/Reference:Explanation:

QUESTION 196

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

42. } ...

50. class Cat implements Jumper{

51. public void jump() {}

52. }

Whichthree are true? (Choose three.)

A.   Cat is-a Animal

B.   Cat is-a JumperC. Dog is-a Animal

D.   Dog is-a Jumper

E.   Cat has-a Animal

F.    Beagle has-a Tail

G.   Beagle has-a Jumper

Answer: BCF

Section: (none)

QUESTION 198 Given a validDateFormat object named df, and

16. Date d = new Date(0L);

17. String ds = "December 15, 2004";

18. // insert code here What updates d's value with the daterepresented by ds?

A.   18. d = df.parse(ds);

B.   18. d = df.getDate(ds);

C.   18. try {

19.   d = df.parse(ds);

20.   } catch(ParseException e) { }; D. 18. try {

19. d = df.getDate(ds);

20. } catch(ParseException e) { };

Answer: C

Section: (none)

Explanation/Reference:Explanation:

QUESTION 199

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:

QUESTION200 Given:

11. String test = "a1b2c3";

12. String[] tokens = test.split("\\d");

13. for(String s: tokens) System.out.print(s + " ");

Whatis the result?

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 at runtime.

Answer: A

Section: (none)

Explanation/Reference:

QUESTION 201 Given:

1. public class TestString3 {

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

3. // insert code here

5. System.out.println(s);

6. }

7. }

Whichtwo code fragments, inserted independently at line 3, generate the output 4247?(Choose two.)

A.   String s = "123456789";s =(s-"123").replace(1,3,"24") - "89";

B.   StringBuffer s = new StringBuffer("123456789");C.delete(0,3).replace(1,3,"24").delete(4,6);

D.   StringBuffer s = new StringBuffer("123456789");

E.   substring(3,6).delete(1,3).insert(1, "24");

F.    StringBuilder s = newStringBuilder("123456789");G. substring(3,6).delete(1,2).insert(1,"24");

H.   StringBuilder s = new StringBuilder("123456789");

I.      delete(0,3).delete(1,3).delete(2,5).insert(1,"24");

Answer: BE

Section: (none)

Explanation/Reference:Explanation:

QUESTION 202 Given:

11. String test = "Test A. Test B. Test C.";

12. // insert code here

13. String[] result = test.split(regex);

Whichregular expression, inserted at line 12, correctly splits test into "TestA", "Test B", and "Test C"?

A.   String regex = "";

B.   String regex = " ";

C.   String regex = ".*";

D.   String regex = "\\s";

E.   String regex = "\\.\\s*";

F.    String regex = "\\w[ \.] +";

Answer: E

Section: (none)

Explanation/Reference:Explanation:

QUESTION 203 Which statement istrue?

A.   A class's finalize() method CANNOT be invoked explicitly.

B.   super.finalize() is called implicitly by any overridingfinalize() method.

C.   The finalize() method for a given object is called no morethan once by the garbage collector.

D.   The order in which finalize() is called on two objects isbased on the order in which the two objectsbecame finalizable.

Answer: C

Section: (none)

Explanation/Reference:Explanation:

QUESTION 204 Given:

11. public class ItemTest {

12. private final int id;

13.  publicItemTest(int id) { this.id = id; } 14. public void updateId(int newId) { id =newId; }

15.

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

17. ItemTest fa = new ItemTest(42);

18. fa.updateId(69);

19. System.out.println(fa.id);

20. }

21. }

Whatis the result?

A.   Compilation fails.

B.   An exception is thrown at runtime.

C.   The attribute id in the ItemTest object remains unchanged.

D.   The attribute id in the ItemTest object is modified to thenew value.

E.   A new ItemTest object is created with the preferred value inthe id attribute.

Answer: A

Section: (none)

Explanation/Reference:Explanation:

QUESTION 205 Given:

11. interface DeclareStuff {

12. public static final int EASY = 3;

13. void doStuff(int t); }

14. public class TestDeclare implements DeclareStuff {

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

16. int x = 5;

17. new TestDeclare().doStuff(++x);

18. }

19. void doStuff(int s) {

20. s += EASY + ++s;

21. System.out.println("s " + s);

22. }

23. }

Whatis the result?

A.s 14 B. s 16 C. s 10

D.   Compilation fails.

E.   An exception is thrown at runtime.

Answer: D

Section: (none)

 

QUESTION 207 Given:

11. public class Commander {

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

13. String myProp = /* insert code here */

14. System.out.println(myProp);

15. }

16.  }and the commandline:

java-Dprop.custom=gobstopper Commander Which two, placed on line 13, will producethe output gobstopper? (Choose two.)

A.   System.load("prop.custom");

B.   System.getenv("prop.custom");

C.   System.property("prop.custom");

D.   System.getProperty("prop.custom");

E.   System.getProperties().getProperty("prop.custom");

Answer: DE

Section: (none)

Explanation/Reference:Explanation:

QUESTION 208 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 DoStuffimplements 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 209 Given:

3.     interface Fish { }

4.     class Perch implements Fish { }

5.     class Walleye extends Perch { }

6.     class Bluegill { }

7.     public class Fisherman {

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

9.     Fish f = new Walleye();

10. Walleye w = new Walleye();

11. Bluegill b = new Bluegill();

12. if(f instanceof Perch) System.out.print("f-p ");

13. if(w instanceof Fish) System.out.print("w-f ");

14. if(b instanceof Fish) System.out.print("b-f ");

15. }

16. }

Whatis the result?

A.   w-f

B.   f-p w-f

C.   w-f b-f

D.   f-p w-f b-f

E.   Compilation fails.

F.    An exception is thrown at runtime.

Answer: B

Section: (none)

 

QUESTION 211 Given:

1.package com.company.application;

2.

3. public class MainClass {

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

5. }

AndMainClass exists in the /apps/com/company/application directory. Assume theCLASSPATH environment variable is set to "." (current directory).Which two java commands entered at the command line will run MainClass? (Choosetwo.)

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 212 Given:

12. import java.util.*;

13. public class Explorer2 {

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

15. TreeSet<Integer> s = new TreeSet<Integer>();

16. TreeSet<Integer> subs = new TreeSet<Integer>();

17. for(int i = 606; i < 613; i++)

18. if(i%2 == 0) s.add(i);

19. subs = (TreeSet)s.subSet(608, true, 611, true);

20. s.add(629);

21. System.out.println(s + " " + subs);

22. }

23. }

Whatis the result?

A.   Compilation fails.

B.   An exception is thrown at runtime.

C.   [608, 610, 612, 629] [608, 610]

D.   [608, 610, 612, 629] [608, 610, 629]E. [606, 608, 610, 612,629] [608, 610]

F.[606, 608, 610, 612, 629] [608, 610, 629]

Answer: E

Section: (none)

Explanation/Reference:Explanation:

QUESTION 213 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 214 Given apre-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. }

Whatthree changes allow the class to be used with generics and avoid an uncheckedwarning? (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)".

Answer: ACF

Section: (none)

Explanation/Reference:Explanation:

QUESTION 215 Given:

34. HashMap props = new HashMap();

35. props.put("key45", "some value");

36. props.put("key12", "some other value");

37. props.put("key39", "yet another value");

38. Set s = props.keySet();

39. // insert code here What, inserted at line 39, will sort thekeys in the props HashMap?

A.   Arrays.sort(s);

B.   s = new TreeSet(s);

C.   Collections.sort(s);

D.   s = new SortedSet(s);

Answer: B

Section: (none)

Explanation/Reference:Explanation:

QUESTION 216 Given:

11. public class Person {

12. private String name;

13. public Person(String name) {

14. this.name = name;

15. }

16. public boolean equals(Object o) {

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

18. Person p = (Person) o;

19. return p.name.equals(this.name);

20. }

21. }

Whichstatement is true?

A.   Compilation fails because the hashCode method is notoverridden.

B.   A HashSet could contain multiple Person objects with thesame name.

C.   All Person objects will have the same hash code because thehashCode method is not overridden.

D.   If a HashSet contains more than one Person object withname="Fred", then removing another Person,also withname="Fred", will remove them all.

Answer: B

Section: (none)

Explanation/Reference:Explanation:

QUESTION 217 Given:

3. import java.util.*;

4. public class Hancock {

5. // insert code here

6. list.add("foo");

7. }

8. }

Whichtwo code fragments, inserted independently at line 5, will compile withoutwarnings? (Choose two.)

A.   public void addStrings(List list) {

B.   public void addStrings(List<String> list) {

C.   public void addStrings(List<? super String> list) {

D.   public void addStrings(List<? extends String> list) {

Answer: BC

Section: (none)

Explanation/Reference:Explanation:

QUESTION 218 Given:

1.     public class Threads4 {

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

3.     new Threads4().go();

4.     }

5.     public void go() {

6.     Runnable r = new Runnable() {

7.     public void run() {

8.     System.out.print("foo");

9.     }

10. };

11. Thread t = new Thread(r);

12. t.start();

13. t.start();

14. }

15. }

Whatis the result?

A.   Compilation fails.

B.   An exception is thrown at runtime.

C.   The code executes normally and prints "foo".

D.   The code executes normally, but nothing is printed.

Answer: B

Section: (none)

Explanation/Reference:Explanation:

QUESTION 219 Given:

1. public class TestOne {

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

3. Thread.sleep(3000);

4. System.out.println("sleep");

5. }

6. } What is the result?

A.   Compilation fails.

B.   An exception is thrown at runtime.

C.   The code executes normally and prints "sleep".

D.   The code executes normally, but nothing is printed.

Answer: C

Section: (none)

Explanation/Reference:Explanation:

QUESTION 220 Given:

1.     public class TestSeven extends Thread {

2.     private static int x;

3.     public synchronized void doThings() {

4.     int current = x;

5.     current++;

6.     x = current;

7.     }

8.     public void run() {

9.     doThings();

10. }

11.}

Whichstatement is true?

A.   Compilation fails.

B.   An exception is thrown at runtime.

C.   Synchronizing the run() method would make the classthread-safe.

D.   The data in variable "x" are protected fromconcurrent access problems.

E.   Declaring the doThings() method as static would make theclass thread-safe.

F.    Wrapping the statements within doThings() in asynchronized(new Object()) { } block would make theclass thread-safe.

Answer: E

Section: (none)

Explanation/Reference:Explanation:

QUESTION 221 Which two codefragments 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:

QUESTION 222 Given:

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

12. Object obj = new int[] { 1, 2, 3 };

13. int[] someArray = (int[])obj;

14. for (int i : someArray) System.out.print(i + " ");

15. }

Whatis the result?

A.   1 2 3

B.   Compilation fails because of an error in line 12.C.Compilation fails because of an error in line 13.

D.   Compilation fails because of an error in line 14.

E.   A ClassCastException is thrown at runtime.

Answer: A

Section: (none)

Explanation/Reference:Explanation:

QUESTION 223 Given:

10. interface Data { public void load(); }

11. abstract class Info { public abstract void load(); }

Whichclass correctly uses the Data interface and Info class?

A.   public class Employee extends Info implements Data { publicvoid load() { /*do something*/ }

}

B.   public class Employee implements Info extends Data { publicvoid load() { /*do something*/ }

}

C.   public class Employee extends Info implements Data { publicvoid load(){ /*do something*/ }public void Info.load(){ /*do something*/ }

}

D.   public class Employee implements Info extends Data { publicvoid Data.load(){ /*do something*/ }public void load(){ /*do something*/ }

}

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

}

F.    public class Employee extends Info implements Data{publicvoid Data.load() { /*do something*/ }

publicvoid Info.load() { /*do something*/ }

}

Answer: A

Section: (none)

Explanation/Reference:Explanation:

QUESTION 224 Given:

11. public static void parse(String str) {

12. try {

13. float f = Float.parseFloat(str);

14. } catch (NumberFormatException nfe) {

15. f = 0;

16. } finally {

17. System.out.println(f);

18. }

19. }

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

21. parse("invalid");

22. }

Whatis the result?

A.   0.0

B.   Compilation fails.

C.   A ParseException is thrown by the parse method at runtime.

D.   A NumberFormatException is thrown by the parse method atruntime.

Answer: B

Section: (none)

Explanation/Reference:Explanation:

QUESTION 225

Given 11. public interface Status {

12. /* insert code here */ int MY_VALUE = 10;

13. }

Whichthree are valid on line 12? (Choose three.)

A.   final

B.   static

C.   native

D.   public

E.   private

F.    abstract

G.   protected

Answer: ABD

Section: (none)

Explanation/Reference:Explanation:

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

Whatis the result?

A. test B. null

C.   An exception is thrown at runtime.

D.   Compilation fails because of an error in line 1.E.Compilation fails because of an error in line 4. F. Compilation fails becauseof an error in line 5.

Answer: A

Section: (none)

Explanation/Reference:Explanation:

QUESTION 227 Given:

11.public interface A { public void m1(); }

12.

13. class B implements A { }

14. class C implements A { public void m1() { } }

15. class D implements A { public void m1(int x) { } }

16. abstract class E implements A { }

17.  abstract class Fimplements A { public void m1() { } } 18. abstract class G implements A {public void m1(int x) { } }

Whatis the result?

A.   Compilation succeeds.

B.   Exactly one class does NOT compile.

C.   Exactly two classes do NOT compile.

D.   Exactly four classes do NOT compile.

E.   Exactly three classes do NOT compile.

Answer: C

Section: (none)

Explanation/Reference:Explanation:

QUESTION 228 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

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


上一篇:二进制枚举子集


下一篇:ocjp 考试题之十