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.
Question
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.
Solution
The code will not compile because of line 2. In Java, you cannot have a method body in an abstract method. The correct way to declare an abstract method in Java is:
public abstract void dive();
So, the correct answer is B. The code will not compile because of line 2.
Similar Questions
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 is the output of the following code? 1: class Mammal { 2: public Mammal(int age) { 3: System.out.print("Mammal"); 4: } 5: } 6: public class Platypus extends Mammal { 7: public Platypus() {8: System.out.print("Platypus"); 9: } 10: public static void main(String[] args) { 11: new Mammal(5); 12: } 13: } *1 pointA. PlatypusB. MammalC. PlatypusMammalD. MammalPlatypusE. The code will not compile because of line 8.F. The code will not compile because of line 11
What is the output of the following code? 1: abstract class Reptile { 2: public final void layEggs() { System.out.println("Reptile laying eggs"); } 3: public static void main(String[] args) { 4: Reptile reptile = new Lizard(); 5: reptile.layEggs(); 6: } 7: } 8: public class Lizard extends Reptile { 9: public void layEggs() { System.out.println("Lizard laying eggs"); } 10: } *1 pointA. Reptile laying eggsB. Lizard laying eggsC. 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.
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 output of the following code?1. public class A {2. int add(int i, int j){3. return i+j;4. }5. }6. public class B extends A{7. public static void main(String argv[]){ 8. short s = 9;9. System.out.println(add(s,6));10. }11.}
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.