Practice Test for the

Sun Certifed Java 1.1 Programmer Exam

This sample test emphasizes Java 1.1-specific questions. It is very likely that the test you take from Sun will have a smaller ratio of Java 1.1-specific questions. That is, most of the questions will be equally valid for Java 1.0. Java Certification for Programmers and Developers.

Every effort was made to ensure this practice test is accurate. However, if in working your way through this practice test you find any questions or answers that you do not feel are right, feel free to email me.

Some rules

You should allow two hours to complete these sixty questions. Note that some of the questions have more than one right answer -- just as they do on the real exam. You need a score of 70%, or at least 42 correct, to pass.


Question 1: Given the following class definition:

class A {
   protected int i;
   A(int i) {
      this.i = i;
   }
}

Which of the following would be a valid inner class for this class?

Select all valid answers.

a)

class B {
}

b)

class B extends A {
}

c)

class B {
   B() { 
      System.out.println("i = " + i);
   }
}

d)

class B {
   class A {
   }
}

e)

class A {
}


Question 2: What statements are true concerning the method notify() that is used in conjunction with wait()?

Select all valid answers.

a) if there is more than one thread waiting on a condition, only the thread that has been waiting the longest is notified

b) if there is more than one thread waiting on a condition,there is no way to predict which thread will be notifed

c) notify() is defined in the Thread class

d) it is not strictly necessary to own the lock for the object you invoke notify() for

e) notify() should only be invoked from within a while loop


Question 3: Given the following class:

class Counter {
   public int startHere = 1;
   public int endHere = 100;
   public static void main(String[] args) {
      new Counter().go();
   }
   void go() {
      // A
      Thread t = new Thread(a);
      t.start();
   }
}

What block of code can you replace at line A above so that this program will count from startHere to endHere?

Select all valid answers.

a)
Runnable a = new Runnable() {
   public void run() {
      for (int i = startHere; i <= endHere; i++) {
         System.out.println(i);
      }
   }
};

b)
a implements Runnable {
   public void run() {
      for (int i = startHere; i <= endHere; i++) {
         System.out.println(i);
      }
  }
};

c)
Thread a = new Thread() {
   public void run() {
      for (int i = startHere; i <= endHere; i++) {
         System.out.println(i);
      }
   }
};


Question 4: What is written to the standard output given the following statement:

System.out.println(4 | 7);

Select the one right answer.

a) 4

b) 5

c) 6

d) 7

e) 0


Question 5: Given the following class:

class Counter {
   public static void main(String[] args) {
      Thread t = new Thread(new CounterBehavior());
      t.start();
   }
}

Which of the following is a valid definition of CounterBehavior that would make Counter’s main() method count from 1 to 100, counting once per second?

Select the one right answer.

a)This class is an inner class to Counter:

class CounterBehavior {
   for (int i = 1; i <= 100; i++);
      try {
         System.out.println(i);
         Thread.sleep(1000);
      } catch (InterruptedException x) {}
   }
}

b) This class is an inner class to Counter:

class CounterBehavior implements Runnable {
   public void run() {
      for (int i = 1; i <= 100; i++);
         try {
            System.out.println(i);
            Thread.sleep(1000);
         } catch (InterruptedException x) {}
      }
   }
}

c) This class is a top-level class:

static class CounterBehavior implements Runnable {
   public void run() {
      try {
         for (int i = 1; i <= 100; i++) {
            System.out.println(i);
            Thread.sleep(1000);
         }
      } catch (InterruptedException x) {}
   }
}


Question 6: Given the following class definition:

class A {
   public int x;
   private int y;
   class B {
      protected void method1() {
      }
      class C {
         private void method2() {
         }
      }
   }
}

class D extends A {
   public float z;
}

 

What can method2() access directly, without a reference to another instance?

 

Select all valid answers.

a) the variable x defined in A

b) the variable y defined in A

c) method1 defined in B

d) the variable z defined in D


Question 7: You have an 8-bit file using the character set defined by ISO 8859-8. You are writing an application to display this file in a TextArea. The local encoding is already set to 8859-8. How can you write a chunk of code to read the first line from this file?

You have three variables accessible to you:

Select all valid answers.

a)

InputStreamReader reader = new InputStreamReader(stream, "8859-8");
BufferedReader buffer = new BufferedReader(reader);
s = buffer.readLine();

b)

InputStreamReader reader = new InputStreamReader(stream);
BufferedReader buffer = new BufferedReader(reader);
s = buffer.readLine();

c)

InputStreamReader reader = new InputStreamReader(myfile, "8859-8");
BufferedReader buffer = new BufferedReader(reader);
s = buffer.readLine();

d)

InputStreamReader reader = new InputStreamReader(myfile);
BufferedReader buffer = new BufferedReader(reader);
s = buffer.readLine();

e)

FileReader reader = new FileReader(myfile);
BufferedReader buffer = new BufferedReader(reader);
s = buffer.readLine();


Question 8: How can you write a line of code for an applet’s init() method that determines how wide the applet is?

Select all valid answers.

a) int width = this.getY();
b) int width = this.getSize().w;
c) int width = getSize();
d) int width = getSize().w;
e) int width = getWidth();


Question 9: For a variable width font, how "wide" is a TextField created using the expression:

new TextField(20)

Select the one right answer.

a) 20 times the average of all the characters in the font used for this TextField object

b) 20 times the width of the letter M

c) 20 times the width of the letter a

d) 20 inches

e) 20 picas


Question 10: Given this interface definition:

interface A {
   int method1(int i);
   int method2(int j);
}

which of the following classes implement this interface and is not abstract?

Select all valid answers.

a)
class B implements A {
   int method1() { }
   int method2() { }
}

b)
class B {
   int method1(int i) { }
   int method2(int j) { }
}

c)
class B implements A {
   int method1(int i) { }
   int method2(int j) { }
}

d)
class B extends A {
   int method1(int i) { }
   int method2(int j) { }
}

e)
class B implements A { 
   int method2(int j) { }
   int method1(int i) { }
}


Question 11: Given the following code:

import java.awt.*;
import java.awt.event.*;
public class MyApplet extends java.applet.Applet {
   public void init() {
      Button b = new Button("Button1");
      b.addMouseListener(new ClickHandler());
      add(b);
   }
   class ClickHandler extends MouseAdapter {
      public void mouseClicked(MouseEvent evt) {
         // A
      }
   }
}

What line of code at A writes the mouse’s horizontal location to the standard output at the time of the event?

 

Fill in the blank.


Question 12: Given the same code as in question 10, how can you write a line of code at A that will place the Button object into a variable named mybutton that is already defined to be a reference to a Button object?

 

Fill in the blank.


Question 13: Which Listener interface can you implement to be able to respond to the user hitting the enter key after typing into a TextField object?

 

Fill in the blank.


Question 14: What is written to the standard output as the result of executing the following statements?

Boolean b1 = new Boolean(true);
Boolean b2 = new Boolean(true);

if (b1 == b2)
    if (b1.equals(b2))
       System.out.println("a");
    else
       System.out.println("b");
else
   if (b1.equals(b2))
      System.out.println("c");
   else
      System.out.println("d");

Select the one right answer.

a) a

b) b

c) c

d) d


Question 15: Which Listener interfaces can you add to a TextArea object?

a) TextListener

b) ActionListener

c) MouseMotionListener

d) MouseListener

e) ComponentListener


Question 16: What appears in the standard output if the method named problem() in the code below throws an instance of class Exception when the method named trythis() is invoked?

public void trythis() {
   try {
      System.out.println("1");
      problem();
   } catch (RuntimeException x) {
      System.out.println("2");
      return;
   } catch (Exception x) {
      System.out.println("3");
      return;
   } finally {
      System.out.println("4");
   }
   System.out.println("5");
}

Select all valid answers.

a) "1"

b) "2"

c) "3"

d) "4"

e) "5"


Question 17: What is the type of the Event object passed to the mouseDragged() method defined in a class that implements java.awt.event.MouseMotionListener (that is, what is the class name for the argument)?

 

Fill in the blank.


Question 18: Examine the following switch block:

char mychar = 'c';
switch (mychar) {
   default:
   case 'a': System.out.println("a"); break;
   case 'b': System.out.println("b"); break;
}

Which of the following questions are definitely true?

Select all valid answers.

a) This switch block is illegal, because only integers can be used in the switch statement.

b) This switch block is fine.

c) This switch block is illegal, because the default statement must come last.

d) When this code runs, nothing is written to the standard output.

e) When this code runs, the letter "a" is written to the standard output.


Question 19: What keyword must appear in a method declaration (followed by the name of the exception) when that method might cause an exception to be thrown and that method does not handle the exception?

 

Fill in the blank.


Question 20: Which statements accurately describe the following line of code?

Select all valid answers.

String[][] s = new String[10][];

a) This line of code is illegal.

b) s is a two-dimensional array containing 10 rows and 10 columns

c) s is an array of 10 arrays.

d) Each element in s is set to ""

e) Each element in s is uninitialized and must be initialized before it is referenced.


Question 21: What will happen if you try to compile and run the following class?

class Test {
   static int myArg = 1;
   public static void main(String[] args) {
      int myArg;
      System.out.println(myArg);
   }
}

Select the one right answer.

a) This code compiles and displays 0 in the standard output when run.

b) This code compiles and displays 1 in the standard output when run.

c) This code does not compile because you cannot define a local variable named the same as a static variable.

d) This code does not compile because the local variable is used before it is initialized.


Question 22: Which declarations for the main() method in a stand-alone program are NOT valid?

Select all valid answers.

a) public static void main()

b) public static void main(String[] string)

c) public static void main(String args)

d) static public int main(String[] args)

e) static void main(String[] args)


Question 23: Which of the following identifiers are ILLEGAL?

Select all valid answers.

a) #_pound

b) _underscore

c) 5Interstate

d) Interstate5

e) _5_


Question 24: If the user invokes a stand-alone application by typing:

java YourApp 1 2 3

and the main() method defines its String[] parameter as args, how can you access the number 2 using args?

Fill in the blank.


Question 25: Which interface implementations can you add as listeners for a TextField object?

Select all valid answers.

a) ActionListener

b) FocusListener

c) MouseMotionListener

d) WindowListener

e) ContainerListener


Question 26: What must be true for the RunHandler class so that instances of RunHandler can be used as written in the code below:

class Test {
   public static void main(String[] args) {
      Thread t = new Thread(new RunHandler());
      t.start();
   }
}

Select all valid answers.

a) RunHandler must implement the java.lang.Runnable interface.

b) RunHandler must extend the Thread class.

c) RunHandler must provide a run() method declared as public and returning void.

d) RunHandler must provide an init() method.


Question 27: To determine if you can invoke addContainerListener() for a component referenced using a variable named c, which expression(s) can you evaluate that will give you a true or false answer to this questions?

Select all valid answers.

a) c == Container

b) c.equals(Class.Container)

c) c instanceof Container

d) c instanceof Component

e) c implements Container


Question 28: Write a statement for a constructor that invokes the no-args, default constructor in its superclass.

 

Fill in the blank.


Question 29: What is written to the standard output given the following statement:

System.out.println(4 & 7);

Select the one right answer.

a) 4

b) 5

c) 6

d) 7

e) 0


Question 30: What will the following block of code write to the standard output when it is executed?

int i = 3;
int j = 0;
double k = 3.2;
if (i < k)
   if (i == j)
      System.out.println(i);
   else
      System.out.println(j);
else
   System.out.println(k);

Select the one right answer.

a) 3

b) 0

c) 3.2

d) none of these


Question 31: How can you use the String method indexOf() to determine which position the letter 'C' is in given this String:

