Site Search:

Self Assessment questions for OCAJP

OCAJP questions:

1. What is the output of the following class? (Choose all that apply)
  1 public class 123C {
  2   private static Long â;
  3   private Object $;
  4   public static void main(String... Boolean) {
  5     String _;
  6     System.out.print(â);
  7     System.out.print($);
  8     System.out.print(_);
  9   }

 10 }
A. Compiler error on line 1.
B. Compiler error on line 2.
C. Compiler error on line 3.
D. Compiler error on line 4.
E. Compiler error on line 7.
F. Compiler error on line 8.
G. nullnullnull.

Answer: A. E. F
A. B. C. D test identifiers
E is correct because non-static variable cannot be referenced from a static context
F is correct because local variable have to be initialized before referencing.
G though instance variable and class variable have default values, this class didn't compile

2. What is the output of the following code?
  1 interface Flyable {void fly();}
  2 public class Bird implements Flyable{
  3   private int a;
  4   Bird(int a) {
  5     a = a;
  6     System.out.print("Bird");
  7   }
  8   Bird() {
  9     this(1);
 10     System.out.print("bird"+a);
 11   }
 12   private int age() {return a++;}
 13   public void fly() {System.out.print("birdfly");}
 14   public static void main(String[] a) {
 15     Bird d = new Duck(3);
 16     d.fly();
 17     System.out.print(d.age());
 18   }
 19 }
 20 class Duck extends Bird {
 21   private int a;
 22   Duck(int a) {
 23     a = a;
 24     System.out.print("Duck");
 25   }
 26   Duck() {System.out.print("duck"+a);}
 27   public void fly() {System.out.print("duckfly");}
 28   public int age() {return ++a;}
 29 }

A. Birdbird0Duckduckfly0.
B. Duckduck3duckfly3.
C. Birdbird3Duckbirdfly3.
D. Birdbird1Duckduckfly3.
E. Birdbird0Duckduckfly1.
F. Birdbird1Duckbirdfly1.
G. Birdbird4Duckduckfly4.
H. The code does not compile.

Answer A.
Duck(int a) didn't call super(), so super() is inserted as the first line of the constructor;
since a = a; didn't assign a to instance variable a, instance variable a still have default value 0;
method age() in Duck class looks like overridden method age() in Bird class, however, age() method in Bird class is private, when it is referenced in parent class, the private method is used.

3. which of the following lines of code compiles?
A. double amount = _1357_._08_;
B. long amount = 2147483648; //Integer.MAX_VALUE is 2147483647
C. int amount = Integer.MAX_VALUE + 1;
D. int amount = 5.6;
E. int amount = 9L; 
F. int amount = 0XE;
G. double amount = 1_2.0_0;

Answer C, F, G
A and G is testing underscores in numeric literals. underscores can not be at beginning and end of a literal, right before and after a decimal point. B, C, D, E tests assign literal to the numeric primitives. C compiles because Integer.MAX_VALUE + 1 is not a literal. F tests numbering system.

4. Given the following class in file /tmp/packagea/Table.java, and we compile it from /tmp with javac packagea/Table.java, what is the output of the following command: java packagea/Table ok "may be" fine? (select all that apply)
  1 package packagea;
  2 import java.util.Date;
  3 import java.sql.*;
  4 //import java.sql.Date;
  5 public class Table {
  6   public void Table() {System.out.print("1 table,");}
  7   public static void main(String args[]) { 
  8     System.out.print(args[1]);
  9     new Table();Chair c = new Chair();
 10     System.out.print(c.amount+" chairs");
 11     c = null;System.gc();
 12 }}  
 13 
 14 //package packagea;
 15 class Chair {
 16   int amount;
 17   public void Chair() { amount = 5;}
 18   protected void finalize() {
 19     Date d = new Date(0);
 20     System.out.print(d);
 21 }}

A. ok1 table,5 chairsWed Dec 31 19:00:00 EST 1969
B. may be1 table,5 chairsWed Dec 31 19:00:00 EST 1969
C. may be0 chairs
D. may be1 table,5 chairs
E. "may1 table,5 chairsWed Dec 31 19:00:00 EST 1969
F. Does not compile

Answer: C
C is correct, because the 1st parameter is ok, 2nd parameter is "may be", args[]'s index start from 0.
A, B, D, E is not correct, because public void Chair() {} and public void Table() {} is not constructor. Constructor would be public Chair() {} and public Table() {}.
finalize() and Garbage collection might or might not be run.
E is incorrect. import java.util.Date and import java.sql.* won't have name conflicts, because explicitly imported class name takes precedence over any wildcards. Multiple class can be in the same .java file, only one class can be public, the file name have to match the public class name.

5. Which of the following statements are true.
A. constructor runs after all fields and instance initializer blocks have run.
B. java have 8 primitives.
C. a byte can hold value from -128 to 127.
D. all java objects are stored in program's memory heap, all references are the same size.
E. finalize() is only run when the objet is eligible for garbage collection, it can only be called once.
F. java is an object oriented programming language, though it allows functional programing.
G. java is an interpreted language, the compiled bytecode is store in .class file.
H. An object is ready for garbage collection when no reference points to it or all references to the object have gone out of scope.

Answer: A, B, C, D, E, F, G, H