Home
1.
Which answer choice can replace line 6 so the code continues to produce the same output?

3: List rug = new ArrayList<>();
4: rug.add("circle");
5: rug.add("square");
6: System.out.println(rug);

  • A.
    System.out.println(rug.asString);
  • B.
    System.out.println(rug.asString());
  • C.
    System.out.println(rug.toString);
  • D.
    System.out.println(rug.toString());
  • Answer & Explanation
  • Report
Answer : [D]
Explanation :
The toString() method is declared in the Object class. Therefore it is available to be called in any Java class and is overridden in some. Java automatically calls the toString() method when you print an object, making Option D correct. Option C is incorrect because toString() is a method, not a variable.
Report
Name Email  
2.
Which best describes this code?

class Stats {
private int data;
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
}
  • A.
    It is a singleton.
  • B.
    It is well encapsulated.
  • C.
    It is immutable.
  • D.
    It is both well encapsulated and immutable.
  • Answer & Explanation
  • Report
Answer : [B]
Explanation :
This code is not a singleton because it has a public constructor. Remember that a public no-argument constructor is provided automatically if no constructor is coded. This code is well encapsulated because the instance variable is private. It is not immutable since there is a setter method. Therefore, Option B is correct.
Report
Name Email  
3.
What design pattern or principle ensures that there will be no more than one instance of a class?
  • A.
    Encapsulation
  • B.
    Immutability
  • C.
    Singleton
  • D.
    Static
  • Answer & Explanation
  • Report
Answer : [C]
Explanation :
The singleton pattern ensures there will be no more than one instance of the object. Depending on how it is implemented, it is possible for there to be zero instances. But it is not possible to have more than one, making Option C correct. Option D means the variable is shared across instances or even without an instance being created but does not limit the number of the instances of the class itself.
Report
Name Email  
4.
What is the output of this code?

class Laptop extends Computer {
public void startup() {

System.out.print("laptop-");

}
}
pubic class Computer {
public void startup() {
System.out.print("computer-");
}
public static void main(String[] args) {
Computer computer = new Laptop();
Laptop laptop = new Laptop();
computer.startup();
laptop.startup();
}
}
  • A.
    computer-laptop-
  • B.
    laptop-computer-
  • C.
    laptop-laptop-
  • D.
    None of the above
  • Answer & Explanation
  • Report
Answer : [C]
Explanation :
Both objects are instances of the class Laptop. This means the startup() method in the Laptop class gets called both times thanks to polymorphism.
Report
Name Email  
5.
Which method can be correctly inserted into this class to meet the contract of the equals() method? You may assume that text is not null.
class Button {
private String text;
public int hashCode() {
return text.hashCode();
}
}
  • A.
    public boolean equals(Object o) {
    if ( o == null ) return true;
    if (! (o instanceof Button)) return false;
    return text.equals(o.text);
    }
  • B.
    public boolean equals(Object o) {
    if ( o == null ) return true;
    Button b = (Button) o;
    return text.equals(b.text);
    }
  • C.
    public boolean equals(Object o) {
    if (! (o instanceof Button)) return false;
    return text.equals(o.text);
    }
  • D.
    public boolean equals(Object o) {
    if (! (o instanceof Button)) return false;
    Button b = (Button) o;
    return text.equals(b.text);
    }
  • Answer & Explanation
  • Report
Answer : [D]
Explanation :
We know that the variable o that equals() is called on isn’t null, since we can’t call instance methods on a null reference. However, a null reference could be passed as a method parameter. If a null is passed in, the method should return false since an object and a null are not equal. Options A and B are incorrect because the first line of those methods should return false rather than true. Option C is incorrect because the cast is missing. The Object class does not have a text variable available. Option D shows a properly implemented equals() method and is correct.
Report
Name Email