Knowee
Questions
Features
Study Tools

Select the correct answerPredict the output of the given Java Program. class Circle {    static String s = " ";    protected Circle() {        s += "Circle ";    } } class SemiCircle extends Circle {    private SemiCircle() {        s += "Semi ";    } }   class SemisemiCircle extends Circle {    private SemisemiCircle() {        s += "SemiSemi ";    }    public static void main(String[] args) {         new SemisemiCircle();        System.out.println(s);    } }OptionsCircleCircle SemiSemiSemiSemi Circle

Question

Select the correct answerPredict the output of the given Java Program. class Circle {    static String s = " ";    protected Circle() {        s += "Circle ";    } } class SemiCircle extends Circle {    private SemiCircle() {        s += "Semi ";    } }   class SemisemiCircle extends Circle {    private SemisemiCircle() {        s += "SemiSemi ";    }    public static void main(String[] args) {         new SemisemiCircle();        System.out.println(s);    } }OptionsCircleCircle SemiSemiSemiSemi Circle

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

Solution

The correct answer is "Circle SemiSemi".

Here's the step by step explanation:

  1. When a new SemisemiCircle object is created in the main method, the constructor of its superclass Circle is called first. This is because in Java, a call to super() is added at the first line of each constructor automatically by the compiler if no call to super() or this() is written by the programmer.

  2. In the Circle constructor, the string "Circle " is appended to the static variable s.

  3. After the Circle constructor is finished, the SemisemiCircle constructor is executed. Here, the string "SemiSemi " is appended to s.

  4. Finally, s is printed out, resulting in "Circle SemiSemi".

Note: The SemiCircle class and its constructor are not used anywhere in the program, so they don't affect the output.

This problem has been solved

Similar Questions

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

Select the correct answerWhat is the output of the following Java program?public class Vehicle { public void move() { System.out.println("The vehicle moves"); }}public class Car extends Vehicle { public void move() { System.out.println("The car moves"); }}public class Main { public static void main(String[] args) { Vehicle vehicle = new Car(); vehicle.move(); }}Options"The car moves""The vehicle moves"The code does not compileNone of these

Select the correct answerPredict the output of the given Java Program. class Tiger {    private final void flipper() {        System.out.println("Tiger");    } }   class Cub extends Tiger {    public final void flipper() {        System.out.println("Cub");    }    public static void main(String[] args) {         new Cub().flipper();    }}OptionsCubTigerCompilation FailsNone of the mentioned

Select the correct answerWhat will be the output of the following Java program?class String_demo {  public static void main(String args[])  {    char chars[] = {'f', 'o', 'r'};    String s = new String(chars);    System.out.println(s);  }}Optionsfrforo

A class Circle is defined in the following code:public class Circle { int radius; static String name; void getPerimeter() { } static void getArea() { }}Let c be an instance of Circle, which of the statements are correct?System.out.println(c.radius);c.getArea();c.getPerimeter();System.out.println(c.name);

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.