Java OCA OCP Practice Question 1939

Question

Given the following code, which statement is true?.

public interface Printable { String describe(); }

class Star implements Printable {
  String starName;//from  w  ww .  j  a va  2  s .  c  o m
  public String describe() { return "star " + starName; }
}

class Main {
  String name;
  Star orbiting;
  public String describe() {
    return "planet " + name + " orbiting " + orbiting.describe();
  }
}

Select the one correct answer:.

  • (a) The code will fail to compile.
  • (b) The code defines a Main has-a Star relationship.
  • (c) The code will fail to compile if the name starName is replaced with the name bodyName throughout the declaration of the Star class.
  • (d) The code will fail to compile if the name starName is replaced with the name name throughout the declaration of the Star class.
  • (e) An instance of Main is a valid instance of a Printable.


(b)

Note

The code will compile.

The code will not fail to compile if the name of the field starName is changed in the Star class, since the Main class does not try to access the field by name, but instead uses the public method describe() in the Star class for that purpose.

An instance of Main is not an instance of Printable, since it neither implements Printable nor extends a class that implements Printable.




PreviousNext

Related