9.13.3. Hard Multiple Choice QuestionsΒΆ
These problems are harder than those that you will typically see on the AP CSA exam.
- ABDC
- Even though b is declared as type
Baseit is created as an object of theDerivedclass, so all methods to it will be resolved starting with theDerivedclass. So themethodOne()inDerivedwill be called. This method first callssuper.methodOneso this will invoke the method in the superclass (which isBase). So next themethodOneinBasewill execute. This prints the letter "A" and invokesthis.methodTwo(). Sincebis really aDerivedobject, we check there first to see if it has amethodTwo. It does, so execution continues in theDerivedclassmethodTwo. This method invokessuper.methodTwo. So this will invoke the method in the super class (Base) namedmethodTwo. This method prints the letter "B" and then returns. Next the execution returns from the call to thesuper.methodTwoand prints the letter "D". We return to theBaseclassmethodOneand return from that to theDerivedclassmethodOneand print the letter "C". - AB
- This would be true if the object was created of type
Base. But the object is really aDerivedobject. So all methods are looked for starting with theDerivedclass. - ABCD
- After the call to
methodOnein the super class printing "A", the code continues with the implicitthis.methodTwowhich resolves from the current object's class which isDerived. Next,methodTwoin theDerivedclass is executed which then callssuper.methodTwowhich invokesprintln"B" frommethodTwoin theBaseclass. Then the "D" in theDerivedmethodTwois printed. Finally the program returns tomethodOnein theDerivedclass are prints "C". - ABC
- The call to
methodTwoinsuper.methodOneis tothis.methodTwowhich is the method from theDerivedclass. Consequently the "D" is also printed.
9-12-14: 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");
}
}
- II only
Point2Ddoes have a constructor that takes anxandyvalue 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
xandyvalues inPoint2Dare public and so can be directly accessed by all classes including subclasses. Also there is a no-arg constructor inPoint2Dso 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
Point2Ddoes have a no-arg constructor. II is true becausePoint2Ddoes have a constructor that takesxandy. III is true becausePoint2Ddoes have a no-arg constructor which will be called before the first line of code is executed in this constructor. The fieldsxandyare public inPoint2Dand thus can be directly accessed by all classes. - I and II only
- This would be true if
xandywere private inPoint2D, but they are public. - I only
Point2Ddoes have a no-arg constructor and since the constructor inPoint3Ddoesn'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.
9-12-15: 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;
}
You have attempted of activities on this page
