What is the output of the given code?abstract class Shape{ int i = 111, j = 222; abstract void calcArea(); abstract void calcVolume();} abstract class Quadrilateral extends Shape{ void calcArea() { System.out.println(i); }}class Square extends Quadrilateral{ void calcVolume() { System.out.println(j); }}public class Test{ public static void main(String[] args) { Square c = new Square(); c.calcArea(); c.calcVolume(); }}Select one:a.111222b.Compile time error because 'class Square' is not override all the abstract methods, so should declare it as 'abstract'c.Compile time error because trying to instantiate the 'class Square' which does not override all the abstract methodsd.Run time Error
Question
What is the output of the given code?abstract class Shape{ int i = 111, j = 222; abstract void calcArea(); abstract void calcVolume();} abstract class Quadrilateral extends Shape{ void calcArea() { System.out.println(i); }}class Square extends Quadrilateral{ void calcVolume() { System.out.println(j); }}public class Test{ public static void main(String[] args) { Square c = new Square(); c.calcArea(); c.calcVolume(); }}Select one:a.111222b.Compile time error because 'class Square' is not override all the abstract methods, so should declare it as 'abstract'c.Compile time error because trying to instantiate the 'class Square' which does not override all the abstract methodsd.Run time Error
Solution
The correct answer is:
b. Compile time error because 'class Square' is not overriding all the abstract methods, so it should be declared as 'abstract'
Explanation:
In Java, when a class extends an abstract class, it must provide implementations for all abstract methods declared in the abstract class. In this case, the class 'Square' extends the abstract class 'Quadrilateral' but does not provide an implementation for the abstract method 'calcVolume()'. Therefore, a compile-time error will occur. To fix this, 'class Square' should either provide an implementation for 'calcVolume()' or be declared as 'abstract'.
Similar Questions
Will the following code get executed successfully ?abstract class Shape{ int i = 111, j = 222; abstract void calcArea(); abstract void calcVolume();}class Square extends Shape{ void calcVolume() { System.out.println(j); } void calcArea(){ System.out.println(j); }}public class Test{ public static void main(String[] args) { Square c = new Square(); c.calcArea(); c.calcVolume(); }}Select one:a.Yes, the code will get executed successfully.b.No – Compilation error.
What is the result of the following program?1: public class Squares {2: public static long square(int x) {3: long y = x * (long) x;4: x = -1;5: return y;6: }7: public static void main(String[] args) {8: int value = 9;9: long result = square(value);10: System.out.println(value);11: } }*A. -1B. 9C. 81D. Compiler error on line 9.E. Compiler error on a different line.
Find OutputType the correct output for the code given below. Also, explain the reason behind your answer.class Shape { public Shape() { System.out.println("inside Shape class default constructor"); }}class Rectangle extends Shape { public Rectangle() { System.out.println("inside Rectangle class default constructor"); }}class Square extends Rectangle { public Square() { System.out.println("inside Square class default constructor"); }}public class ConstructorChain { public static void main(String[] args) { Square sq = new Square(); }}
Select the correct answerWhat will be the output of the following Java program? class shape { int breadth; int height; int area; void area() { area = 1/2 * breadth * height; } void area(int x) { area = x; } } class triangle { public static void main(String args[]) { shape obj = new shape(); obj.breadth = 4; obj.height = 4; obj.area(4); System.out.println(obj.area); } }Options04816
What will be the output of the following code?public class Circle { private double radius; public Circle(double radius) { this.radius = radius; } public double getArea() { return Math.PI * radius * radius; } public static void main(String[] args) { Circle c = new Circle(2.5); System.out.println(c.getArea()); }}Question 14Answera.3.14b.6.28c.19.63d.Compilation error
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.