Knowee
Questions
Features
Study Tools

Given:class FourWheeler { public FourWheeler () { System.out.print(1); } } class Car extends FourWheeler{ public Car() { System.out.print(2); } } class Audi extends Car{ public Audi() {System.out.print(3); } } public class Driver{ public static void main( String[] argv ) {new Audi(); } } What is the result when this code is executed?Select one:a.The code runs with no outputb.321c.123d.3

Question

Given:class FourWheeler { public FourWheeler () { System.out.print(1); } } class Car extends FourWheeler{ public Car() { System.out.print(2); } } class Audi extends Car{ public Audi() {System.out.print(3); } } public class Driver{ public static void main( String[] argv ) {new Audi(); } } What is the result when this code is executed?Select one:a.The code runs with no outputb.321c.123d.3

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

Solution

The correct answer is c.123.

Here's why:

When you create an instance of a subclass (in this case, Audi), the constructor of the superclass (in this case, FourWheeler and Car) is called first. This is because the subclass inherits the properties of the superclass, and the superclass needs to be initialized first to ensure that all its properties are available to the subclass.

So, when you create a new Audi object in the main method, the constructors are called in this order: FourWheeler -> Car -> Audi.

Therefore, the output will be 123.

This problem has been solved

Similar Questions

What will be the output of the following program ?class FourWheeler{    public FourWheeler()    {        System.out.println("Class FourWheeler");    }}class Car extends FourWheeler{    public Car()     {        System.out.println("Class Car");        }}class Audi extends Car{   public Audi()    {        super();        System.out.println("Class Audi");    }}class Driver{    public static void main(String args[])    {        Audi cc=new Audi();    }}Select one:a.Class AudiClass CarClass FourWheelerb.Compile Time Errorc.Class FourWheelerClass CarClass Audid.Exception occurs

class FourWheeler{    public FourWheeler()    {        System.out.println("Class FourWheeler");    }}class Car extends FourWheeler{    public Car()     {        System.out.println("Class Car");        }}class Audi extends Car{   public Audi()    {        super();        System.out.println("Class Audi");    }}class Driver{    public static void main(String args[])    {        Audi cc=new Audi();    }}

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 vehicle moves"The code does not compileNone of these"The car moves"

Analyze the following code carefully. Please select the one that applies.public class Test {  public static void main(String[] args) {    A a = new A();    a.print();  }}class A {  String s;  A(String s) {    this.s = s;  }void print() {    System.out.println(s);  }}

what is the result of the following piece of code:public class Person{ public void talk(){ System.out.print("I am a Person"); }}public class Student extends Person{ public void talk(){ System.out.print("I am a Student"); }}public class Test{ public static void main(String args[]){ Person p = new Student(); p.talk(); }}I am a Person I am a Student I am a Person I am a StudentI am a Student I am a Person

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.