String s = "ABCDE";

Write a complete statement in your answer, but you do not have to assign the letter you retrieve to another variable.

 

Fill in the blank.


Question 32: Given that the variable g references a valid Graphics object, what does the following statement do?

g.fillRect(2, 3, 10, 20);

Select all valid answers.


a) draw the outline of a rectangle in the current background color

b) draw the outline of a rectangle in the current foreground color

c) fill in a rectangle using the current background color

d) fill in a rectangle using the current foreground color

e) fill in a rectangle in black


Question 33: Describe the following applet.

import java.applet.Applet;
import java.awt.event.*;
import java.awt.*;
public class MyApplet extends Applet {
   Button b1, b2;
   public void init() {
      ActionListener a = new ActionListener() {
         public void actionPerformed(ActionEvent evt) {
            if (evt.getSource() == b1) {
               b1.setEnabled(false);
               b2.setEnabled(true);
            } else {
               b1.setEnabled(true);
               b2.setEnabled(false);
            }
         }
      };
      b1 = new Button("1");
      b1.addActionListener(a);
      add(b1);
      b2 = new Button("2");
      b2.addActionListener(a);
      add(b2);
   }
}

Select all valid answers.

a) Nothing appears in the applet

b) One button appears in the applet but does nothing

c) Two buttons appear in the applet

d) When the user clicks a button, nothing happens

e) When the user clicks a button, it becomes disabled

f) When a user clicks a button, the other button becomes enabled


Question 34: The method setBackground() defined for the Graphics class:

Select all valid answers.

a) takes an integer value

b) takes an instance of class Color

c) takes an instance of a Component subclass

d) sets the drawing mode for the associated Component object

e) sets the drawing color for the associated Component object

f) changes the background color for the associated Component object


Question 35: What does the following program do when it is run with the command:

java Mystery Mighty Mouse

class Mystery {
   public static void main(String[] args) {
      Changer c = new Changer();
      c.method(args);
      System.out.println(args[0] + " " + args[1]);
   }
   static class Changer {
      void method(String[] s) {
         String temp = s[0];
         s[0] = s[1];
         s[1] = temp;
      }
   }
}

Select the one right answer.

a) This program causes an ArrayIndexOutOfBoundsException to be thrown

b) This program runs but does not write anything to the standard output

c) This program writes "Mighty Mouse" to the standard output

d) This program writes "Mouse Mighty" to the standard output


Question 36: What happens when you try to compile and run the following program?

class Mystery {
   String s;
   public static void main(String[] args) {
      Mystery m = new Mystery();
      m.go();
   }
   void Mystery() {
      s = "constructor";
   }
   void go() {
      System.out.println(s);
   }
}

Select the one right answer.

a) this code will not compile

b) this code compiles but throws an exception at runtime

c) this code runs but nothing appears in the standard output

d) this code runs and "constructor" in the standard output

e) this code runs and writes "null" in the standard output


Question 37: What can you write at the comment //A in the code below so that this program writes the word "running" to the standard output?

class RunTest implements Runnable {
   public static void main(String[] args) {
      RunTest rt = new RunTest();
      Thread t =new Thread(rt);
      //A
   }
   public void run() {
      System.out.println("running");
   }
   void go() {
      start(1);
   }
   void start(int i) {
   }
}

Select all valid answers.

a) System.out.println("running");

b) rt.start();

c) rt.go();

d) rt.start(1);


Question 38: What order can you place the following pieces of a source file in so that the source file will compile without errors or warnings?

//A
import java.applet.*;

//B
class Helper {
}

//C
package myclasses;

//D
public class MyApplet extends java.applet.Applet {
}

Select all valid answers.

a) A, B, C, D

b) A, C, B, D

c) C, A, B, D

d) C, A, D, B

e) C, B, A, D


Question 39: Analyze these two consequetive lines of code:

float f = 3.2;
int i = f;

Select all valid answers.

a) this code would not compile

b) this code would compile and i would be set to 3

c) the second line could compile if it were written instead as:

   int i = (byte)f;

d) the first line could compile if it were written instead as:

   float f = 3.2F;


Question 40: Construct an array of 3 String objects containing the strings "a", "b", and "c" using the { } notation. Call the String by the variable name s.

 

Fill in the blank.


Question 41: What is the final value of temp in this sequence?

long temp = (int)3.9;
temp %= 2;

a) 0

b) 1

c) 2

d) 3

e) 4

Select the one right answer.


Question 42: Analyze this line of code:

if (5 & 7 > 0 && 5 | 2) System.out.println("true");

Select the one right answer.

a) this line of code will not compile

b) this code will compile but nothing will appear in the standard output

c) this code will compile and write the word "true" in the standard output


Question 43: Create a List object that allows multiple selections and displays 5 rows at a time.

 

Start by writing:

List l =

in your answer.

 

Fill in the blank.


Question 44: What will the user interface look like in an applet given the following init() method?

public void init() {
   setLayout(new BorderLayout());
   add("East", new Button("hello"));
}

Select the one right answer.

a) Nothing will appear in the applet

b) A button will appear in the applet set in the exact center

c) A button will appear on the left side of the applet

d) A button will appear on the right side of the applet

e) A button will fill the entire applet


Question 45: Choose all true statements about the paint() method defined in the Component class:

Select all valid answers.

a) it is protected

b) it takes an instance of class Graphics

c) it is static

d) it is invoked automatically whenever you minimize and then maximize a component, such as a window

e) there is also a version that takes an int


Question 46: Given this ActionListener:

class HandleClick implements ActionListener {
   public void actionPerformed(ActionEvent evt) {
      // A
   }
}

What line of code can you write at A that will make a component referenced by c disappear from the display?

 

Fill in the blank.


Question 47: Analyze the following code:

class WhatHappens implements Runnable {
   public static void main(String[] args) {
      Thread t = new Thread(this);
      t.start();
   }
   public void run() {
      System.out.println("hi");
   }
}

Select the one right answer.

a) This program does not compile

b) This program compiles but nothing appears in the standard output

c) This program compiles and the word "hi" appears in the standard output, once

d) This program compiles and the word "hi" appears continuously in the standard output until the user hits control-c to stop the program


Question 48: What is wrong with the following code?

final class First {
   private int a = 1;
   int b = 2;
}

class Second extends First {
   public void method() {
      System.out.println(a + b);
   }
}

Select all valid answers.

a) You cannot invoke println() without passing it a String

b) Since a is private, no classes other than First can access it

c) Second cannot extend First

d) final is not a valid keyword for a class


Question 49: Analyze the following two classes.

class First {
   static int a = 3;
}

final class Second extends First {
   void method() {
      System.out.println(a);
   }
}

Select the one right answer.

a) Class First compiles, but class Second does not

b) Class Second compiles, but class First does not

c) Neither class compiles

d) Both classes compile, and if method() is invoked, it writes 3 to the standard output

e) Both classes compile, but if method() is invoked, it throws an exception


Question 50: Why won’t the following class compile?

class A {
   private int x;
   public static void main(String[] args) {
      new B();
   }
   class B {
      B() {
         System.out.println(x);
      }
   }
}

Select the one right answer.

a) Class B tries to access a private variable defined in its ouer class.

b) Class A attempts to create an instance of B when there is no current instance of class A.

c) Class B’s constructor must be public.


Question 51: Analyze the following code.

void looper() {
   int x = 0;
one:
   while (x < 10) {
two:
      System.out.println(++x);
      if (x > 3)
         break two;
   }
}

Select all valid answers.

a) This code compiles

b) This code does not compile

c) This method writes the number 0 to the standard output

d) the numbers 1 and 2 to the standard output

e) the number 3 to the standard output

f) the number 4 to the standard output

g) the numbers 5 through 9 to the standard output

