Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

Review Questions

7.35What will be the result of compiling and running the following program?
public class Polymorphism {
  public static void main(String[] args) {
    A ref1 = new C();
    B ref2 = (B) ref1;
    System.out.println(ref2.f());
  }
}

class A           { int f() { return 0; } }
class B extends A { int f() { return 1; } }
class C extends B { int f() { return 2; } }

Select the one correct answer.

  1. The program will fail to compile.

  2. The program will compile but will throw a ClassCastException, when run.

  3. The program will compile and print 0, when run.

  4. The program will compile and print 1, when run.

  5. The program will compile and print 2, when run.

7.36What will be the result of compiling and running the following program?
public class Polymorphism2 {
  public static void main(String[] args) {
    A ref1 = new C();
    B ref2 = (B) ref1;
    System.out.println(ref2.g());
  }
}

class A {
  private int f() { return 0; }
  public int g() { return 3; }
}
class B extends A {
  private int f() { return 1; }
  public int g() { return f(); }
}
class C extends B {
  public int f() { return 2; }
}

Select the one correct answer.

  1. The program will fail to compile.

  2. The program will compile and print 0, when run.

  3. The program will compile and print 1, when run.

  4. The program will compile and print 2, when run.

  5. The program will compile and print 3, when run.

7.37Which statements about the program are true?
public interface HeavenlyBody { String describe(); }

class Star {
  String starName;
  public String describe() { return "star " + starName; }
}

class Planet extends Star {
  String name;
  public String describe() {
    return "planet " + name + " orbiting star " + starName;
  }
}

Select the two correct answers:

  1. The code will fail to compile.

  2. The code defines a Planet is-a Star relationship.

  3. The code will fail to compile if the name starName is replaced with the name bodyName throughout the declaration of the Star class.

  4. The code will fail to compile if the name starName is replaced with the name name throughout the declaration of the Star class.

  5. An instance of Planet is a valid instance of HeavenlyBody.

7.38Given the following code, which statement is true?
public interface HeavenlyBody { String describe(); }

class Star implements HeavenlyBody {
  String starName;
  public String describe() { return "star " + starName; }
}

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

Select the one correct answer:

  1. The code will fail to compile.

  2. The code defines a Planet has-a Star relationship.

  3. The code will fail to compile if the name starName is replaced with the name bodyName throughout the declaration of the Star class.

  4. The code will fail to compile if the name starName is replaced with the name name throughout the declaration of the Star class.

  5. An instance of Planet is a valid instance of a HeavenlyBody.

7.39Which statement is not true?

Select the one correct answer.

  1. Maximizing cohesion and minimizing coupling are the hallmarks of a welldesigned application.

  2. Coupling is an inherent property of any non-trivial OO design.

  3. Adhering to the JavaBeans naming standard can aid in achieving encapsulation.

  4. Dependencies between classes can be minimized by hiding implementation details.

  5. Each method implementing a single task will result in a class that has high cohesion.

  6. None of the above.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint