QUESTIONS AND COMPLETE
SOLUTIONS
Consider athe afollowing aclass adefinitions.
public aclass aBike
{
private aint anumWheels a= a2;
// aNo aconstructor adefined
}
public aclass aEBike aextends aBike
{
private aint anumBatteries;
public aEBike(int abatteries)
{
numBatteries a= abatteries;
}
}
The afollowing acode asegment aappears ain aa amethod ain aa aclass aother athan aBike aor
aEBike.
EBike aeB a= anew aEBike(4);
Which aof athe afollowing abest adescribes athe aeffect aof aexecuting athe acode asegment?
A
An aimplicit acall ato athe azero-parameter aBike aconstructor ainitializes athe ainstance
avariable anumWheels. aThe ainstance avariable anumBatteries ais ainitialized ausing athe
avalue aof athe aparameter abatteries.
B
An aimplicit acall ato athe aone-parameter aBike aconstructor awith athe aparameter apassed
ato athe aEBike aconstructor ainitializes athe ainstance avariable anumWheels. aThe
ainstance avariable anumBatteries ais ainitialized ausing athe avalue aof athe aparameter
abatteries.
C
Because asuper ais anot aexplicitly acalled afrom athe aEBike ac a- a(correct aanswer) a-A
,An aimplicit acall ato athe azero-parameter aBike aconstructor ainitializes athe ainstance
avariable anumWheels. aThe ainstance avariable anumBatteries ais ainitialized ausing athe
avalue aof athe aparameter abatteries.
Consider athe afollowing aclass adefinitions.
public aclass aBird
{
private aint abeakStrength;
public aBird(int ainput)
{
beakStrength a= ainput;
}
public avoid asetBeakStrength(int astrength)
{
beakStrength a= astrength;
}
}
public aclass aHawk aextends aBird
{
private aint atalonStrength;
public aHawk(int atalon, aint abeak)
{
super(beak);
talonStrength a= atalon;
}
}
The afollowing astatement aappears ain aa amethod ain aanother aclass.
Bird ab a= anew aHawk(5, a8);
Which aof athe afollowing abest adescribes athe aeffect aof aexecuting athe astatement?
A
The aBird avariable ab ais ainstantiated aas aa aHawk. aThe ainstance avariable
atalonStrength ais ainitialized awith athe avalue afrom athe aparameter atalon. aThe aHawk
aconstructor acannot aset athe ainstance avariable abeakStrength abecause aa asubclass
adoes anot ahave aaccess ato aa aprivate avariable ain aits asuperclass.
B
The aBird avariable ab ais ainstantiated aas aa aHawk. aThe acall asuper(beak) areturns aa
avalue afrom athe ainstance avariable abeakStrength ain a- a(correct aanswer) a-D
The aBird avariable ab ais ainstantiated aas aa aHawk. aThe acall asuper(beak) ainvokes athe
aBird aconstructor aand ainitializes athe ainstance avariable abeakStrength awith athe avalue
afrom athe aparameter abeak. aThe ainstance avariable atalonStrength ais athen ainitialized
awith athe avalue afrom athe aparameter atalon.
, Consider athe afollowing aclass adefinitions.
public aclass aBook
{
private aString abookTitle;
public aBook()
{
bookTitle a= a"";
}
public aBook(String atitle)
{
bookTitle a= atitle;
}
}
public aclass aTextBook aextends aBook
{
private aString asubject;
public aTextBook(String atheSubject)
{
subject a= atheSubject;
}
}
The afollowing acode asegment aappears ain aa amethod ain aa aclass aother athan aBook aor
aTextBook.
Book ab a= anew aTextBook("Psychology");
Which aof athe afollowing abest adescribes athe aeffect aof aexecuting athe acode asegment?
A
The aTextBook aconstructor ainitializes athe ainstance avariable asubject awith athe avalue
aof athe aparameter atheSubject, aand athen ainvokes athe azero-parameter aBook
aconstructor, awhich ainitializes athe ainstance avariable abookTitle ato a"".
B
The aTextBook aconstructor ainitializes athe ainstance avariable asubject awith athe avalue
aof athe aparameter atheSubject, aand athen ainvokes athe aone-parameter aBook
aconstructor awith atheSubject aas athe aparameter, awhi a- a(correct aanswer) a-C
There ais aan aimplicit acall ato athe azero-parameter aBook aconstructor. aThe ainstance
avariable abookTitle ais athen ainitialized ato a"". aThen, athe ainstance avariable asubject ais
ainitialized awith athe avalue aof athe aparameter atheSubject.
Consider athe afollowing aclass adefinitions.
public aclass aDrink
{
// aimplementation anot ashown
}
public aclass aCoffee aextends aDrink