h) the number 10 to the standard output


Question 52: What appears in the standard output when the method named testing is invoked?

void testing() {
one:
two:
   for (int i = 0; i < 3; i++) {
three:
      for (int j = 10; j < 30; j+=10) {
         System.out.println(i + j);
         if (i > 2)
            continue one;
      }
   }
}

Select all valid answers.

a) 10 and 20

b) 11 and 21

c) 12 and 22

d) 13 and 23

e) 30, 31, 32, 33


Question 53: What will the result be for the following block of code when it is executed?

int i = 3;
int j = 0;
float k = 3.2F;
long m = -3;
if (Math.ceil(i) < Math.floor(k))
   if (Math.abs(i) == m)
      System.out.println(i);
   else
      System.out.println(j);
else
   System.out.println(Math.abs(m) + 1);

 

Select the one right answer.

a) 3

b) 0

c) -3

d) 4

e) none of these


Question 54: The ISO code for the language you are interested in is 8859-5. Assume you have a stream in a variable named mystream that’s associated with a file generated in this 8-bit character set. If the default conversion to Unicode in your environment is for the encoding 8859-1, how can you create a new instance of InputStreamReader that will perform the conversion from ISO 8859-5 to Unicode, automatically? (Start by writing the keyword new and do not add a semicolon at the end.)

 

Fill in the blank.


Question 55: What is written to the standard output as the result of executing the following statements?

Boolean b1 = new Boolean(true);
Boolean b2 = new Boolean(true);
Object obj1 = (Object)b1;
Object obj2 = (Object)b2;

if (obj1 == obj2)
   if (obj1.equals(obj2))
      System.out.println("a");
   else
      System.out.println("b");
else
   if (obj1.equals(obj2))
      System.out.println("c");
   else
      System.out.println("d");

Select the one right answer.

a) a

b) b

c) c

d) d


Question 56: What will the user interface look like in an applet given the following init() method?

public void init() {
   setLayout(new BorderLayout());
   add(new Button("hello"));
}

Select the one right answer.

a) Nothing will appear in the applet

b) A button will appear in the applet set in the exact center

c) A button will appear in the applet along the top and centered horizontally

d) A button will appear in the top left corner

e) A button will fill the entire applet


Question 57: What expressions are true concerning the following lines of code?

int[] arr = {1, 2, 3};
for (int i=0; i < 2; i++)
   arr[i] = 0;

Select all valid answers.

a) arr[0] == 0

b) arr[0] == 1

c) arr[1] == 1

d) arr[2] == 0

e) arr[3] == 0


Question 58: What will happen if you try to compile and execute B’s main() method?

class A {
   int i;
   A(int i) {
      this.i = i * 2;
   }
}

class B extends A {
   public static void main(String[] args) {
      B b = new B(2);
   }
   B(int i) {
      System.out.println(i);
   }
}

Select the one right answer.

a) The instance variable i is set to 4

b) The instance variable i is set to 2

c) The instance variable i is set to 0

d) This code will not compile


Question 59: Which best describes the user interface of an applet given the following init() method:

public void init() {
   setLayout(new BorderLayout());
   add("North", new TextField(10));
   add("Center", new Button("help"));
}

Select all valid answers.

a) The TextField object will be placed at the top of the applet and will be 10 columns wide

b) The Button object will be centered in the applet and will be just large enough to contain the text "help"

c) The Button object will be centered in the applet and will start at the left edge of the applet, fit just under the TextField object above it, and extend to the right and bottom edge of the applet

d) The TextField object will be placed along the top of the applet and will stretch from the left edge to the right edge

e) The placement of the Button object and the TextField object depends on the overall size of the applet.


Question 60: Which of the following statements about try, catch, and finally are true?

Select all valid answers.

a) A try block must always be followed by a catch block

b) A try block can be followed either by a catch block or a finally block, or both

c) A catch block must always be associated with a try block

d) A finally can never stand on its own (that is, without being associated with try block)

e) None of these are true