Dan Chisholm's
Java Programmer Certification Mock Exam

Please Help Save a Tree!

The pages of this web site are not formatted to conserve paper, but my new book (ISBN: 0-9745862-0-X) is formatted to save paper, save your printer cartridge, save a loose-leaf binder, and save money. If you prefer to work my exams from printed pages, then give your printer a rest and buy my new book.

Today, you can find my book at BookSurge.com.

Are you a university student studying Java programming? Do you agree that my book would serve as a helpful workbook and companion to be used along with the Java fundamentals textbook that is currently being used in your class? If so, then please ask your professor to consider using my book in future classes.

If you have any questions or comments concerning my mock exams or my book, then please send an e-mail to me at scjpexam2000@yahoo.com.

I would also like to read your response to the following questions.


Question 1

Which of the following statements is true in terms of the value returned by the java.lang.Math.round method?

a. Rounds the argument to the nearest whole number. If the result is equally close to two whole numbers, then the even one is returned.
b. Rounds the argument to the nearest whole number. If the result is equally close to two whole numbers, then the odd one is returned.
c. Adds 0.5 to the argument and takes the floor of the result.
d. Adds 0.5 to the argument and takes the ceil of the result.
e. None of the above

Question 2

Suppose that the java.lang.Math.round method is invoked with an argument of type float, and the result exceeds the range of type int. Which of the following occurs?

a. The result is implicitly widened to type long.
b. The returned value is Float.NaN.
c. A run-time exception is thrown.
d. The bits that overflow are ignored and no exception is thrown.
e. The returned value is zero but no exception is thrown.
f. None of the above

Question 3

Which of the following statements is true in terms of the java.lang.Math.sqrt method?

a. It is not overloaded.
b. Returns a float if the argument type is float.
c. If the argument is negative, then the returned value is the square root of the absolute value of the argument.
d. Throws an ArithmeticException if the argument is negative.
e. None of the above

Question 4

Which of the following statements are true in terms of the java.lang.Math.sin method?

a. It is not overloaded.
b. Returns a float if the argument type is float.
c. The argument is an angle measured in radians.
d. The argument is an angle measured in degrees.
e. Throws an ArithmeticException if the argument is greater than 360.

Question 5

Which of the following statements is true in terms of the java.lang.Math.random method?

a. The random method name is overloaded.
b. The argument type is a double that represents a seed value.
c. Throws an ArithmeticException if the seed value is negative.
d. The returned value is always greater than zero and less than or equal to one.
e. None of the above

Question 6

Which of the following statements is true in terms of the java.lang.Math.floor method?

a. Four overloaded versions of floor exist.
b. An ArithmeticException is declared in the throws clause.
c. The type of the return value depends on the argument type.
d. The returned value is always of an integral primitive type.
e. Returns the largest whole number that is less than or equal to the argument value.
f. Returns the smallest whole number that is greater than or equal to the argument value.
g. None of the above

Question 7

Which of the following statements is true in terms of the java.lang.Math.ceil method?

a. Four overloaded versions of ceil exist.
b. An ArithmeticException is declared in the throws clause.
c. The type of the return value depends on the argument type.
d. The returned value is always of an integral primitive type.
e. Returns the largest whole number that is less than or equal to the argument value.
f. Returns the smallest whole number that is greater than or equal to the argument value.
g. None of the above

Question 8

Which of the following statements are true in terms of the java.lang.Math.abs method?

a. Four overloaded versions of abs exist.
b. An ArithmeticException is declared in the throws clause.
c. The type of the return value depends on the argument type.
d. The returned value is always of a floating-point primitive type.
e. If the argument is a negative integral value, then the returned value is always positive.
f. If the argument is positive, then the argument is returned.

Question 9

The java.lang.Math.abs method can return which of the following?

a. Negative infinity
b. Positive infinity
c. NaN
d. Short.MIN_VALUE
e. Integer.MIN_VALUE
f. Long.MIN_VALUE
g. Negative zero
h. Positive zero

