Sun Certified Programmer Practice Exam

Sun Certified Programmer Practice Exam

by Barry Boone

Email: ???

For as long as it will be there you' ll find the original document here.
I made a copy and you'll find it in the following section.

Here are the rules:

Allow 2 hours and 15 minutes to complete this exam. You should turn off the telephone, go someplace you won’t be disturbed, check the time on your watch, and begin. Don’t bring any books with you, because you won’t have them during the test. You can take breaks, but don’t look anything up. You can have a piece of scratch paper and a pen or pencil, but you can’t have any crib sheets!

If you get 49 questions right, you’ve hit the 70% mark, and you’ve passed. Good luck!


Question 1: Which of the following class definitions defines a legal abstract class?

Select all right answers.

a)  
class Animal { abstract void growl(); }
 b)  
abstract Animal {abstract void growl();}
 c)  
class abstract Animal {abstract void growl();}
 d)  
abstract class Animal {abstract void growl();}
 e)  
abstract class Animal {abstract void growl() {System.out.println("growl");}}

Question 2: For an object to be a target for a Thread, that object must be of type:

Fill in the blank.
 

Question 3: What is the proper way of defining a class named Key so that it cannot be subclassed?

a)  
class Key { }
 b)  
abstract final class Key { }
 c)  
native class Key { }
 d)  
class Key {final;}
 e)  
final class Key { }

Question 4: What modes are legal for creating a new RandomAccessFile object?

Select all valid answers.

  1. "w"
  2. "r"
  3. "x"
  4. "rw"
  5. "xrw"
 

Question 5: Given the following code:
class Tester { public static void main(String[] args) { CellPhone cell = new CellPhone();

cell.emergency();

}
}

class Phone {

final void dial911() { // code to dial 911 here . . . } }

class CellPhone extends Phone {

void emergency() { dial911(); } }
What will happen when you try to compile and run the Tester class?

  1. The code will not compile because Phone is not also declared as final.
  2. The code will not compile because you cannot invoke a final method from a subclass.
  3. The code will compile and run fine.
  4. The code will compile but will throw a NoSuchMethodException when Tester is run.
  5. Phone and CellPhone are fine, but Tester will not compile because it cannot create an instance of a class that derives from a class defining a final method.

Question 6: Which assignments are legal?

Select all valid answers.

  1. long test = 012;
  2. float f = -412;
  3. int other = (int)true;
  4. double d = 0x12345678;
  5. short s = 10;
 

Question 7: Given this class definitions:
abstract class Transaction implements Runnable { }

class Deposit extends Transaction {

protected void process() { addAmount(); }

void undo(int i) {

System.out.println("Undo"); }
}
What will happen if we attempted to compile the code?

Select the one right answer.

  1. This code will not compile because the parameter i is not used in undo().
  2. This code will not compile because there is no main() method.
  3. This code will not compile because Deposit must be an abstract class.
  4. This code will not compile because Deposit is not declared public.
  5. Everything will compile fine.

Question 8: Which exception might wait() throw?

Fill in the blank.
 
 

Question 9: Which of the following are not Java keywords:
abstract double int static
boolean else interface super
break extends long superclass
byte final native switch
case finally new synchronized
catch float null this
char for open throw
class goto  package throws
close  if private transient
const implements protected try
continue import public void
default instanceof return volatile
do integer short while
 

Select all valid answers.

  1. superclass
  2. goto
  3. open
  4. close
  5. integer
  6. goto, import
  7. goto, superclass, open, close
  8. they are all valid keywords

Question 10: Which of the following represents an octal number?

Select all that apply.

  1. 0x12
  2. 32O
  3. 032
  4. (octal)2
  5. 1
 

Question 11: What will appear in the standard output when you run the Tester class?
class Tester {

int var;

Tester(double var) { this.var = (int)var; }

Tester(int var) {

this("hello"); }

Tester(String s) {

this();

System.out.println(s);

}

Tester() {

System.out.println("good-bye"); }

public static void main(String[] args) {

Tester t = new Tester(5); }
}

  1. nothing
  2. "hello"
  3. 5
  4. "hello" followed by "good-bye"
  5. "good-bye" followed by "hello"

Question 12: Write a line of code to use the String’s substring() method to obtain the substring "lip" from a String instance named s that is set to "tulip".

Fill in the blank.
 
 

Question 13: There are a number of labels in the source code below. These are labeled a through j. Which label identifies the earliest point where, after that line has executed, the object referred to by the variable first may be garbage collected?
class Riddle { public static void main(String[] args) { String first, second;

String riddle;

if (args.length < 2)

return; a: first = new String(args[0]);

b: second = new String(args[1]);

c: riddle = "When is a " + first;

d: first = null;

e: riddle += " like a " + second + "?";

f: second = null;

g: System.out.println(riddle);

h: args[0] = null;

i: args[1] = null;

j: 

} }
Select the one right answer.

 

  1. d:
  2. e:
  3. h:
  4. i:
  5. j:

Question 14: What are the range of values for a variable of type byte?

Select the one right answer.

  1. -2^7 to 2^7 - 1
  2. 0 to 2^7
  3. -2^15 to 2^15
  4. -2^15 to 2^15 -1
  5. -2^15 - 1 to 2^15

Question 15: What will happen when you try to compile and run the following program?
class Car { int milesPerGallon;

int index;

Car(int mpg) {

milesPerGallon = mpg;

index = 0;

}

Car() {

}

public static void main(String[] args) {

int index;

Car c = new Car(25);

if (args.length > 0)

if (args[index].equals("Hiway")) milesPerGallon*= 2; System.out.println("mpg: " + milesPerGallon); }
Select the one right answer.

  1. The code compiles and displays "mpg: 50" if the command-line argument is "Hiway". If the command-line argument is not "Hiway", the code displays "mpg: 25".
  2. The code compiles and displays "mpg: 50" if the command-line argument is "Hiway". If the command-line argument is not "Hiway", the code throws an ArrayIndexOutOfBoundsException.
  3. The code does not compile because the automatic variable named index has not been initialized.
  4. The code does not compile because milesPerGallon has not been initialized.
  5. The code does not compile because the no-args constructor is not written correctly.

Question 16: What will happen when you compile and run this program:
class Array { public static void main(String[] args) { int length = 100;

int[] d = new int[length];

for (int index = 0; index < length; index++) 

System.out.println(d[index]); } }
Select the one right answer.

  1. The code will not compile because the int[] array is not declared correctly.
  2. The code will compile but will throw an IndexArrayOutOfBoundsException when it runs and nothing will appear in the standard output.
  3. The code will display the numbers 0 through 99 in the standard output, and then throw an IndexOutOfBoundsException.
  4. The code will compile but the println() method will throw a NoSuchMethodException.
  5. This code will work fine and display 100 zeroes in the standard output.

Question 17: What is the result of attempting to compile and run the following class?
class Ar { public static void main(String[] args) { int[] seeds = new int[3];

for (int i = 0; i < seeds.length; i++)

System.out.println(i); } }
Select all valid answers.

  1. 0
  2. 1
  3. 2
  4. 3
  5. the program does not compile because the seeds array is not initialized

Question 18: What method name can you use from the applet to read a String passed to an applet via the <param> tag? (Supply the method name only, without parameters.)

Fill in the blank.
 

Question 19: Given these class definitions:
class Superclass { }

class Subclass1 extends Superclass { }

and these objects:
Superclass a = new Superclass();

Subclass1 b = new Subclass1();

which of the following explains the result of the statement:
a = b;

Select the one right answer.

  1. Illegal at compile time
  2. Legal at compile time but possibly illegal at runtime
  3. Definitely legal at runtime
 
DISCLAIMER
   
 

 

1