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
Solution
The correct answer is "Circle SemiSemi".
Here's the step by step explanation:
-
When a new
SemisemiCircleobject is created in themainmethod, the constructor of its superclassCircleis called first. This is because in Java, a call tosuper()is added at the first line of each constructor automatically by the compiler if no call tosuper()orthis()is written by the programmer. -
In the
Circleconstructor, the string "Circle " is appended to the static variables. -
After the
Circleconstructor is finished, theSemisemiCircleconstructor is executed. Here, the string "SemiSemi " is appended tos. -
Finally,
sis 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.
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);
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.