1. Java Class Design, Stage 2 Problems

OCP Stage 2 Problem 1.1 (view answer and explanation)

Given the class:

public class MyClass {
     @Override
     public int hashCode() {
          return 1;
     }
}

Which three statements are true about MyClass objects?

A. All objects will be considered equal.
B. All objects will be considered unequal.
C. All objects will return the same value when the toString() method is invoked.
D. When using these objects as keys in a Hashtable, all values will be overwritten.
E. When using these objects as keys in a Hashtable, all values will be placed in the same bucket.

OCP Stage 2 Problem 1.2 (view answer and explanation)

Given:

public class OurExams {
    public static void main(String[] args) {
        String exams = "1Z0-808 & 1Z0-809";
        exams.replaceAll("[C8]","Z");
        for(String e : exams.split("-")) {
            System.out.print(e.indexOf("Z") + " ");
        }
    }
}

What is the result?

A. 1 0 1
B. -1 7 1
C. -1 -1 -1
D. 0 -1 0
E. 1 7 -1
F. 1 1 1

OCP Stage 2 Problem 1.3 (view answer and explanation)

Which two keywords are required to declare a constant?

A. const
B. public
C. volatile
D. abstract
F. final
E. static

OCP Stage 2 Problem 1.4 (view answer and explanation)

Given the classes:

class Browser {
    String name = "Generic";
    int anonimity = 0;
    public void getAnonumityLevel() {
        System.out.println(name + " " + anonimity);;
    }
}

class MozillaFirefox extends Browser {
    String name = "Firefox";
    int anonimity = 2;
    public void getAnonumityLevel() {
        System.out.println(name + " " + anonimity);;
    }
}

class Tor extends MozillaFirefox {
    String name = "Onion";
    int anonimity = 8;
    public void getAnonumityLevel() {
        System.out.println(name + " " + anonimity);;
    }
}

Which one of the following code fragments demonstrates polymorphism?

A. Browser b = new Tor();
System.out.println(b.name);
B. Tor b = new MozillaFirefox();
System.out.println(b.name);
C. Browser b = new Tor();
System.out.println(b.getAnonymityLevel());
D. Tor b = new MozillaFirefox();
System.out.println(b.getAnonymityLevel());

OCP Stage 2 Problem 1.5 (view answer and explanation)

Given the following code:

public class Password {
    private StringBuilder psw;
    public Password() { psw = new StringBuilder("_"); }

    public void make(byte i)   { psw.append("b" + i); }
    public void make(long i)   { psw.append("l" + i); }
    public void make(float i)  { psw.append("f" + i); }

    public void make(Double i) { psw.append("D" + i); }
    public void make(Long i)   { psw.append("L" + i); }
    public void make(Object i) { psw.append("O" + i); }

    public void finish() {  psw.append("_"); }
    public void show()   {  System.out.println(psw);  }
}

Which two code fragments will generate the output _l2l3b4_?

A. Password env = new Password();
env.make(2);
env.make(3);
env.make(4);
env.finish();
env.show();
B. Password env = new Password();
env.make(2.0);
env.make(3d);
env.make(4);
env.finish();
env.show();
C. Password env = new Password();
env.make(2L);
env.make(3);
env.make((byte)4.0);
env.finish();
env.show();
D. Password env = new Password();
env.make((long)2);
env.make(3.0);
env.make(4);
env.finish();
env.show();
E. Password env = new Password();
env.make(2);
env.make(3);
env.make((byte) 4);
env.finish();
env.show();

OCP Stage 2 Problem 1.6 (view answer and explanation)

Given:

01   class Book {
02       private String title, author;
03       private Book(String title) {
04           this.title = title;
05           show();
06       }
07       protected Book(String title, String author) {
08           this(title);
09           this.author = author;
10           show();
11       }
12       protected void show() {
13           System.out.println(title + " " + author);
14       }
15   }
16   class SerialBook extends Book {
17       private String series;
18       public SerialBook(String series, String title, String author) {
19           this.series = series;
20           show();
21       }
22       public static void main(String[] args) {
23           new SerialBook("Discworld", "Hogfather", "Terry Pratchett");
24       }
25   }

What is the result?

A. Discworld null
B. Discworld Hogfather
C. Discworld HogfatherDiscworld Hogfather
D. Discworld nullDiscworld HogfatherDiscworld Hogfather
E. Compilation fails due to an error on line 08.
F. Compilation fails due to an error on line 18.

OCP Stage 2 Problem 1.7 (view answer and explanation)

Given the following code:

public class Adder {
     public static String add(long arg1, long arg2) { return "longs";    }
     public static String add(Long arg1, Long arg2) { return "Longs";    }
     public static String add(Object args)          { return "Objects";  }
     public static String add(long... args)   { return "varargs long";   }
     public static String add(Long... args)   { return "varargs Long";   }
     public static String add(Object... args) { return "varargs Object"; }
}

Which code fragment will generate the output varargs Long?

