Question

Show the output of running the class Test in the following code lines: interface A { void print (); } class C ( class B exten
w class Test { public static void main(String[] args) { B b = new B(); if (b instanceof A) System.out.println(b is an instan
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

Option 1: b is an instance of A followed by b is an instance of C

Explanation:

The instanceof keyword is a operator it returns true if a reference variable is containing a given type of object reference, otherwise returns false. In the given code segment class B is sub-class of C and it implements the interface A, therefore, b reference variable is a instance of Class C as well as interface A. Remember, vice-versa is not true.

Add a comment
Know the answer?
Add Answer to:
Show the output of running the class Test in the following code lines: interface A {...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Show the output of running the class Test in the following code lines: a) Nothing. b)...

    Show the output of running the class Test in the following code lines: a) Nothing. b) b is an instance of A followed by b is an instance of c c) b is an instance of C d) b is an instance of A interface A { void print (); } class C {} class B extends c implements A { public void print() { } } class Test { public static void main(String[] args) { B b = new...

  • In each of the following questions, first use NetBeans IDE to run the code (if there...

    In each of the following questions, first use NetBeans IDE to run the code (if there is an error fix it) then take a screenshot of the output and paste it under (A). In (B), justify the output you obtained in (A). - 9. Given the following: public class WorkingSheet {     public static void main(String[] args) {        B myB = new B();        A myA = new B();        System.out.print(myB instanceof A);        System.out.print(myB instanceof C);        System.out.print(myA...

  • I Need UML Design for the following Java code: Colorable interface: public interface Colorable { //a...

    I Need UML Design for the following Java code: Colorable interface: public interface Colorable { //a void method named howToColor(). void howToColor(); } GeometricObject class: public class GeometricObject { } Sqaure class: public class Square extends GeometricObject implements Colorable{ //side variable of Square double side;    //Implementing howToColor() @Override public void howToColor() { System.out.println("Color all four sides."); }    //setter and getter methods for Square public void setSide(double side) { this.side = side; }    public double getSide() { return...

  • Abstract classes and Interfaces problems 10. Explain one similarity and one difference between ab...

    Abstract classes and Interfaces problems 10. Explain one similarity and one difference between abstract classes and interfaces. 11. Consider the following declarations. public interface Shape{ int someFunction(Shape other); //other functions not shown } public class Square implements Shape {/*implementation not shown*/} Which of the following function headings of someFunction must be added to the declaration of the Square class such that it will satisfy the Shape interface? public int someFunction (Shape other) public int someFunction (Square other) public boolean someFunction(Object...

  • Abstract classes and Interfaces problems 10. Explain one similarity and one difference between abstract classes and...

    Abstract classes and Interfaces problems 10. Explain one similarity and one difference between abstract classes and interfaces. 11. Consider the following declarations. public interface Shape{ int someFunction(Shape other); //other functions not shown } public class Square implements Shape {/*implementation not shown*/} Which of the following function headings of someFunction must be added to the declaration of the Square class such that it will satisfy the Shape interface? public int someFunction (Shape other) public int someFunction (Square other) public boolean someFunction(Object...

  • 1. Analyze the following code: public class Test implements Runnable { public static void main(String[] args) { Thread t = new Thread(this); t.start(); } public void run() { System....

    1. Analyze the following code: public class Test implements Runnable { public static void main(String[] args) { Thread t = new Thread(this); t.start(); } public void run() { System.out.println("test"); } } 1. The code compiles but will not print anything since t does not invoke the run method. 2. The code will not compile since you cannot invoke "this" in a static method. 3. The program compiles, runs, and prints tests on the console. 2. What will the following example...

  • Code using Java What is an interface? Complete the following code: interface IExample{ public void print();...

    Code using Java What is an interface? Complete the following code: interface IExample{ public void print(); } class Example1 implements IExample{ . . . } // Driver class public class Questions { public static void main(String[] args) { . . . } } To have this message when you execute it: Implementation of an interface

  • Treat.java HairballRemedy.java ScoobyTreat.java Munchies.java public interface Treat { String getColor(); String getSize(); } public class HairballRemedy...

    Treat.java HairballRemedy.java ScoobyTreat.java Munchies.java public interface Treat { String getColor(); String getSize(); } public class HairballRemedy implements Treat { public String getColor() { return "blue"; } public String getSize() { return "0.5 oz"; } } public class ScoobyTreat implements Treat { public String getColor() { return "green"; } public String getSize() { return "2 oz"; } } public class Munchies { public static void main(String[] args) { HairballRemedy a=new HairballRemedy(); ScoobyTreat b=new ScoobyTreat(); Treat c=a; Treat d=b; System.out.println(a.getColor()); System.out.println(b.getColor()); System.out.println(c.getColor());...

  • What is wrong with the following Java Code. public class TestEdible { abstract class Animal {...

    What is wrong with the following Java Code. public class TestEdible { abstract class Animal { /** Return animal sound */ public abstract String sound(); } class Chicken extends Animal implements Edible { @Override public String howToEat() { return "Chicken: Fry it"; } @Override public String sound() { return "Chicken: cock-a-doodle-doo"; } } class Tiger extends Animal { @Override public String sound() { return "Tiger: RROOAARR"; } } abstract class Fruit implements Edible { // Data fields, constructors, and methods...

  • List the output for the following lines of code (Hint: 4 lines of code should print):...

    List the output for the following lines of code (Hint: 4 lines of code should print): public class MPL3 { public static void main(String[] args) { String s1 = "Java isn't just for breakfast."; String s2 = "JAVA isn't just for breakfast."; String s3 = "A cup of java is a joy forever."; if (s3.compareToIgnoreCase(s1) < 0) { System.out.println("\"" + s3 + "\""); System.out.println("precedes"); System.out.println("\"" + s1 + "\""); System.out.println("in alphabetic ordering"); } else System.out.println("s3 does not precede s1."); }...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT