Are you taking CLEP classes? Try clep.ai
| preferred AP College board partner for AP classes
AP Computer Science A/Unit 9: Inheritance
Start Practice TestPractice Test
About Exam
medium Solved by 1 students

Polymorphism and Method Compilation

< Prev
Next >

Consider the following Book and AudioBook class definitions.

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;
    }
}

Consider the following code segment, which appears in 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 statement best explains why the code segment will fail to compile?

A

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

B

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

C

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

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
Report Question
Mark for review

Question Leaderboard

Not enough data yet to show leaderboard.

No comments yet. Be the first to comment!

AI Tutor

How can I help?

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