A. System.out.println(Adder.add(1, 2));
B. System.out.println(Adder.add(1L, 2L));
C. System.out.println(Adder.add(1.0, 2.0));
D. Long[] arrLongs = {1L, 2L};
System.out.println(Adder.add(arrLongs));
E. int[] arrInts = {1, 2};
System.out.println(Adder.add(arrInts));

OCP Stage 2 Problem 1.8 (view answer and explanation)

Given:

class OCA {
    private Double getScore() {
       return 98.0;
    }
}
class OCP extends OCA {
    public Integer getScore() throws Exception {
        return 97;
    }
}

Which statement is true about the code?

A. The getScore() method in OCP overrides the getScore() method in OCA.
B. The getScore() method in OCP overloads the getScore() method in OCA.
C. The getScore() method in OCP hides the getScore() method in OCA.
D. The getScore() method in OCP has no relationship with the getScore() method in OCA.

OCP Stage 2 Problem 1.9 (view answer and explanation)

Which two statements are true about implementing Singletons?

A. A singleton can be an interface or abstract class.
B. A singleton is limited to concrete classes and enumerations.
C. A singleton must be designed as thread-safe.
D. A singleton must provide a public constructor.
E. A singleton can implement the Serializable interface.
F. A singleton is not limited to a single class instance.

OCP Stage 2 Problem 1.10 (view answer and explanation)

Given:

class Daddy {
    private String firstName, lastName;
    protected void showName(){
        System.out.println(firstName + " " + lastName);
    }

    private Daddy(String firstName) {
        this.firstName = firstName;
        showName();
    }

    protected Daddy(String firstName, String lastName) {
        this(firstName);
        this.lastName = lastName;
        showName();
    }
}

Which class correctly extends Daddy?

A. class Sonny extends Daddy { }
B. class Sonny extends Daddy {
    Sonny() {}
}
C. class Sonny extends Daddy {
    Sonny() {
¬†¬†¬†¬†¬†this(“”);
    }
}
D. class Sonny extends Daddy {
    Sonny() {
¬†¬†¬†¬†¬†¬†¬† super(“”);
    }
}
E. class Sonny extends Daddy {
    Sonny() {
¬†¬†¬†¬†¬†¬†¬† this(“”,””);
    }
}
F. class Sonny extends Daddy {
    Sonny() {
¬†¬†¬†¬†¬†¬†¬† super(“”,””);
    }
}

OCP Stage 2 Problem 1.11 (view answer and explanation)

Given the following class:

public class Unique {
    private static Unique instance;
    private Unique() {}
}

Which method should be added to this class to make it a Singleton?

A. public Unique(Unique instance) {
    Unique.instance = instance;
}
B. public Unique(int numberOfObjects) {
    Unique[] instances = new Unique[numberOfObjects];
    for (int i = 0; i < numberOfObjects; i++) {
        instances[i] = new Unique();
    }
}
C. public static synchronized void setInstance(Unique instance) {
    Unique.instance = instance;
}
D. public static synchronized Unique getInstance() {
    if (Unique.instance == null)
        instance = new Unique();
    return instance;
}

OCP Stage 2 Problem 1.12 (view answer and explanation)

Given:

interface Ruminant {}
abstract class Animal {}
abstract class Tail {}
abstract class TailedAnimal extends Animal {}

Which class represents a Ruminant Animal that has a Tail?

A. class Sheep extends TailedAnimal, Ruminant {}
B. class Camel extends TailedAnimal implements Ruminant {}
C. class Elk implements Ruminant extends TailedAnimal{
Tail t;}
D. class Cow extends Animal implements Ruminant {
Tail t;}

OCP Stage 2 Problem 1.13 (view answer and explanation)

Given:

public class Vessel {
    protected double depth;
    protected int distance;
    int raid (int miles) {
       return distance += miles;
    }
    protected static void report(Vessel vsl) {
        System.out.println(vsl.distance);
    }
}

public class Submarine extends Vessel {
    public double depth;
    public int raid (int miles) {
        depth += .5;
        return distance += miles;
    }
    public static void report(Vessel vsl) {
        System.out.println(vsl.distance + " @ " + vsl.depth);
    }
}

Which two code fragments will output 3000 @ 0.0?

A. Submarine sub = new Submarine();
sub.raid(3000);
sub.report(sub);
B. Vessel sub = new Submarine();
sub.raid(3000);
sub.report(sub);
C. Vessel sub = new Vessel();
sub.raid(3000);
sub.report(sub);
D. Vessel sub = new Vessel();
sub.raid(3000);
Submarine.report(sub);

OCP Stage 2 Problem 1.14 (view answer and explanation)

Given the classes:

abstract class BlueChipStock {}
class IBM extends BlueChipStock {}
class ExxonMobil extends BlueChipStock {}

abstract class Option {}
class Put extends Option {}
class Call extends Option {}

Which two expressions will evaluate to true?

A. new BlueChipStock() instanceof Object;
B. new IBM() instanceof BlueChipStock;
C. new ExxonMobil() instanceof Option;
D. new Option() instanceof Object;
E. new Put() instanceof Option;
F. new Call() instanceof BlueChipStock;

OCP Stage 2 Problem 1.15 (view answer and explanation)

