Combine PDF
Combine PDF
public Book()
{
bookTitle = "";
}
The following code segment appears in a method in a class other than Book or TextBook.
Which of the following best describes the effect of executing the code segment?
(A) The TextBook constructor initializes the instance variable subject with the value of "Geometry", and
then calls the default Book constructor, which initializes the instance variable bookTitle to "".
(B) The TextBook constructor initializes the instance variable subject with the value of "Geometry", and
then invokes the one-parameter Book constructor, which initializes the instance variable bookTitle to
"Geometry".
(C) There is an implicit call to the default Book constructor first, and the instance variable bookTitle is then
initialized to "". Then, the instance variable subject is initialized with the value of "Geometry".
(D) There is an implicit call to the default Book constructor first, and the instance variable bookTitle is then
initialized to "Geometry". Then, the instance variable subject is initialized with the value of "Geometry".
(E) The code segment will not execute because the TextBook constructor does not contain an explicit call to
one of the Book constructors.
The next two questions refer to the two classes defined below:
(A) Nothing is printed, Java will not let us initialize an object declared as a different type.
(B) better make it
(C) make it
(D) make it better
(E) better
example.speak();
(A) faster
(B) do it
(C) do it faster
(D) faster do it
(E) Nothing is printed. speak is a ThisClass method and example is a ThatClass object
Use the following classes to answer the questions that follow.
B 4) Which of the following is a correct way to implement the parameter constructor in exampleB?
(A) I only
(B) I and II only
(C) II and III only
(D) I and III only
(E) I, II, and III
D 5) Which of the following is the correct way to implement the toString() method in exampleB so that
it returns its two instance variables?
B 6) Which of the following is a correct way to implement the equals method in exampleA so that it returns
true if both exampleB objects have the same data and false otherwise?
public WhatsIt()
{
//implementation not shown
}
public Athlete() {
name = "Athlete 1";
}
public TrackStar() {
super();
mile = 8.5;
}
(A) I only
(B) I and II only
(C) II and III only
(D) I and III only
(E) I, II, and III
(A) roster[0].play();
(B) roster[0].run();
(C) roster[1].play();
(D) roster[1].run();
(E) roster[0].play();
roster[1].run();
Unit 9: Inheritance Name: ________________________
Unit 9 Summative Review II
Multiple Choice
Questions 1−10 refer to the BankAccount, SavingsAccount, and CheckingAccount classes defined
below:
public BankAccount()
{ balance = 0; }
public BankAccount(double acctBalance)
{ balance = acctBalance; }
public SavingsAccount()
{ /* implementation not shown */ }
D 1) Of the methods shown, how many different non-constructor methods can be invoked by a
SavingsAccount object?
(A) 1
(B) 2
(C) 3
(D) 4
(E) 5
A 2) Which of the following correctly implements the default constructor of the SavingsAccount class?
I. interestRate = 0;
super();
II. super();
interestRate = 0;
III. super(interestRate);
(A) II only
(B) I and II only
(C) II and III only
(D) III only
(E) I, II, and III
D 3) Which is a correct implementation of the constructor with parameters in the SavingsAccount class?
(C) super();
interestRate = rate;
(D) super(acctBalance);
interestRate = rate;
I. super(acctBalance);
II. super();
deposit(acctBalance);
III. deposit(acctBalance);
(A) I only
(B) II only
(C) III only
(D) II and III only
(E) I, II, and III
C 5) Which is the correct implementation code for the withdraw method in the CheckingAccount class?
(A) super.withdraw(amount);
if(balance < MIN_BALANCE)
super.withdraw(FEE);
(B) withdraw(amount);
if(balance < MIN_BALANCE)
withdraw(FEE);
(C) super.withdraw(amount);
if(getBalance() < MIN_BALANCE)
super.withdraw(FEE);
(D) withdraw(amount);
if(getBalance() < MIN_BALANCE)
withdraw(FEE);
A program to test the BankAccount, SavingsAccount, and CheckingAccount classes has these
declarations:
(A) b.deposit(200);
(B) s.withdraw(500);
(C) c.withdraw(500);
(D) s.deposit(10000);
(E) s.addInterest();
Which of the following will transfer money from one account to another without error?
I. b.transfer(DisneySavings, 50);
II. DisneySavings.transfer(GoldieChecking, 30);
III. GoldieChecking.transfer(b, 55);
(A) I only
(B) II only
(C) III only
(D) I, II, and III
(E) None
public class A {
public A() {
System.out.print("pear ");
}
public A(int z) {
System.out.print("peach ");
}
A 15) Which of the following is true about the immediate parent and child classes of a class?
(A) Every class can have many different child classes but only one parent class
(B) Every class can have many different child classes and many different parent classes
(C) Every class can have either one parent class or one child class, not both
(D) Every class can have only one child class but many different parent classes
(E) Every class can have only one child class and only one parent class
Free Response
16) A class Triangle has been created. It has three instance data, Point p1, p2, p3, a constructor that
receives the 3 Points and initializes p1, p2, and p3, a toString method that outputs the 3 Points, and a
computeSurfaceArea method that calculates the surface area of the Triangle (as a double value).
We want to extend this class to represent a 3-dimensional Pyramid, which consists of 4 Points. Since the
Pyramid will consist of 4 triangles, the computeSurfaceArea method for the Pyramid will compute the
surface area of the Triangle made up of 3 of those 4 Points.
Write the Pyramid class to handle the difference(s) between a Pyramid and the previously defined
Triangle. Assume that the class Point has a toString method.
public Person(){
myAge = 0;
}
public Person(int x){
myAge = x;
}
public Employee() {
super();
salary = 0.0;
vacationDays = 0;
}
3) Write the toString() and equals() method for an Employee. Format the String nicely, but it can be
done however you would like.
Write a code segment that holds all 4 people in a single array, and then uses a loop to print off the
saySomething method for each of the people.
The following classes start creating an inheritance hierarchy of the Dungeons and Dragons monsters. Use the
classes below to answer the questions that follow.
1) On each line, circle if the code segment is legal or illegal (produces a syntax error).
2) For the following code segments, comment on if they are legal or illegal (produces a syntax error). Then give
a sentence as to why.
FrostGiant yeti = new FrostGiant(); Legal, FrostGiant object inherits the getSize method
int size = yeti.getSize(); from Giant (which gets it from Monster)
Monster vegeta = new Dragon(); Illegal, the Monster object doesnt have the
String n = vegeta.getSubtrace(); getSubtrace method
An Elephant is a subclass of Animal. An elephant adds its own instance variable called trunkLength
that represents the length of the elephant's trunk in inches. The Elephant class adds four additional methods:
● eatMeal – this method increases the weight of the elephant by 20 pounds
● walkAround – this method decrease the weight of the elephant by 5 pounds
● grow – this method increase the trunk length by weight MOD 4
● toString – this method returns a String that displays the elephant's weight and trunk length
In the main method, an ArrayList of Elephants is created. The output of the code below is shown.
import java.util.ArrayList;
The following classes start creating an inheritance hierarchy of the Dungeons and Dragons monsters. Use the
classes below to answer the questions that follow.
1) UML is an acronym that stands for Unified Modeling Language. A UML diagram is a way to visually
represent the contents of a program. It is very similar to the inheritance hierarchy diagrams we created in class,
but the instance variables and method names are listed under the class names, to help identify the class
components. Complete the UML diagram below that has already been started for you (we will come back to this
example in the future homework assignments as well).
2) Below each class, list the instance variables that each class member will have when instantiated.
3) Use the following code to create an inheritance hierarchy chart and answer the questions that follow.
public class G
{ /* code not shown */ }
2) Create an inheritance hierarchy in the space below. Then use it answer the true/false statements to the right
by circling the correct result.
G
/\ T / F a) J is a G
J P
/\ \ T / F b) Q is a P
Q T A
T / F c) A is a T
T / F d) Q is a G
T / F e) A is a P
T / F f) J is a P
Unit 9: Inheritance Name: _________________
9.2, 9.3 & 9.4 – Methods and Constructors for Subclasses
1) Trace the main method of the program below by writing out what each line in the main method does.
public class A
{
private int x;
public A(){
x = 1;
System.out.print(“Default A ”);
}
public class C
{
public static void main(){
A a = new A(); prints “Default A”
A aa = new A(10); “Parameter A”
B b = new B(); “Parameter A Default B”
B bb = new B(5); “Parameter B”
System.out.println(aa.getX()); 10
System.out.println(bb.getX()); 0
System.out.println(a.getX()); 1
System.out.println(b.getX()); 2
}
}
2) For the following classes, write the code for the appropriate constructor description.
public Monster(){
size = "medium";
● Monster should have a default
lang = "common";
} constructor with size equal to
“medium” and lang equal to
public Monster(String size, String lang){ “common”.
this.size = size; ● Monster should have a two
this.lang = lang; parameter constructor.
}
3) Trace through the following lines of code and describe what they do. Assume that this code is created outside
of the object creating classes. Use the Monster, Giant, and FrostGiant code from above. Some lines of
code below have illegal syntax, while other lines of code compile without issue. Assume that all changed to the
instance variables are cumulative as the code compiles through each line.
Code Result
Giant grace = new Giant(“huge”, ”giant”, Calls Giant(String, String, int), which calls
15); Monster(String, String). size = "huge", lang =
"giant", ac = 15.
Calls FrostGiant(String, String, int, int),
FrostGiant freddy = new which calls Giant(String, String, int). size =
FrostGiant(“large”, “common”, 18, 225); "large", lang = "common", ac = 18, lifespan =
225.
1) Consider the following classes. Then, determine what is printed when the code in the main method is run.
}
}
A 2) Consider the following two classes:
public class A{
public int method1(int x){
return 2;
}
}
Which of the following could be the signature of a method in class B that correctly overloads method1 in class
A?
public class A{
public int method1(int x){
return 2;
}
}
Which of the following could be the signature of a method in class B that correctly overrides method1 in class
A?
public display(){
System.out.print(x + “, “ + y);
}
}
public display(){
/*implementation below*/;
}
}
Which of the following should replace /*implementation below*/ so that the code above displays 3, 4,
5 to the console?
(B)
super();
System.out.println(“ , “ + z);
(D)
super();
System.out.println(“ , “ + super.z);
(E)
super(x, y);
System.out.println(“ , “ + super.z);
Unit 9: Inheritance Name: __________________
9.7 – Final, Protected, and Object Superclass
1) Consider the following classes. There are spaces and blanks in the code for you to complete the classes so
they function correctly, given their visibility modifiers. Follow the instructions given in the code comments.
public class A {
private int i;
public A(){
i = 5;
}
public boolean equals(A obj) {
return this.i == obj.i;
}
}
A a = new A();
A a2 = new A();
System.out.println(a == a2);
System.out.println(a.equals(a2));
A b = new B();
B b2 = new B();
B b3 = b2;
System.out.println(b2 == b3);
System.out.println(b.equals(b2));
System.out.println(b.equals(b3));