Question 10

class SRC123 {
  public static void main (String[] args) {
    System.out.print(Math.floor(-3.6) + "," + Math.ceil(-3.6)+ ",");
    System.out.print(Math.floor(3.6) + "," + Math.ceil(3.6));
}}

What is the result of attempting to compile and run the program?

a. Prints: -3.0,-4.0,3.0,4.0
b. Prints: -3.0,-4.0,4.0,3.0
c. Prints: -4.0,-3.0,3.0,4.0
d. Prints: -4.0,-3.0,4.0,3.0
e. Prints: -4.0,-4.0,3.0,3.0
f. Compile-time error
g. Run-time error
h. None of the above

Question 11

class SRC124 {
  public static void main (String[] args) {
    System.out.print(Math.round(-3.6) + "," + Math.round(-3.4)+ ",");
    System.out.print(Math.round(3.4) + "," + Math.round(3.6));
}}

What is the result of attempting to compile and run the program?

a. Prints: -3.0,-4.0,3.0,4.0
b. Prints: -3.0,-4.0,4.0,3.0
c. Prints: -4.0,-3.0,3.0,4.0
d. Prints: -4.0,-3.0,4.0,3.0
e. Prints: -4.0,-4.0,3.0,3.0
f. Compile-time error
g. Run-time error
h. None of the above

Question 12

class SRC125 {
  public static void main (String[] args) {
    System.out.print(Math.round(-3.6) + Math.round(3.6) + ",");
    System.out.print(Math.round(-3.4) + Math.round(3.4));
}}

What is the result of attempting to compile and run the program?

a. Prints: 0,0
b. Prints: 0,-1
c. Prints: -1,0
d. Prints: -1,-1
e. Prints: 0,1
f. Prints: 1,0
g. Prints: 1,1
h. Compile-time error
i. Run-time error
j. None of the above

Question 13

Which of the following is used to force each thread to reconcile its working copy of a variable with the master copy in main memory?

a. final
b. static
c. synchronized
d. transient
e. volatile
f. native

Question 14

class A extends Thread {
  public void run() {
    synchronized (this) {
      try {wait(5000);} catch (InterruptedException ie){}
  }}
  public static void main(String[] args) {
    A a1 = new A();
    long startTime = System.currentTimeMillis();
    a1.start();
    System.out.print(System.currentTimeMillis() - startTime + ",");
    try {a1.join(6000);} catch (InterruptedException ie) {}
    System.out.print(System.currentTimeMillis() - startTime);
}}

What are the possible results of attempting to compile and run the program?

a. The first number printed is greater than or equal to 0
b. The first number printed must always be greater than 5000
c. The second number printed must always be greater than 5000
d. The second number printed must always be greater than 6000
e. The synchronized block inside the run method is not necessary
f. Compile-time error
g. Run-time error

Question 15

class A extends Thread {
  String[] sa;
  public A(String[] sa) {this.sa = sa;}
  public void run() {
    synchronized (sa) {System.out.print(sa[0] + sa[1] + sa[2]);}
}}
class B {
  private static String[] sa = new String[]{"X","Y","Z"};
  public static void main (String[] args) {
    synchronized (sa) {
      Thread t1 = new A(sa); t1.start();
      sa[0] = "A"; sa[1] = "B"; sa[2] = "C";
}}}

What is the result of attempting to compile and run the program?

a. Prints: XYZ
b. Prints: AYZ
c. Prints: ABZ
d. Prints: ABC
e. Compile-time error
f. Run-time error
g. None of the above

Question 16

class A extends Thread {
  String[] sa;
  public A(String[] sa) {this.sa = sa;}
  public void run() {
    synchronized (sa) {
      while (!sa[0].equals("Done")) {
        try {sa.wait();} catch (InterruptedException ie) {}
    }}
    System.out.print(sa[1] + sa[2] + sa[3]);
}}
class B {
  private static String[] sa = new String[]{"Not Done","X","Y","Z"};
  public static void main (String[] args) {
    Thread t1 = new A(sa); t1.start();
    synchronized (sa) {
      sa[0] = "Done";
      sa[1] = "A"; sa[2] = "B"; sa[3] = "C";
      sa.notify();
}}}