Given:

public class Base{ public void doStuff(){ /* code */} }

Which of the following code snippets is an example of an “IS-A” relationship?

A. public abstract class ClassA extends Base{
      public ClassA() {
          super();
      }
}
B. public class ClassB {
      public Base getInstance() {
          return new Base();
      }
}
C. public class ClassC {
      private Base instance;
      public ClassC() {
          instance = new Base();
      }
      public void run() {
          instance.doStuff();
      }
}
D. public class ClassD {
      private static ClassD instance;
      private ClassD() {}
      public static ClassD getInstance() {
          if (instance == null)
              instance = new ClassD();
          return instance;
      }
}

OCP Stage 2 Problem 1.16 (view answer and explanation)

Given classes in independent files:

class Trending {
    protected int trend;
    public void setTrend(int level) {
        trend = Math.round(level / 100);
    }
    public int getTrend() { return trend;}
}
class TheWalkingDead extends Trending {
    _________________ void setTrend(int level) {
        trend = Math.round((float) Math.sqrt((level + 50) / 50));
    }
}

Which access modifier should be inserted to compile TheWalkingDead?

A. private
B. package
C. protected
D. public

OCP Stage 2 Problem 1.17 (view answer and explanation)

Given the class:

public class Archimedes {
    public static double getCircleArea(double radius) {
        return PI * pow(radius, 10);    // raises the 1st argument to the power of the 2nd argument
}
    public static double getSquareArea(double side) {
        return pow(side, 10);
    }
}

Which import declaration(s) must appear before the class declaration for Archimedes to compile?

A. import java.lang.*;
B. import java.lang.Math;
import java.lang.Math.PI;
import java.lang.Math.pow;
C. static import java.lang.Math;
D. import static java.lang.Math.*;

OCP Stage 2 Problem 1.18 (view answer and explanation)

Given the classes:

class Runner {
    public void run() { System.out.println("Running..."); }
}
class Code extends Runner {
    public void run() { System.out.println("It's alive!"); }
}
class Pantyhose extends Runner {
    public void run() { System.out.println("Not again!"); }
}
class Snippet extends Code {
    public void test() { System.out.println("Testing...");
    }
}

Which code fragment will throw a runtime exception?

A. Runner r = new Code();
r.run();
B. Code c = new Pantyhose();
c.run();
C. Code c = new Snippet();
c.test();
D. Runner r = new Code();
((Snippet) r).test();
E. Runner r = new Snippet();
((Snippet) r).test();

OCP Stage 2 Problem 1.19 (view answer and explanation)

Given:

class JavaCert {
    protected Number getScore() {
        return 85;
    }
}

Which two return types are allowed for the methods that override getScore()?

A. int
B. double
C. Double
D. AtomicInteger
E. Object
F. String
G. Character

OCP Stage 2 Problem 1.20 (view answer and explanation)

Given the code fragment:

String str = "Powers of 2";
int a = 8, b = 256;
StringBuilder sb = new StringBuilder(str);

sb.append(a);           // n1
sb.append(b);           // n2
sb.append(a > b);       // n3
sb.append(a < b);       // n4
sb.insert(a, "" + a);   // n5
sb.insert(b, "" + b);   // n6

Which line will throw a runtime exception?

A. n1
B. n2
C. n3
D. n4
E. n5
F. n6

OCP Stage 2 Problem 1.21 (view answer and explanation)

Given the following two files:

package doomed;

public class Doom {
    protected int level;
    int frags;
}

package doomed;
public class Player {
    public static void main(String[] args) {
        Doom session = new Doom();
        session.level++;                 // n1
        session.frags += 1000;           // n2
        System.out.println(session.level + " - " + session.frags);
    }
}

What is the result?

A. 1 – 1000
B. 0 – 0
C. RuntimeException is thrown at line n1.
D. RuntimeException is thrown at line n2.
E. Compilation fails.

OCP Stage 2 Problem 1.22 (view answer and explanation)

Given:

class Father {
    public String who;
    Father getRelation() {
        who = "Father";
        return this;
    }
}

class Son extends Father {
    protected Son getRelation() {            // n1
        who = "Son";
        return this;
    }
}

class Grandson extends Son {
    public Grandson getRelation() {          // n2
        who = "Grandson";
        return this;
    }
    public static void main(String[] args) {
        System.out.print(new Grandson().getRelation().who);
    }
}

What is the result?

A. Father
B. Son
C. Grandson
D. Compilation fails due to an error on line n1.
E. Compilation fails due to an error on line n2.

OCP Stage 2 Problem 1.23 (view answer and explanation)

OCP Stage 2 Problem 1.24 (view answer and explanation)

OCP Stage 2 Problem 1.25 (view answer and explanation)

OCP Stage 2 Problem 1.26 (view answer and explanation)

OCP Stage 2 Problem 1.27 (view answer and explanation)

OCP Stage 2 Problem 1.28 (view answer and explanation)

OCP Stage 2 Problem 1.29 (view answer and explanation)

OCP Stage 2 Problem 1.30 (view answer and explanation)

Leave Comment

Your email address will not be published.