Review Questions & Certified Solutions / 100% Correct /
Already Graded A+.
Consider the following class B. The Poodle variable myDog is instantiated as a Poodle. The instance
declarations. variable size is initialized to "toy". An implicit call to the no-argument Dog
constructor is made, initializing the instance variable name to "NoName".
public class Dog
{
private String name;
public Dog()
{
name = "NoName";
}
}
public class Poodle extends
Dog
{ private String size;
public Poodle(String s)
{ size =
s;
}
}
The following statement appears
in a method in another class.
Poodle myDog = new
Poodle("toy");
Which of the following best
describes the result of executing the
statement?
Terms in this set (18)
,Consider the following class A. BallCap myHat = new BallCap("L", "Denver");
declarations. public class Hat System.out.println(myHat);
{ private String size;
public Hat(String s)
{ size =
s;
}
public String toString()
{ return "Size " + size + " hat";
}
}
public class BallCap extends
Hat
{
private String team;
public BallCap(String mySize,
String myTeam)
{
super(mySize); team
= myTeam;
}
public String toString()
{ return super.toString() + " with " +
team + " logo";
}
}
A code segment located in a different
class is intended to produce the
following output. Size L hat with
Denver logo Which of the following
code segments will produce this
output?
, Consider the following class E. Line 8 causes a compile-time error because the getCompass method is not
declarations. defined for objects of type MultiTool.
public class MultiTool
{ private int blade;
private int screwdriver;
public MultiTool(int b, int s)
{
blade = b; screwdriver =
s;
}
}
public class DeluxeMultiTool
extends MultiTool
{
private boolean compass;
public DeluxeMultiTool(int b, int s,
boolean c)
{ super(b, s);
compass = c;
}
public String getCompass()
{
return compass + "";
}
}
The following code segment
appears in a method in another
class.
ArrayList<MultiTool> toolList = new
ArrayList<MultiTool>();
MultiTool tool1 = new
DeluxeMultiTool(4, 2, false); //
Line 2
DeluxeMultiTool tool2 = new
DeluxeMultiTool(3, 1, true); //
Line 3 toolList.add(tool1); //
Line 4 toolList.add(tool2); //
Line 5 for (MultiTool tool :
toolList)
{
System.out.println(tool.getComp
ass()); // Line 8
}
The code segment does not
compile. Which of the following
best explains the cause of the
error?