A Java music app defines the following classes and then executes Media m = new Song();.
class Media { String describe() { return "media"; } } class Song extends Media { String describe() { return "song"; } int bpm() { return 120; } }
Select all of the statements below that are true.
After Song s = (Song) m;, the expression s == m evaluates to true.
m.describe() returns "song", because the object referred to is still a Song.
Media x = new Media(); Song y = (Song) x; is rejected by the compiler.
m.bpm() does not compile, because the compiler checks it against the static type Media.
The assignment creates a new Media object that keeps only the fields Media declares.
Unlock full course notes by signing up
Classifying cast outcomes in a bank hierarchy
A Java banking program defines these classes:
It then declares Account a = new Premium(); and Savings s = new Savings();. Treat each statement in the table on its own, as if it were the only statement after these two declarations. In the Outcome column, type OK if the statement compiles and runs, COMPILE if the compiler rejects it, or RUNTIME if it compiles but throws a ClassCastException.
Tracking stock after reference reassignment
A warehouse program uses the following Java classes to track the number of kits in a stock record:
The following statements execute in order inside a method:
Determine the final number of kits accessed through saved, the final number of kits accessed through (KitStock) current, and the number of KitStock instances created by the shown new expressions. Give the first two quantities in kits and all three answers as exact integers.
Tracing instanceof tests in a shape loop
A Java geometry program defines these classes:
Its main method runs:
What value does the program print? Give the answer as a whole number.
Casting an array versus casting its elements
A Java media application uses these class declarations:
Each labelled case below runs independently inside a method, starting with fresh variables. Classify what happens when its two statements execute.
Typed array: