What is the output of the following application?package flying;class Rotorcraft {protected final int height = 5;abstract int fly();}public class Helicopter extends Rotorcraft {private int height = 10;protected int fly() {return super.height;}public static void main(String[] unused) {Helicopter h = (Helicopter)new Rotorcraft();System.out.print(h.fly());}}A. 5B. 10C. The code does not compile. D. The code compiles but produces a ClassCastException at runtime.Question 3AnswerBCAD
Question
What is the output of the following application?package flying;class Rotorcraft {protected final int height = 5;abstract int fly();}public class Helicopter extends Rotorcraft {private int height = 10;protected int fly() {return super.height;}public static void main(String[] unused) {Helicopter h = (Helicopter)new Rotorcraft();System.out.print(h.fly());}}A. 5B. 10C. The code does not compile. D. The code compiles but produces a ClassCastException at runtime.Question 3AnswerBCAD
Solution
The code will not compile. The reason is that you are trying to cast a parent object (Rotorcraft) to a child object (Helicopter). This is not allowed in Java. The correct answer is C. The code does not compile.
Similar Questions
Given:class FourWheeler { public FourWheeler () { System.out.print(1); } } class Car extends FourWheeler{ public Car() { System.out.print(2); } } class Audi extends Car{ public Audi() {System.out.print(3); } } public class Driver{ public static void main( String[] argv ) {new Audi(); } } What is the result when this code is executed?Select one:a.321b.The code runs with no outputc.123d.3
What is the result of the following code? 1: public abstract class Bird { 2: private void fly() { System.out.println("Bird is flying"); } 3: public static void main(String[] args) { 4: Bird bird = new Pelican(); 5: bird.fly(); 6: } 7: } 8: class Pelican extends Bird { 9: protected void fly() { System.out.println("Pelican is flying"); } 10: } *1 pointA. Bird is flyingB. Pelican is flyingC. The code will not compile because of line 4.D. The code will not compile because of line 5.E. The code will not compile because of line 9.
What will be the output of the following code snippet?public class MyClass { static int value = 10; public void printValue() { System.out.println(value); } public static void main(String[] args) { MyClass obj = new MyClass(); obj.printValue(); }}Question 4Answera.0b.10c.Compilation errord.Runtime exception
What is the output of this program? class A { public int i; private int j; } class B extends A { void display() { super.j = super.i + 1; System.out.println(super.i + " " + super.j); } } class inheritance { public static void main(String args[]) { B obj = new B(); obj.i=1; obj.j=2; obj.display(); } }2 23 3Runtime ErrorCompilation Erro
What is the output of the following code? 1: public abstract class Whale { 2: public abstract void dive() {}; 3: public static void main(String[] args) { 4: Whale whale = new Orca(); 5: whale.dive(); 6: } 7: } 8: class Orca extends Whale { 9: public void dive(int depth) { System.out.println("Orca diving"); } 10: }*1 pointA. Orca divingB. The code will not compile because of line 2.C. The code will not compile because of line 8.D. The code will not compile because of line 9.E. The output cannot be determined from the code provided.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.