You are on page 1of 8

SCJP 5 QUESTIONS

For more details on SUN Certifications, visit http://sunjavasnips.blogspot.com/

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)

You might also like