11.20. Hard Multiple Choice QuestionsΒΆ

These problems are harder than those that you will typically see on the AP CS A exam.

    10-14-1: Assume that Base b = new Derived(); appears in a client program. What is the result of the call b.methodOne();?

    public class Base
    {
       public void methodOne()
       {
          System.out.print("A");
          methodTwo();
       }
    
       public void methodTwo()
       {
          System.out.print("B");
       }
    }
    
    public class Derived extends Base
    {
       public void methodOne()
       {
          super.methodOne();
          System.out.print("C");
       }
    
       public void methodTwo()
       {
          super.methodTwo();
          System.out.print("D");
       }
    }
    
  • ABDC
  • Even though b is declared as type Base it is created as an object of the Derived class, so all methods to it will be resolved starting with the Derived class. So the methodOne() in Derived will be called. This method first calls super.methodOne so this will invoke the method in the superclass (which is Base). So next the methodOne in Base will execute. This prints the letter "A" and invokes this.methodTwo(). Since b is really a Derived object, we check there first to see if it has a methodTwo. It does, so execution continues in the Derived class methodTwo. This method invokes super.methodTwo. So this will invoke the method in the super class (Base) named methodTwo. This method prints the letter "B" and then returns. Next the execution returns from the call to the super.methodTwo and prints the letter "D". We return to the Base class methodOne and return from that to the Derived class methodOne and print the letter "C".
  • AB
  • This would be true if the object was created of type Base. But the object is really a Derived object. So all methods are looked for starting with the Derived class.
  • ABCD
  • After the call to methodOne in the super class printing "A", the code continues with the implicit this.methodTwo which resolves from the current object's class which is Derived. Next, methodTwo in the Derived class is executed which then calls super.methodTwo which invokes println "B" from methodTwo in the Base class. Then the "D" in the Derived methodTwo is printed. Finally the program returns to methodOne in the Derived class are prints "C".
  • ABC
  • The call to methodTwo in super.methodOne is to this.methodTwo which is the method from the Derived class. Consequently the "D" is also printed.

    10-14-2: If you have the following classes. Which of the following constructors would be valid for Point3D?

    public class Point2D {
       public int x;
       public int y;
    
       public Point2D() {}
    
       public Point2D(int x,int y) {
          this.x = x;
          this.y = y;
       }
      // other methods
    }
    
    public class Point3D extends Point2D
    {
       public int z;
    
       // other code
    }
    
    I.  public Point3D() {}
    II. public Point3D(int x, int y, int z)
        {
           super(x,y);
           this.z = z;
        }
    III. public Point3D(int x, int y)
         {
            this.x = x;
            this.y = y;
            this.z = 0;
         }
    
  • II only
  • Point2D does have a constructor that takes an x and y value so this is okay. Also the call to super is the first line of code in the child constructor as required. However, both I and III are okay as well.
  • III only
  • The x and y values in Point2D are public and so can be directly accessed by all classes including subclasses. Also there is a no-arg constructor in Point2D so the super no-arg constructor will be called before the first line of code in this constructor.
  • I, II, and III
  • I is true because Point2D does have a no-arg constructor. II is true because Point2D does have a constructor that takes x and y. III is true because Point2D does have a no-arg constructor which will be called before the first line of code is executed in this constructor. The fields x and y are public in Point2D and thus can be directly accessed by all classes.
  • I and II only
  • This would be true if x and y were private in Point2D, but they are public.
  • I only
  • Point2D does have a no-arg constructor and since the constructor in Point3D doesn't have an explicit call to super as the first line of code in the constructor one will be added for the no-arg constructor. However, both II and III are okay as well.
Next Section - 11.21. Mixed Up Code Practice