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.
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.
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 { }
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
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); } } };
System.out.println(4 | 7);
Select the one right answer.
a) 4
b) 5
c) 6
d) 7
e) 0
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) {} } }
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
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();
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();
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
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) { } }
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.
Fill in the blank.
Fill in the blank.
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
a) TextListener
b) ActionListener
c) MouseMotionListener
d) MouseListener
e) ComponentListener
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"
Fill in the blank.
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.
Fill in the blank.
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.
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.
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)
Select all valid answers.
a) #_pound b) _underscore c) 5Interstate d) Interstate5 e) _5_
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.
Select all valid answers.
a) ActionListener
b) FocusListener
c) MouseMotionListener
d) WindowListener
e) ContainerListener
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.
Select all valid answers.
a) c == Container b) c.equals(Class.Container) c) c instanceof Container d) c instanceof Component e) c implements Container
Fill in the blank.
System.out.println(4 & 7);
Select the one right answer.
a) 4
b) 5
c) 6
d) 7
e) 0
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
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.
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
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
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
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
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
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);
//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
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;
Fill in the blank.
long temp = (int)3.9; temp %= 2;
a) 0
b) 1
c) 2
d) 3
e) 4
Select the one right answer.
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
Start by writing:
List l =
in your answer.
Fill in the blank.
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
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
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.
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
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
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
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.
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
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
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
Fill in the blank.
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
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
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
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
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.
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