| preferred AP College board partner for AP classes
AP Computer Science A/Unit 9: Inheritance
Start Practice TestPractice Test
Share
About Exam
medium
Inheritance and Compile-Time Errors
< Prev
Next >

Consider the following class declarations.

public class Book
{
    private int numPages;
    private String bookTitle;

    public Book(int pages, String title)
    {
        numPages = pages;
        bookTitle = title;
    }

    public String toString()
    {
        return bookTitle + " " + numPages;
    }

    public int length()
    {
        return numPages;
    }
}

public class AudioBook extends Book
{
    private int numMinutes;

    public AudioBook(int minutes, int pages, String title)
    {
        super(pages, title);
        numMinutes = minutes;
    }

    public int length()
    {
        return numMinutes;
    }

    public double pagesPerMinute()
    {
        return ((double) super.length()) / numMinutes;
    }
}

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

Line 1: Book[] books = new Book[2];
Line 2: books[0] = new AudioBook(100, 300, "The Jungle");
Line 3: books[1] = new Book(400, "Captains Courageous");
Line 4: System.out.println(books[0].pagesPerMinute());
Line 5: System.out.println(books[0].toString());
Line 6: System.out.println(books[0].length());
Line 7: System.out.println(books[1].toString());

Which of the following best explains why the code segment will not compile?

A

Line 4 will not compile because variables of type Book may only call methods in the Book class.

B

Line 5 will not compile because the AudioBook class does not have a method named toString declared or implemented.

C

Line 6 will not compile because the statement is ambiguous. The compiler cannot determine which length method should be called.

D

Line 2 will not compile because variables of type Book may not refer to variables of type AudioBook.

Hint
Did You Know?
Explain Why
Explain All Answers
Check Answer
Show Correct Answer

Question Leaderboard

Not enough data yet to show leaderboard.

AI Tutor

How can I help?

APFIVE © 2020.
Email: apfive@apfive.org|Privacy Policy