0% found this document useful (0 votes)
8 views6 pages

Unit 9 Summative Review III - MCQ Practice

The document contains a series of multiple-choice questions related to object-oriented programming concepts, specifically inheritance in Java. It includes class definitions and code segments, asking for the effects of executing certain lines of code and the correct implementations of methods. The questions test understanding of constructors, method overriding, and polymorphism.

Uploaded by

dcoolcal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

Unit 9 Summative Review III - MCQ Practice

The document contains a series of multiple-choice questions related to object-oriented programming concepts, specifically inheritance in Java. It includes class definitions and code segments, asking for the effects of executing certain lines of code and the correct implementations of methods. The questions test understanding of constructors, method overriding, and polymorphism.

Uploaded by

dcoolcal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Unit 9: Inheritance​ ​ ​ ​ ​ ​ ​ ​ ​ Name: _________________

Unit 9 Summative Review III - MCQ Practice

C 1) Consider the following class definitions.

public class Book


{
private String bookTitle;

public Book()
{
bookTitle = "";
}

public Book(String title)


{
bookTitle = title;
}
}

public class TextBook extends Book


{
private String subject;

public TextBook(String theSubject)


{
subject = theSubject;
}
}

The following code segment appears in a method in a class other than Book or TextBook.

Book b = new TextBook("Geometry");

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:

public class ThisClass public class ThatClass extends ThisClass


{ {
​ public ThisClass() ​ public ThatClass()
​ { ​ {
​ ​ System.out.print("better "); ​ ​ System.out.print("make it ");
​ } ​ }

​ public void speak() ​ public void speak()


​ { ​ {
​ ​ System.out.print("do it "); ​ System.out.print("faster ");
​ } ​ }
} }

B 2) What is printed when the following line of code is executed?

ThisClass example = new ThatClass();

(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

A 3) If example is declared as in the previous question:

ThisClass example = new ThatClass();

What is printed when the following call is made?

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.

public class exampleA { public class exampleB extends exampleA {


​ private int number; ​ private int otherNumber;

​ public exampleA(int n) { ​ public exampleB(int first, double second) {


​ ​ number = n; ​ ​ /*implementation asked for in 4*/
} ​ }

public int getNumber(){ ​ public void toString() {


​ return number; ​ ​ /*implementation asked for in 5*/
} ​ }

public void setNumber(int n) { public boolean equals(example ex) {


​ number = n; /*implementation asked for in 6*/
} }
} }

B 4) Which of the following is a correct way to implement the parameter constructor in exampleB?

I. setNumber(first); II. super(first); III. this.setNumber(first)


otherNumber = second; otherNumber = second; ;
otherNumber = second;

(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?

(A) return first + getNumber();


(B) return otherNumber + this.number;
(C) return first + " " + second;
(D) return otherNumber + " " + getNumber();
(E) return otherNumber + getNumber();

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?

I. return this.otherNumber == ex.otherNumber && this.getNumber() ==


ex.getNumber();

II. if(otherNumber == ex.otherNumber && getNumber() == ex.getNumber())


​ return true;
else
​ return false;

III. return otherNumber == ex.otherNumber && this.number == ex.number;


(A) I only​ (B) I and II only​ (C) II and III only​ (D) I and III only​ (E) I, II, and III
C
C 7) Consider the following class definition.

public class WhatsIt


{
​ private int length;
​ private int width;

​ public WhatsIt()
{
​ ​ //implementation not shown
​ }

​ public WhatsIt(int l, int w)


{
​ ​ //implementation not shown
​ }

​ public int getArea ()


{
​​ // implementation not shown
​ }

​ private int getPerimeter ()


{
​​ // implementation not shown
​ }
}

A child class AmaCallIt that extends WhatsIt would inherit:

(A) length, width, WhatsIt(), WhatsIt(int l, int w), getArea(), getPerimeter()


(B) length, width, WhatsIt(), WhatsIt(int l, int w), getArea()
(C) WhatsIt(), WhatsIt(int l, int w), getArea()
(D) length, width, getArea(), getPerimeter()
(E) length, width, getArea()
Use the following classes to answer questions 8-10:

public class Athlete {


​ private String name;

​ public Athlete() {
​ ​ name = "Athlete 1";
​ }

​ public void play() {


​ ​ System.out.print("athlete play ");
​ ​ run();
​ }

​ public void run() {


​ ​ System.out.print("athlete run ");
​ }

​ public String getName() { return name; }


}

public class TrackStar extends Athlete {


​ private double mile;

​ public TrackStar() {
​ ​ super();
​ ​ mile = 8.5;
​ }

​ public void play() {


​ ​ System.out.print("track play ");
​ ​ super.run();
​ }

​ public void run() {


​ ​ System.out.print("track run ");
​ }

​ public double getMile() { return mile; }


}

B 8) The following instantiation appears in a class other than Athlete or TrackStar:

Athlete micah = new TrackStar();

What is printed as a result of micah.play()?

(A) athlete play athlete run


(B) track play athlete run
(C) athlete play track run
(D) track play track run
(E) Error, an Athlete cannot be instantiated as a TrackStar
C 9) You want to add an equals method to TrackStar to compare two TrackStar objects. Which of the
following has the correct implementation of that equals method?

I. public boolean equals(TrackStar obj)


{
return getName() == obj.getName() && getMile() == obj.getMile();
}

II. public boolean equals(Athlete obj)


{
return getName().equals(((TrackStar) obj).getName()) &&
getMile() == ((TrackStar) obj).getMile();
}

III. public boolean equals(Object obj)


{
return getName().equals(((TrackStar) obj).getName()) &&
this.mile == ((TrackStar) obj).mile;
}

(A) I only
(B) I and II only
(C) II and III only
(D) I and III only
(E) I, II, and III

A 10) An array of TrackStars are created:

Athlete[] roster = new Athlete[2];


roster[0] = new TrackStar();
roster[1] = new Athlete();

Which of the following correctly prints track play athlete run?

(A) roster[0].play();
(B) roster[0].run();
(C) roster[1].play();
(D) roster[1].run();
(E) roster[0].play();
roster[1].run();

You might also like