Sei sulla pagina 1di 12

11 medium-hard mock exam questions from the updated K & B Java certification book, for Java 5 (coming soon).

(Kathy Sierra / Bert Bates)

Notes: 1) The real exam will tell you how many answers to choose from; we don't, because we want this quiz to be a little harder than the real exam. 2) The real exam contains drag and drop questions; we formatted question #9 to reflect the way a drag and drop question might be asked (but without the whole dragging and dropping part) 3) These questions are a good representation of the tone, style, difficulty, and content of the real exam. If you've taken earlier versions of the Sun Certified Java Programmer exam, you'll notice a big difference--a switch from "knowledgebased" questions that ask a specific question about a Java rule, to "performancebased" questions where you must analyze code and then apply your knowledge of Java to figure out what's going on. You'll find less language trivia (or API memorization) questions, and more that see if you know how the language really works. Also, the questions we've presented here are geared toward the new Tiger/Java 5 features. 4) You're right--there is some code on the exam that you'd be fired if you tried to write code like that on the job. The exam developers tried to reduce this as much as possible, but we want you to be prepared for the real thing. 5) The book includes explanations for the answers -- we didn't do that here because we haven't finished that part. These questions *have* been beta-tested, so we're fairly confident that the answers are correct... but you know how that goes. If you don't understand an answer, check the javaranch.com SCJP certification forum, where the questions are being discussed.There are no dumb questions, so please ask in the forums! But we (Kathy and Bert) are not able to answer individual questions online right now, so javaranch is your best place for answers. 5) We're working on the book update as fast as we can! It will shipping from Amazon in late September or early October. Sorry for the delay... good luck with your studies.

6) These questions are copyrighted 2005, Kathy Sierra & Bert Bates. Our publisher requires that we say that. You can point people here, and you can print them out and share them with others, but please do not reproduce them in another book or mock exam.

1. Given:

enum Horse { PONY(10), // insert code here HORSE(15); Horse(int hands) { this.height = hands; this.weight = hands * 100; } int height; int weight; int getWeight() { return weight; } void setWeight(int w) { weight = w; } } class Stable { public static void main(String [] hay) { Horse h = Horse.ICELANDIC; System.out.println(h.getWeight() + " " + h.height); } } Which, inserted independently at '// insert code here', produces the output: 800 13 ? (Choose all that apply.) A). ICELANDIC(13) { weight = 800; },

B). ICELANDIC(13) { setWeight(800); }, C). ICELANDIC(13) { this.weight = 800; }, D). ICELANDIC(13) { public int getWeight() { return 800; } }, E). None of the above code will produce the specified output. F). Because of other code errors, none of the above will compile.

2. Given:

1. class Voop { 2. public static void main(String [] args) { 3. 4. 5. } 6. // insert code here 7. } Which, inserted at line 6, will compile? (Choose all that apply.) A). static void doStuff(int... doArgs) { } B). static void doStuff(int[] doArgs) { } C). static void doStuff(int doArgs...) { } D). static void doStuff(int... doArgs, int y) { } E). static void doStuff(int x, int... doArgs) { } F). None of the above code fragments will compile. doStuff(1); doStuff(1,2);

3. Given:

class Bird { { System.out.print("b1 "); } public Bird() { System.out.print("b2 "); } } class Raptor extends Bird {

static { System.out.print("r1 "); } public Raptor() { System.out.print("r2 "); } { System.out.print("r3 "); } static { System.out.print("r4 "); } } class Hawk extends Raptor { public static void main(String[] args) { System.out.print("pre "); new Hawk(); System.out.println("hawk "); } } What is the result? A). pre b1 b2 r3 r2 hawk B). pre b2 b1 r2 r3 hawk C). pre b2 b1 r2 r3 hawk r1 r4 D). r1 r4 pre b1 b2 r3 r2 hawk E). r1 r4 pre b2 b1 r2 r3 hawk F). pre r1 r4 b1 b2 r3 r2 hawk G). pre r1 r4 b2 b1 r2 r3 hawk H). The order of output cannot be predicted I). Compilation fails

4. Which are most typically thrown by an API developer or an application developer as opposed to being thrown by the JVM? (Choose all that apply.)

A). ClassCastException B). IllegalStateException C). NumberFormatException D). IllegalArgumentException E). ExceptionInInitializerError

5. Given:

class Eggs { int doX(Long x, Long y) { return 1; } int doX(long... x) { return 2; } int doX(Integer x, Integer y) { return 3; } int doX(Number n, Number m) { return 4; } public static void main(String[] args) { new Eggs().go(); } void go() { short s = 7; System.out.print(doX(s,s) + " "); System.out.println(doX(7,7)); } } What is the result? A). 1 1 B). 2 1 C). 3 1 D). 4 1 E). 1 3 F). 2 3 G). 3 3 H). 4 3

6. Given:

import java.io.*; class Player {

Player() { System.out.print("p"); } } class CardPlayer extends Player implements Serializable { CardPlayer() { System.out.print("c"); } public static void main(String[] args) { CardPlayer c1 = new CardPlayer(); try { FileOutputStream fos = new FileOutputStream("play.txt"); ObjectOutputStream os = new ObjectOutputStream(fos); os.writeObject(c1); os.close(); FileInputStream fis = new FileInputStream("play.txt"); ObjectInputStream is = new ObjectInputStream(fis); CardPlayer c2 = (CardPlayer) is.readObject(); is.close(); } catch (Exception x ) { } } } What is the result? A). pc B). pcc C). pcp D). pcpc E). Compilation fails F). An exception is thrown at runtime

7. Given:

import java.util.regex.*; class Regex2 { public static void main(String[] args) { Pattern p = Pattern.compile(args[0]); Matcher m = p.matcher(args[1]);

boolean b = false; while(b = m.find()) { System.out.print(m.start() + m.group()); } } } And the command line: java Regex2 "\d*" ab34ef What is the result? A). 234 B). 334 C). 2334 D). 0123456 E). 01234456 F). 12334567 G). Compilation fails

8. Given: bw is a reference to a valid BufferedWriter And the snippet: 15 BufferedWriter b1 = new BufferedWriter(new File("f")); 16. BufferedWriter b2 = new BufferedWriter(new FileWriter("f1")); 17. BufferedWriter b3 = new BufferedWriter(new PrintWriter("f2")); 18. BufferedWriter b4 = new BufferedWriter(new BufferedWriter(bw)); What is the result? A). Compilation succeeds B). Compilation fails due only to an error on line 15.

C). Compilation fails due only to an error on line 16. D). Compilation fails due only to an error on line 17. E). Compilation fails due only to an error on line 18. F). Compilation fails due to errors on multiple lines.

9. Using the fragments below, complete the following code so that it compiles and prints "40 36 30 28". Note, you may use a fragment from zero to many times. CODE: import java.util.*; class Pants { int size; Pants(int s) { size = s; } } public class FoldPants { List ________ p = new ArrayList ________ (); class Comp implements Comparator ________ { public int ________(Pants one, Pants two) { return ________ - ________; } } public static void main(String[] args) { new FoldPants().go(); } void go() { p.add(new Pants(30)); p.add(new Pants(36)); p.add(new Pants(28)); p.add(new Pants(40)); Comp c = new Comp(); Collections. ________ ________; for( ________ x : p) System.out.print(________ + " "); } } FRAGMENTS:

<Pants> Pants compare one.size two.size c.size (p, c)

<FoldPants> FoldPants compareTo x.size (c, p) sortList

<Comp> Comp sort

<Comparator> Comparator

order

p.size

10. Given the following three source files:

1. package org; 2. public class Robot { } 1. package org.ex; 2. public class Pet { } 1. package org.ex.why; 2. public class Dog { int foo = 5; } And the following incomplete source file: // insert code here public class MyClass { Robot r; Pet p; Dog d; void go() { int x = d.foo; } } Which statement(s) must be added for MyClass to compile? (Choose all that apply.) A). package org; B). import org.*; C). package org.*;

D). package org.ex; E). import org.ex.*; F). import org.ex.why; G). package org.ex.why; H). package org.ex.why.Dog;

11. Given:

1. import java.util.*; 2. public class Fruits { 3. public static void main(String [] args) { 4. 5. 6. 7. 8. } 9. // insert code here 10. } 11. class Citrus { } 12. class Orange extends Citrus { } Which, inserted independently at line 9, will compile? (Choose all that apply.) A). public static void bite(Set<?> s) { } B). public static void bite(Set<Object> s) { } C). public static void bite(Set<Citrus> s) { } D). public static void bite(Set<? super Citrus> s) { } E). public static void bite(Set<? super Orange> s) { } F). public static void bite(Set<? extends Citrus> s) { } G). Because of other errors in the code, none of these will compile. Set<Citrus> c1 = new TreeSet<Citrus>(); Set<Orange> o1 = new TreeSet<Orange>(); bite(c1); bite(o1);

Answers:

1. D is correct. (objective 1.1) 2. A and E use valid var-args syntax. (objective 1.1) 3. D is correct. Note: you'll probably never see this many choices on the real exam! (objective 1.3) 4. B, C and D are correct. (objective 2.6) 5. H is correct. (objective 3.1) 6. C is correct. (objective 3.3) 7. E is correct. (objective 3.5) 8. B is correct. (objective 3.2) 9. The correct code is: import java.util.*; class Pants { int size; Pants(int s) { size = s; } } public class FoldPants { List<Pants> p = new ArrayList<Pants>(); class Comp implements Comparator <Pants> { public int compare(Pants one, Pants two) { return two.size - one.size; } } public static void main(String[] args) { new FoldPants().go(); } void go() { p.add(new Pants(30)); p.add(new Pants(36)); p.add(new Pants(28)); p.add(new Pants(40)); Comp c = new Comp(); Collections.sort(p, c); for(Pants x : p) System.out.print(x.size + " "); } }

(objective 6.5) 10. B, E, and G are required. (objective 7.5) 11. A, E, and F are correct uses of generics with (bounded) wildcards. (objective 6.4)

Potrebbero piacerti anche