Menu
Question Index
...

What will be the output of the following program?

import java.util.*;
class Shape {
    int value;
    public Shape(int value) {
        this.value = value;
    }
    public int shape() {
        return value + value;
    }
}
class Car extends Shape {
    public Car(int value) {
        super(value);
    }
    public int shape() {
        return value * value;
    }
}
public class SuperClassAndSubClass {
    public static void main(String arg[]) {
        List<Car> shapes = new ArrayList<Car>();
        shapes.add(new Car(12));  shapes.add(new Car(2));
        Car[] shapesArray = new Car[2];
        shapesArray[0] = new Car(12);     shapesArray[1] = new Car(2);
        SuperClassAndSubClass object = new SuperClassAndSubClass();
        object.printShapesArray(shapesArray);
        object.printShapesList(shapes);
    }
    public void printShapesList(List<Shape> shapes) {
        System.out.println();
        System.out.print("List :");
        for (Shape shape : shapes) {
            System.out.print(shape.shape() + " ");
        }
    }
    public void printShapesArray(Shape[] shapes) {
        System.out.print("Array :");
        for (Shape shape : shapes) {
            System.out.print(((Car) shape).shape() + " ");
        }
    }
}


Array :144 4
List :144 4
Array :24 4
List :144 4
Some other output
Compilation Error or Runtime Error

Doubts

Problems

Topic: Unknown

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App