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 "knowledge-based" questions that ask a specific question about a Java rule, to "performance-based" 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. doStuff(1);
4. doStuff(1,2);
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.
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> <FoldPants> <Comp> <Comparator>
Pants FoldPants Comp Comparator
compare compareTo sort order
one.size two.size x.size p.size
c.size (p, c) (c, p) sortList
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. Set<Citrus> c1 = new TreeSet<Citrus>();
5. Set<Orange> o1 = new TreeSet<Orange>();
6. bite(c1);
7. bite(o1);
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.
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)
adult baby mistress, amateur anal, free anal movies, asian, jessica simpsons ass, dennis leary asshole download, bbw tgps, daddy's little girl bdsm, nude beach hotties, beastiality- free sample movie trailers, teens beach bikini candid, bisexual mmf female domination, rock bitch video, nude blonde bathing, pamela anderson blowjobs, redhead bondage, britney spears boob, sexy high heels and thigh high boots, sex with kids in brazil, brunette models legs, thick asses big thick butt, free nude beach cam, anal cartoons, celeb naked, nude woman celebreties, cheerleader wet panties, chicks masturbating, naked chubby ladies, closeups creampies, boy cock, naked coeds in nashville, gangfucked creampies, condom cum, gay dad and son porn, black internet dating site, pussy and dick, lesbians dildo fuck, fat dirty sluts fucking clips, domination shemale, adult sex dvd, ebony anal sex, , , interracial facial, submissive sexual fantasy video, long fat dicks, girls bare feet, female piercing, pvc fetish, ffm blow, gay fisting deectal fisting, flexible containers vs. rigid container, fucking machine movies free, teacher fuck young students, bloods gang initiation, gangbang video, erotic gay stories, free gothic erotica, dad on son sex, group sex party, hairy granny cunts, , free hardcore sex movies, hentai ass, naked cartoon housewives, privat incest, american indian heritage month, naked latin teen, legs and heels pics, lesbian anal, exotic dancer lingerie, live sex on stage, bread machine recipies, spanked maid, drunk ladies and male stripers, shemale masturbation videos, mature lady, festa della mela alte adige, women spanking men discipline, hardcore midget sex, top milf, views on gays in military, incest mom son xxx, golden piss in mouth, xxx free movies, deep nine inch nails, naked old men hairy bears, nipple erection, mary kate nude, anime lesbian nurses, nylon bullwhips, naughty milfs lesbian office, girls like giving oral, fingering a woman orgasm, gay outdoor, , free porn peep shows, hardcore anal penetration, pamela anderson porn, very young pornstars, pregnant women fucking, public slut, horse pussy, rape singapore, free reality sex sites, redhead women nude, free scat stories karen, japanese schoolgirl free galleries, sexy secretary photo, caught having sex, free shaved gallery, shemale hentai, golden shower, dak silicone breast enhancers, boys skinny dipping stories, fucking horny sluts, smoking sucks, public spankings in school, busty stockings, cuckold watches wife stud, suck job, teen slut head oral swallow bj boise, isuzu rodeo engine swaps, tanned secretary orgasm, shark tattoo designs, teen gay, thong models, , young perky tits, toons, sexual toys, tranny lesbian facials, 2002 slp trans am, twink cock sucking, buying japanese school uniforms, cheerleading upskirt, free hairy vagina, free pamela tommy lee video, voyeur sex cam, naughty wife blogs, xxx celebs, young girls naked, blood for blood wasted youth crew t shirts, cougar mountain zoo, samples of how to write a good adult friend finder ad, amateur gallery, anal cum pie, milf asian, whipped ass movies, asshole closeup, bbw fat, bdsm japan, girls naked on beach, retro beastiality, asian bikini model gallery, bisexual men fucking, ass bitch, blonde teen fucked, office blowjobs, bdsm dungeon, perfect boobs, sexy boots in mud, brazil pussy, brunettes having sex, boy butts, naked web cam babes, funny adult cartoons, celeb desktop wallpaper, , lightspeed university cheerleaders, fingering chicks, chubby clit, video closeup of cock entering pussy in full screen, adult cock and ball torture, coed orgy, best creampie sites, cum snowballs, love my dad cock, dating services comparisons, , dirty anime, forced sex domination, brigitte lahaie on dvd, ebony nudes pic, erotica artwork, amateur ethnic boys, face fuckers, , fantasy football sharks, free fat tit women, hands, female celebrity, fetish footwear, ffm internal anal cum eating, latinas fisting, flexible magnetic strip with adhesive back, free sex games, rape fuck, gang bang trailer, gangbang milf gallery, free gay porn galleries, gothic vampire art gallery, dads and twinks, black group sex, female hairy armpits, handjobs mpegs, french hardcore sex hardcore sex, lesbian hentai, amateur housewife voyeur, my big incest family, indian lesbians porn, gay latins, shaving legs, mature lesbian, adult lingerie, live 911 calls, free fucking machine videos, french maid threesome sex, nude male pics, masturbation vegetables fruits, mature amateur, big melons and facials, naked men pics, midget sex galleries, free milf anal, military gay guys, free mom fuck son, black cock in my wife's mouth, free hentai anime movies, fancy lady nails, naked massage, big long nipples, , , womens nylon panties, shoe dangling office girl, free oral sex pics, close up pussy orgasm, outdoor brick bbq pit, evening in paris, party naked, marlin model 60 peep sight, zoomed in cunt penetration videos up close, harry potter porn, sexy pornstar fucking, pregnant jacuzzi, exposing cock in public, pussy stretching, the duke university rape case, amateur reality lesbian teen, hot redhead teenager, mc scat cat, schoolgirl stripped and whipped, secretary oral sex, babysitter sex, shaved petite, fat shemales, peeing in the shower, silicone pussy, skinny girl ass, naughty sluts, smoking models videos, sexual spanking pictures, nudes in stockings, stud dog contract, aim icons yankees suck, gays who swallow piss, marine swap meet, tanned white ass, colorful tattoos, teen virgins, young teens flashing thongs, throat gagging blowjobs, tiny pussy thumb or gallery, small tits, young toon, children learning toys, tiny tranny, teen tran, teen twink galleries, free gay uniform boys, my upskirt shots, hot vagina, jackson sunbathing video, voyeur thong, my sexy wife, xxx hardcore porn, illegal young girls, harvest party ideas for teen youth groups, zoobilee zoo theme songs,