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
Base
it is created as an object of theDerived
class, so all methods to it will be resolved starting with theDerived
class. So themethodOne()
inDerived
will be called. This method first callssuper.methodOne
so this will invoke the method in the superclass (which isBase
). So next themethodOne
inBase
will execute. This prints the letter "A" and invokesthis.methodTwo()
. Sinceb
is really aDerived
object, we check there first to see if it has amethodTwo
. It does, so execution continues in theDerived
classmethodTwo
. 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.methodTwo
and prints the letter "D". We return to theBase
classmethodOne
and return from that to theDerived
classmethodOne
and print the letter "C". - AB
- This would be true if the object was created of type
Base
. But the object is really aDerived
object. So all methods are looked for starting with theDerived
class. - ABCD
- After the call to
methodOne
in the super class printing "A", the code continues with the implicitthis.methodTwo
which resolves from the current object's class which isDerived
. Next,methodTwo
in theDerived
class is executed which then callssuper.methodTwo
which invokesprintln
"B" frommethodTwo
in theBase
class. Then the "D" in theDerived
methodTwo
is printed. Finally the program returns tomethodOne
in theDerived
class are prints "C". - ABC
- The call to
methodTwo
insuper.methodOne
is tothis.methodTwo
which is the method from theDerived
class. 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
Point2D
does have a constructor that takes anx
andy
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
andy
values inPoint2D
are public and so can be directly accessed by all classes including subclasses. Also there is a no-arg constructor inPoint2D
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 becausePoint2D
does have a constructor that takesx
andy
. III is true becausePoint2D
does have a no-arg constructor which will be called before the first line of code is executed in this constructor. The fieldsx
andy
are public inPoint2D
and thus can be directly accessed by all classes. - I and II only
- This would be true if
x
andy
were private inPoint2D
, but they are public. - I only
Point2D
does have a no-arg constructor and since the constructor inPoint3D
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.
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