OCP Flashcards – 1. Java Class Design

OCP Exam Objective 1 ‚Äď Java Class Design

Flashcard OCP.1.Java.Class.Design.001

True or false: instance variables and methods can be accessed by this() or super() ?

Click here to view answer
False, because such vars and methods must be static.

Illustration:

class Base{
    String name = "";
    Base(String name){
        this.name = name;
    }
}

class Test extends Base{
    String test = "Test";
    static String best = "Best";

    String run(){ return test; }
    static String walk(){ return best; }

    Test(){
//       super(test);     // INVALID: "cannot reference test before supertype ctor has been called"
//       super( run() );  // INVALID: "cannot reference this before supertype ctor has been called"
        super( walk() );  // VALID
    }
}

For example,¬†to call super(test) we’d need to¬†access the test var first but it’s¬†an instance field, in other words it is defined only in the context¬†of this object, which itself becomes available ony after the parent object has been fully initialized. So what we have here is a Catch-22 situation: to construct a parent object we¬†must¬†already know¬†its child but the child cannot¬†appear before¬†its parent comes into existence… Therefore, test must be available independently from any objects,¬†so it must be¬†static. Same logic¬†applies¬†to instance¬†methods and¬†the invocation¬†of this().

 

Flashcard OCP.1.Java.Class.Design.002

How many access modifiers are allowed for inner classes?

Click here to view answer
Three: public, private, and protected

 

Flashcard OCP.1.Java.Class.Design.003

Consider the following definition:

class MyClass {
    public boolean equals(MyClass obj){ return false; }
}

Does the method override or overload its namesake in Object?

Click here to view answer
It overloads it.

 

Flashcard OCP.1.Java.Class.Design.004

Which type determines the version of overridden method to be executed thanks to polymorphism?

Click here to view answer
Actype (or the actual type of the object, a.k.a. object type)

 

Flashcard OCP.1.Java.Class.Design.005

Which modifier must be used to prevent a method from being visible in a derived class?

Click here to view answer
private

 

Flashcard OCP.1.Java.Class.Design.006

Which methods should be overriden if custom type objects are to be used to search a Hashtable by its keys?

Click here to view answer
hashCode() and equals()

 

Flashcard OCP.1.Java.Class.Design.007

When overriding equals(), which class is to be used as the method’s argument?

Click here to view answer
Object

 

Flashcard OCP.1.Java.Class.Design.008

Which three modifiers are implicit for fields declared in an interface?

Click here to view answer
public, static, and final

 

Flashcard OCP.1.Java.Class.Design.009

Which access modifiers are allowed for class members?

Click here to view answer
public, private, and protected

 

Flashcard OCP.1.Java.Class.Design.010

Which methods of the Object class are invoked by the Object‘s toString() method?

Click here to view answer
getClass() and hashCode():

public String toString() {
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

BTW, getClass() and hashCode() are native methods:

public final native Class<?> getClass();
public native int hashCode();

 

Flashcard OCP.1.Java.Class.Design.011

What must be done to access an instance member from a static method?

Click here to view answer
Since non-static vars or methods cannot be directly accessed from a static method, an instance of the class in question must be created first.

 

Flashcard OCP.1.Java.Class.Design.012

Which character-based class in standard Java library is immutable?

Click here to view answer
String

 

Flashcard OCP.1.Java.Class.Design.013

Which type dictates that the hidden member’s version is to be used?

Click here to view answer
Reftype (or the reference type)

 

Flashcard OCP.1.Java.Class.Design.014

What access modifier is to be applied to accessor methods so that the code follows standard encapsulation principles?

Click here to view answer
public

 

Flashcard OCP.1.Java.Class.Design.015

What datatypes can implement the Singleton Design Pattern?

Click here to view answer
Concrete classes and enumerations

 

Flashcard OCP.1.Java.Class.Design.016

What interface members can be hidden in a subinterface or implementing class?

Click here to view answer
Constants

 

Flashcard OCP.1.Java.Class.Design.017

Which method of the Object class determines the bucket in which an object is placed in a Hashtable?

Click here to view answer
hashCode()

 

Flashcard OCP.1.Java.Class.Design.018

What is the name of design pattern that is implemented by a class that restricts instantiation to only a single instance?

Click here to view answer
Singleton

 

Flashcard OCP.1.Java.Class.Design.019

What object-oriented mechanism allows subclasses to make use of superclass’s methods?

Click here to view answer
Inheritance

 

Flashcard OCP.1.Java.Class.Design.020

Which modifiers allow fields to be omitted during serialization?

Click here to view answer
static and transient

 

Flashcard OCP.1.Java.Class.Design.021

What object-oriented mechanism is responsible¬†for the unique implementation of a superclass’s method in a derived class?

Click here to view answer
Polymorphism

 

Flashcard OCP.1.Java.Class.Design.022

What mechanism is responsible for hiding the implementation of code behind the API?

Click here to view answer
Encapsulation

 

Flashcard OCP.1.Java.Class.Design.023

What implicit conversions are not supported in case of covariant return types?

Click here to view answer
Autoboxing and unboxing

 

Flashcard OCP.1.Java.Class.Design.024

Which access modifier must be used on instance variables to implement encapsulation?

Click here to view answer
private

 

Flashcard OCP.1.Java.Class.Design.025

Which three components are contained by Singleton classes?

Click here to view answer
A private constructor, a static member referencing the instance, and a public static accessor method returning that reference

 

Flashcard OCP.1.Java.Class.Design.026

Which method of the Object class is invoked when adding objects to a Hashtable?

Click here to view answer
hashCode()

 

Flashcard OCP.1.Java.Class.Design.027

What is meant by covariant return types?

Click here to view answer
The term describes the situation when an overriding method can return a subtype of the overridden method’s return type

 

Flashcard OCP.1.Java.Class.Design.028

Which datatypes can access a field or method declared as protected?

Click here to view answer
Subclasses and any class within the same package

 

Flashcard OCP.1.Java.Class.Design.029

Which method of the Throwable class returns a short description of an exception or error?

Click here to view answer
toString()

 

Flashcard OCP.1.Java.Class.Design.030

Which feature of Java allows to change the code without affecting its clients?

Click here to view answer
Encapsulation

 

Flashcard OCP.1.Java.Class.Design.031

How many times does a static initialization block run?

Click here to view answer
Only once, when a class is loaded for the first time

 

Flashcard OCP.1.Java.Class.Design.032

What does Object‘s toString() return?

Click here to view answer
public String toString() returns any String. The method is used to provide a human-readable representation of the object.

 

Flashcard OCP.1.Java.Class.Design.033

What does the instanceof operator check?

Click here to view answer
instanceof checks if the left operand is the same class or interface (or a subclass) as the right operand. obj instanceof Object returns true unless obj itself is null.

 

Flashcard OCP.1.Java.Class.Design.034

What is the result if the left operand of instanceof is null?

Click here to view answer
If the left operand of instanceof is null, the result is false.

 

Flashcard OCP.1.Java.Class.Design.035

What happens if the two operands of instanceof are not in the same class hierarchy?

Click here to view answer
If the two operands are not in the same class hierarchy, the code will not compile

 

Flashcard OCP.1.Java.Class.Design.036

What does public boolean equals(Object obj) return when called with null or a class of the wrong type?

Click here to view answer
equals() is used to specify which instance variables should be considered for equality. The method is required to return false when the object passed in is null or is of the wrong type

 

Flashcard OCP.1.Java.Class.Design.037

What does Object‘s hashCode() return, and how this value should be computed?

Click here to view answer
public int hashCode() returns a number calculated with all or some of the instance variables used in equals(). It is used to provide a grouping in some collections. The method is required to return the same number when called with objects that are equal as determined by equals().

 

Leave Comment

Your email address will not be published.