Bird
0
0

What will be the output of this Java code?

medium📝 Predict Output Q5 of 15
Java - Abstraction
What will be the output of this Java code?
abstract class Shape {
  abstract double area();
}
class Circle extends Shape {
  double radius;
  Circle(double r) { radius = r; }
  double area() { return 3.14 * radius * radius; }
}
public class Test {
  public static void main(String[] args) {
    Shape s = new Circle(2);
    System.out.println(s.area());
  }
}
ACompilation error
B4.0
C12.56
D0.0
Step-by-Step Solution
Solution:
  1. Step 1: Calculate area using Circle's method

    Area = 3.14 * 2 * 2 = 12.56
  2. Step 2: Confirm method call and output

    Shape reference points to Circle object; calling area() returns 12.56, printed to console.
  3. Final Answer:

    12.56 -> Option C
  4. Quick Check:

    Abstract method overridden returns correct value [OK]
Quick Trick: Abstract method calls subclass implementation [OK]
Common Mistakes:
  • Using wrong formula for area
  • Expecting compilation error due to abstract class
  • Confusing radius with diameter

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes