Which three lines will compile and output "right on!"?

Given:

public class Speak { /* Line 1 */
public static void main(String[] args) { /* Line 2 */
Speak speakIT = new Tell(); /* Line 3 */
Tell tellIt = new Tell(); /* Line 4 */
speakIT.tellItLikeItIs(); /* Line 5 */
(Truth)speakIt.tellItLikeItIs(); /* Line 6 */
((Truth)speakIt).tellItLikeItIs(); /* Line 7 */
tellIt.tellItLikeItIs(); /* Line 8 */
(Truth)tellIt.tellItLikeItIs(); /* Line 9 */
((Truth)tellIt).tellItLikeItIs(); /* Line 10 */
}
}
class Tell extends Speak implements Truth {
public void tellItLikeItIs() {
System.out.println(“Right on!”);
}
}
interface Truth { public void tellItLikeItIs()};

Which three lines will compile and output “right on!”?

Given:

public class Speak { /* Line 1 */
public static void main(String[] args) { /* Line 2 */
Speak speakIT = new Tell(); /* Line 3 */
Tell tellIt = new Tell(); /* Line 4 */
speakIT.tellItLikeItIs(); /* Line 5 */
(Truth)speakIt.tellItLikeItIs(); /* Line 6 */
((Truth)speakIt).tellItLikeItIs(); /* Line 7 */
tellIt.tellItLikeItIs(); /* Line 8 */
(Truth)tellIt.tellItLikeItIs(); /* Line 9 */
((Truth)tellIt).tellItLikeItIs(); /* Line 10 */
}
}
class Tell extends Speak implements Truth {
public void tellItLikeItIs() {
System.out.println(“Right on!”);
}
}
interface Truth { public void tellItLikeItIs()};

Which three lines will compile and output “right on!”?

A.
Line 5

B.
Line 6

C.
Line 7

D.
Line 8

E.
Line 9

F.
Line 10



Leave a Reply 3

Your email address will not be published. Required fields are marked *


Jazz

Jazz

“C,D,F”

Explanation:

Speak speakIT = new Tell(); // variable speakIT of type Speak.

In-correct:
A – speakIT.tellItLikeItIs(); /* Line 5 */ // Compile Time Error (no method found)
B – (Truth)speakIt.tellItLikeItIs(); /* Line 6 */ //Compile Time Error (not a statement)
E – (Truth)tellIt.tellItLikeItIs(); /* Line 9 */ // Compile Time Error (not a statment)

Correct:

C – ((Truth)speakIT).tellItLikeItIs(); /* Line 7 */ //change speakIt to speakIT.
D – tellIt.tellItLikeItIs(); /* Line 8 */
F – ((Truth)tellIt).tellItLikeItIs(); /* Line 10 */

James

James

The Answer is C, D, F.

Note that the interface should be written as:

interface Truth {
public void tellItLikeItIs();
}

The semicolon goes after the method signature, not after the interface body.

pooja

pooja

Only D and F will compile-