Knowee
Questions
Features
Study Tools

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.Compile time error because 'class Square' is not override all the abstract methods, so should declare it as 'abstract'b.Compile time error because trying to instantiate the 'class Square' which does not override all the abstract methodsc.Run time Errord.111222

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.Compile time error because 'class Square' is not override all the abstract methods, so should declare it as 'abstract'b.Compile time error because trying to instantiate the 'class Square' which does not override all the abstract methodsc.Run time Errord.111222

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

The correct answer is d. 111222

Explanation: In the given code, the class 'Square' extends the abstract class 'Quadrilateral' which in turn extends another abstract class 'Shape'. The class 'Shape' has two abstract methods 'calcArea()' and 'calcVolume()'. The class 'Quadrilateral' overrides the 'calcArea()' method and the class 'Square' overrides the 'calcVolume()' method. Hence, all the abstract methods are overridden.

In the main method, an object of class 'Square' is created and the methods 'calcArea()' and 'calcVolume()' are called. The 'calcArea()' method prints the value of 'i' which is 111 and the 'calcVolume()' method prints the value of 'j' which is 222. Hence, the output of the code will be 111222.

This problem has been solved

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

1/3

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.