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 toner, and save money. If you prefer to work my exams from printed pages, then you can save a tree, save your printer toner, and save money if you buy my new book instead of attempting to print the pages of my web site.

Today, you can find my book on the retail web site of the company that prints it and distributes it.


Question 1

class MWC204 {
  static void m1(StringBuffer s1) {
    s1 = s1.append("B"); System.out.print(s1);
  }
  static void m2(StringBuffer s1) {
    s1.append("C"); System.out.print(s1);
  }
  public static void main(String[] s) {
    StringBuffer s1 = new StringBuffer("A");
    m1(s1); m2(s1);
    System.out.print(s1);
}}

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

a. Prints: AAA
b. Prints: ABAA
c. Prints: ABACA
d. Prints: ABABAB
e. Prints: ABABCAB
f. Prints: ABABCABC
g. Prints: ABCABCABC
h. Compile-time error
i. Run-time error
j. None of the above

Question 2

class MWC205 {
  static void m1(StringBuffer s1) {
    s1.append("B"); System.out.print(s1);
  }
  static void m2(StringBuffer s1) {
    s1 = new StringBuffer("C"); System.out.print(s1);
  }
  public static void main(String[] s) {
    StringBuffer s1 = new StringBuffer("A");
    m1(s1); m2(s1);
    System.out.print(s1);
}}

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

a. Prints: AAA
b. Prints: ABCA
c. Prints: ABCAB
d. Prints: ABCABC
e. Prints: ABCAC
f. Prints: ABABCABC
g. Compile-time error
h. Run-time error
i. None of the above

Question 3

class MWC200 {
  public static void main (String[] args) {
    String s1 = "ABC";
    StringBuffer s2 = new StringBuffer(s1);
    System.out.print(s2.equals(s1) + "," + s1.equals(s2));
}}

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

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

Question 4

class MWC201 {
  public static void main (String[] args) {
    String s1 = new String("ABC"), s2 = new String("ABC");
    StringBuffer sb1 = new StringBuffer(s1);
    StringBuffer sb2 = new StringBuffer(s2);
    System.out.print(s1.equals(s2) + "," + sb1.equals(sb2));
}}

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

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

Question 5

class MWC202 {
  public static void main (String[] args) {
    StringBuffer sb1 = new StringBuffer("ABC");
    StringBuffer sb2 = new StringBuffer("ABC");
    System.out.print((sb1==sb2)+","+sb1.equals(sb2));
}}

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

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

Question 6

class MWC203 {
  public static void main (String[] args) {
    String s1 = new String("ABC"), s2 = new String("ABC");
    StringBuffer sb1 = new StringBuffer(s1);
    StringBuffer sb2 = new StringBuffer(s2);
    boolean b1 = s1.hashCode() == s2.hashCode();
    boolean b2 = sb1.hashCode() == sb2.hashCode();
    System.out.print(b1 + "," + b2);
}}

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

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


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