What is the result of attempting to compile and run the program?

a. Prints: XYZ
b. Prints: AYZ
c. Prints: ABZ
d. Prints: ABC
e. Compile-time error
f. Run-time error
g. None of the above

Question 17

Which of the following are true statements?

a. The Thread.join method is static
b. The Thread.join method is always invoked on an instance of Thread
c. The Thread.join method causes the current thread to wait for the referenced thread to die
d. The Thread.join method declares an InterruptedException in the throws clause
e. The Thread.join method accepts a timeout value as an argument
f. The timeout value sets the minimum time that the current thread will wait for the death of the referenced thread
g.  Thread.join will return immediately if the timeout value is zero
h. A timeout of zero will allow Thread.join to wait forever if necessary

Question 18

Which of the following allows a thread t1 to become the holder of the lock of object obj1.

a. By blocking on I/O
b. By entering a synchronized instance method of the obj1
c. By invoking the wait method on the object
d. By entering the body of a block that is synchronized on obj1
e. By entering a synchronized static method of the obj1
f. By invoking the notify method on obj1

Question 19

After invoking the wait method on an object, obj1, a thread, T1, will remain in the wait set of obj1 until which of the following occurs?

a. Another thread invokes the notify method on the object, obj1, and T1 is selected to move out of the wait set
b. Another thread invokes the notifyAll method on the object
c. Another thread invokes the resume method on thread T1
d. Another thread interrupts thread T1
e. The priority of thread T1 is increased
f. A specified timeout period has elapsed
g. Another thread invokes the join method on thread T1

Question 20

class A implements Runnable{public void run() {}}
class B {
  public static void main(String[] args) {
    Thread t1 = new Thread();             // 1
    Thread t2 = new Thread(new A());      // 2
    Thread t3 = new Thread(new A(), "A"); // 3
    Thread t4 = new Thread("A");          // 4
}}

A compile-time error is generated at which line?

a. 1
b. 2
c. 3
d. 4
e. None of the above

Question 21

class A implements Runnable{public void run() {}}
class B {
  public static void main(String[] args) {
    Thread t1 = new Thread();             // 1
    Thread t2 = new Thread(new A());      // 2
    Thread t3 = new Thread("A", new A()); // 3
    Thread t4 = new Thread("A");          // 4
}}

A compile-time error is generated at which line?

a. 1
b. 2
c. 3
d. 4
e. None of the above

Question 22

class A extends Thread {
  private boolean done;
  public void setDone(boolean done) {this.done = done;}
  public void run() {
    synchronized (this) {
      while (!done) {try {wait();} catch (InterruptedException ie){}}
  }}
  public static void main(String[] args) {
    A a1 = new A();
    long startTime = System.currentTimeMillis();
    a1.start();
    System.out.print(System.currentTimeMillis() - startTime);
}}


Which is a possible result of attempting to compile and run the program?

a. The number printed is greater than or equal to 0
b. The synchronized block inside the run method is not necessary
c. This program runs to completion after the elapsed time is printed
d. Compile-time error
e. Run-time error
f. None of the above

Question 23

class A extends Thread {
  public void run() {
    synchronized (this) {
      try {wait();} catch (InterruptedException ie){}
  }}
  public static void main(String[] args) {
    A a1 = new A(); a1.setDaemon(true);
    long startTime = System.currentTimeMillis();
    a1.start();
    System.out.print(System.currentTimeMillis() - startTime + ",");
}}

Which is a possible result of attempting to compile and run the program?

a. The number printed is greater than or equal to 0
b. The synchronized block inside the run method is not necessary
c. Thread a1 waits forever and the program runs forever
d. Compile-time error
e. Run-time error
f. None of the above

Question 24

class A extends Thread {
  private Object obj;
  public A(Object obj) {this.obj = obj;}
  public void run() {
    try {
      synchronized (obj) {obj.wait();}
    } catch (InterruptedException ie) {}
    System.out.print(Thread.currentThread().getName());
}}
class B {
  private void m1() {
    for (int i = 0; i < 10; i++) {
      A t1 = new A(this);
      t1.setName(String.valueOf(i)); t1.setDaemon(true); t1.start();
    }
    synchronized (this) {notifyAll();}
  }
  public static void main(String[] args) {new B().m1();}
}

What are the possible results of attempting to compile and run the program?

a. All of the numbers 0 through 9 must always be printed
b. Some or all of the numbers 0 through 9 could be printed
c. Nothing is printed
d. Run-time error

Question 25

class C extends Thread {
  private static String[] sa = new String[]{"Not Done","X","Y","Z"};
  public void run() {
    synchronized (sa) {
      while (!sa[0].equals("Done")) {
        try {sa.wait();} catch (InterruptedException ie) {}
    }}
    System.out.print(sa[1] + sa[2] + sa[3]);
  }
  public static void main (String[] args) {
    start();
    synchronized (sa) {
      sa[0] = "Done";
      sa[1] = "A"; sa[2] = "B"; sa[3] = "C";
      sa.notify();
}}}

Which is a possible result of attempting to compile and run the program?

a. Prints: XYZ
b. Prints: AYZ
c. Prints: ABZ
d. Prints: ABC
e. Compile-time error
f. Run-time error
g. None of the above

Question 26

class C extends Thread {
  private static String[] sa = new String[]{"Not Done","X","Y","Z"};
  public void run() {
    synchronized (this) {
      while (!sa[0].equals("Done")) {
        try {wait();} catch (InterruptedException ie) {}
    }}
    System.out.print(sa[1] + sa[2] + sa[3]);
  }
  void m1() {
      start();
      synchronized (this) {
        sa[0] = "Done";
        sa[1] = "A"; sa[2] = "B"; sa[3] = "C";
  }}
  public static void main (String[] args) {
    new C().m1(); notify();
}}

Which is a possible result of attempting to compile and run the program?

a. Prints: XYZ
b. Prints: AYZ
c. Prints: ABZ
d. Prints: ABC
e. Compile-time error
f. Run-time error
g. None of the above

Question 27

class A extends Thread {
  public void run() {
    long startTime = System.currentTimeMillis();
    long endTime = startTime + 10000;
    while (System.currentTimeMillis() < endTime) {
      yield();
  }}
  public static void main(String[] args) {
    A a1 = new A();
    long startTime = System.currentTimeMillis();
    a1.start(); sleep(1000); a1.interrupt(); a1.join();
    System.out.print(System.currentTimeMillis() - startTime);
}}

Which is a possible result of attempting to compile and run the program?

a. Prints a number that is less than 1000
b. Prints a number between 1000 and 9999
c. Prints a number larger than 10000
d. Compile-time error
e. Run-time error
f. None of the above

Question 28

class A extends Thread {
  static long startTime;
  public void run() {
    for (int i = 0; i < 99999; i++) {Math.sin(i);}
    String name = Thread.currentThread().getName();
    long time = System.currentTimeMillis();
    System.out.println(name + " done at " + (time - startTime));
  }
  public static void main(String[] args) {
    A t1 = new A(); A t2 = new A();
    t1.setName("T1"); t2.setName("T2");
    t1.setPriority(Thread.MIN_PRIORITY);
    t2.setPriority(Thread.MAX_PRIORITY);
    startTime = System.currentTimeMillis();
    t1.start(); t2.start();
}}

Which of the following is a true statement?

a. The priority assigned to thread T2 is greater than the priority assigned to T1
b. Java guarantees that thread T2 will get more CPU time than T1
c. Java guarantess that thread T2 will run to completion before T1
d. None of the above


Copyright © 2002-2004, Dan Chisholm
All rights reserved.