0% found this document useful (0 votes)
23 views

Inheritance Review

The document discusses class definitions for Book, TextBook, Drink, and Coffee classes. It provides code segments and asks questions about constructor calls and method calls between the classes. Key details include inheritance, superclass calls, and implicit vs explicit calls.

Uploaded by

happyy1736
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Inheritance Review

The document discusses class definitions for Book, TextBook, Drink, and Coffee classes. It provides code segments and asks questions about constructor calls and method calls between the classes. Key details include inheritance, superclass calls, and implicit vs explicit calls.

Uploaded by

happyy1736
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

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("Psychology");


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 the
parameter theSubject, and then invokes the zero-parameter Book constructor, which initializes the
instance variable bookTitle to "".
(B) The TextBook constructor initializes the instance variable subject with the value of the
parameter theSubject, and then invokes the one-parameter Book constructor with theSubject as
the parameter, which initializes the instance variable bookTitle to the value of the
parameter theSubject.
(C) There is an implicit call to the zero-parameter Book constructor. The instance variable bookTitle is
then initialized to "". Then, the instance variable subject is initialized with the value of the
parameter theSubject.
(D) The code segment will not execute because the TextBook constructor does not contain an explicit call to
one of the Book constructors.
(E) The code segment will not execute because the TextBook constructor does not have a parameter for the
title of the book.
Consider the following class definitions.

public class Drink


{
// implementation not shown
}
public class Coffee extends Drink
{
// There may be instance variables and constructors that are not shown.
// No methods are defined for this class.
}
The following code segment appears in a method in a class other than Drink or Coffee.

Coffee myCup = new Coffee();


myCup.setSize("large");
Which of the following must be true so that the code segment will compile without error?
(A) The Drink class must have a public method named getSize that takes a String value as its
parameter.
(B) The Drink class must have a public method named getSize that takes no parameters.
(C) The Drink class must have a public method named setSize that takes a String value as its
parameter.
(D) The Drink class must have a public method named setSize that takes no parameters.
(E) The Drink class must have a String instance variable named size.
Consider the following class definitions.

public class Robot


{
private int servoCount;
public int getServoCount()
{ return servoCount; }

public void setServoCount(int in)


{ servoCount = in; }
}
public class Android extends Robot
{
private int servoCount;

public Android(int initVal)


{ setServoCount(initVal); }

public int getServoCount()


{ return super.getServoCount(); }

public int getLocal()


{ return servoCount; }

public void setServoCount(int in)


{ super.setServoCount(in); }

public void setLocal(int in)


{ servoCount = in; }
}
The following code segment appears in a method in another class.

int x = 10;
int y = 20;
/* missing code */
Which of the following code segments can be used to replace /* missing code */ so that the value 20 will be
printed?
(A) Android a = new Android(x);
a.setServoCount(y);
System.out.println(a.getServoCount());

(B) Android a = new Android(x);


a.setServoCount(y);
System.out.println(a.getLocal());

(C) Android a = new Android(x);


a.setLocal(y);
System.out.println(a.getServoCount());

(D) Android a = new Android(y);


a.setServoCount(x);
System.out.println(a.getLocal());

(E) Android a = new Android(y);


a.setLocal(x);
System.out.println(a.getLocal());
Consider the following class definitions.

public class Book


{
private String author;
private String title;
public Book(String the_author, String the_title)
{
author = the_author;
title = the_title;
}
}
public class Textbook extends Book
{
private String subject;
public Textbook(String the_author, String the_title, String the_subject)
{
/* missing implementation */
}
}
Which of the following can be used to replace /* missing implementation */ so that the Textbook constructor
compiles without error?
A. author = the_author;
title = the_title;
subject = the_subject;

B. super(the_author, the_title);
super(the_subject);

C. subject = the_subject;
super(the_author, the_title);

D. super(the_author, the_title);
subject = the_subject;

E. super(the_author, the_title, the_subject);


Consider the following class definitions.

public class Road


{
private String roadName;
public Road(String name)
{
roadName = name;
}
}

