Knowee
Questions
Features
Study Tools

Analyze the following code and choose the correct answer from below.public class Test {   public static void main(String[] args) {     new B();   } } class A {   int i = 7;      public A() {     System.out.println("i from A is " + i);   }    public void setI(int i) {     this.i = 2 * i;   } } class B extends A {   public B() {     setI(20);     // System.out.println("i from B is " + i);       }    @Override   public void setI(int i) {     this.i = 3 * i;   } }

Question

Analyze the following code and choose the correct answer from below.public class Test {   public static void main(String[] args) {     new B();   } } class A {   int i = 7;      public A() {     System.out.println("i from A is " + i);   }    public void setI(int i) {     this.i = 2 * i;   } } class B extends A {   public B() {     setI(20);     // System.out.println("i from B is " + i);       }    @Override   public void setI(int i) {     this.i = 3 * i;   } }

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

Solution

The given code is written in Java. It consists of three classes: Test, A, and B. Class B extends Class A, meaning it inherits the properties and methods of Class A.

The main method in the Test class creates a new instance of class B.

In class A, there is an integer variable 'i' initialized with the value 7. There is a constructor that prints the value of 'i' and a method setI() that sets the value of 'i' to twice the input value.

In class B, which extends class A, the constructor calls the setI() method with the value 20. The setI() method in class B is overridden to set the value of 'i' to thrice the input value.

However, the print statement in the constructor of class B is commented out, so it won't print anything.

If the print statement in class B's constructor was not commented out, it would print "i from B is 60" because the setI() method in class B sets 'i' to 3 times the input value (20*3 = 60).

But in the current state of the code, when a new instance of class B is created, it will only print "i from A is 7" from the constructor of class A. This is because the print statement in class B's constructor is commented out and the constructor of the superclass (class A) is called before the constructor of the subclass (class B).

This problem has been solved

Similar Questions

Analyze the following code and choose a correct answer from below. public class Test {    public static void main(String[] args) {     B b = new B();     b.m(5);     System.out.println("i is " + b.i);   } } class A {   int i;   public void m(int i) {      this.i = i;    } }  class B extends A {   public void m(String s) {   } } Group of answer choicesThe program has a compile error, because m is overridden with a different signature in B.The program has a compile error, because b.m(5) cannot be invoked since the method m(int) is hidden in B.The program has a runtime error on b.i, because i is not accessible from b.The method m is not overridden in B. B inherits the method m from A and defines an overloaded method m in B.

B A class A  {    String name="A";    public String getName()  {        return name;    }      String greeting()  {        return "class A";    }}class B extends A  {    String name="B";          String greeting()  {        return "class B";    }  }public class Test   {    public static void main(String arg[])  {        A a=new A();        A b=new B();        System.out.println(a.greeting()+" has name "+a.getName());        System.out.println(b.greeting()+" has name "+b.getName());    }} Place the names "A" and "B" in the following output. class has name                            class has name

What will be the output?class A{ int i = 10; public void printValue(){ System.out.print("Value-A"); }}class B extends A{ int i = 12; public void printValue(){ System.out.print("Value-B"); }}public class Test{ public static void main(String args[]){ A a = new B(); a.printValue(); System.out.print(a.i); }}Value-B 11Value-B 10Value-A 10 Value-A 11

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);  }}

Select the correct answerWhat will be the output of the following Java program?class A { int i; void display() { System.out.println(i); }}class B extends A { int j; void display() { System.out.println(j); }}class inheritance_demo { public static void main(String args[]) { B obj = new B(); obj.i = 1; obj.j = 2; obj.display(); }}Options102Compilation Error

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.