public class Highway extends Road


{
private int speedLimit;

public Highway(String name, int limit)


{
super(name);
speedLimit = limit;
}
}
The following code segment appears in a method in another class.

Road r1 = new Highway("Interstate 101", 55); // line 1


Road r2 = new Road("Elm Street"); // line 2
Highway r3 = new Road("Sullivan Street"); // line 3
Highway r4 = new Highway("New Jersey Turnpike", 65); // line 4
Which of the following best explains the error, if any, in the code segment?
(A) Line 1 will cause an error because a Road variable cannot be instantiated as an object of type Highway.
(B) Line 2 will cause an error because the Road constructor is not properly called.
(C) Line 3 will cause an error because a Highway variable cannot be instantiated as an object of type Road.
(D) Line 4 will cause an error because the Highway constructor is not properly called.
(E) The code segment compiles and runs without error.
public class Document
{
private int pageCount;
private int chapterCount;
public Document(int p, int c)
{
pageCount = p;
chapterCount = c;
}
public String toString()
{
return pageCount + " " + chapterCount;
}
}
The following code segment, which is intended to print the page and chapter counts of a Document object,
appears in a class other than Document.

Document d = new Document(245, 16);


System.out.println( /* missing code */ );
Which of the following can be used as a replacement for /* missing code */ so the code segment works as
intended?
I. d.toString()
II. d
III. d.pageCount + " " + d.chapterCount

A I only
B II only
C III only
D I and II only
E I, II and III
Free response

This question involves the design of an interface, writing a class that implements the interface, and writing a
method that uses the interface.

(a) A number group represents a group of integers defined in some way. It could be empty, or it could
contain one or more integers.

Write an interface named NumberGroup that represents a group of integers. The interface should have
a single contains method that determines if a given integer is in the group. For example,
- if group1 is of type NumberGroup, and it contains all numbers between -5 and 3
o then group1.contains(-5) would return true.
o but group1.contains(5) would return false.

- Write the complete NumberGroup interface. It must have exactly one method. (Hint: Do not
overthink this, there is not a lot of code.)
(b) A range represents a number group that contains all (and only) the integers between a minimum value
and a maximum value, inclusive.

Write the Range class, which implements NumberGroup. The Range class represents the group
of int values that range from a given minimum value up through a given maximum value, inclusive.
For example, the declaration

NumberGroup range1 = new Range(-3, 2);

represents the group of integer values -3, -2, -1, 0, 1, 2.

Write the complete Range class. Include all necessary instance variables and methods as well as a
constructor that takes two int parameters. The first parameter represents the minimum value, and the
second parameter represents the maximum value of the range. You may assume that the minimum is less
than or equal to the maximum.
(c) The MultipleGroups class represents a collection of NumberGroup objects and is a
NumberGroup. The MultipleGroups class stores the number groups in the instance variable
groupList which is initialized in the constructor.

Write the MultipleGroups method contains. The method takes an integer and returns true if and only
if the integer is contained in one or more of the number groups in groupList.

Code segments and a portion of the MulitpleGroups class is shown below. Your method should produce
the desired output from the sample code segments.

public class MultipleGroups implements NumberGroup


{

private NumberGroup[] groupList;

public MultipleGroups(int totalGroups)


{ groupList = new NumberGroup[totalGroups]; }

public void setGroup(int index, Range r)


{ groupList[index] = r; }

/** returns true if at least one of the number groups contains num */
public boolean contains(int num)
{ /** to be implemented in part (c) */ }
}

Sample code segment


NumberGroup multiple1 = new MultipleGroups(3);
multiple1.setGroup(0, new Range(5, 8));
multiple1.setGroup(1, new Range(10, 12));
multiple1.setGroup(2, new Range(1, 6));

System.out.println(multiple1.contains(2)); // true
System.out.println(multiple1.contains(9)); // false
System.out.println(multiple1.contains(6)); // true

/** returns true if at least one of the number groups contains num */
public boolean contains(int num)
{

You might also like