Knowee
Questions
Features
Study Tools

the correct answerAssuming that all the necessary imports are done, what will be the output of the following Java program? class parent { int a; } class child extends parent { int b; void display() { super.a = b + 1; System.out.println(b + " " + i); } } class inheritance { public static void main(String args[]) { child obj = new child(); obj.a=1; obj.b=2; obj.display(); }}

Question

the correct answerAssuming that all the necessary imports are done, what will be the output of the following Java program? class parent { int a; } class child extends parent { int b; void display() { super.a = b + 1; System.out.println(b + " " + i); } } class inheritance { public static void main(String args[]) { child obj = new child(); obj.a=1; obj.b=2; obj.display(); }}

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

Solution

The program will not compile because there is no variable 'i' defined in the 'display()' method of the 'child' class. The line 'System.out.println(b + " " + i);' is trying to print the value of 'i', which is not declared anywhere in the program.

To fix this, you should replace 'i' with 'a' (the variable from the parent class that is inherited by the child class). The corrected 'display()' method would look like this:

void display() { super.a = b + 1; System.out.println(b + " " + a); }

With this correction, the output of the program would be "2 3". This is because 'b' is 2 (as set in the main method) and 'a' is 'b + 1', which is 3.

This problem has been solved

Similar Questions

elect the correct answerAssuming that all the necessary imports are done, what will be the output of the following Java code? class parent {  int x;     void display() {      System.out.println(x);     } }     class child extends parent {     int y;     void display() {         System.out.println(y);     } }     class method_overriding {     public static void main(String args[]) {          child obj = new child();         obj.x=4;         obj.y=5;            obj.display();          } }Options354Compilation Error

the correct answerAssuming that all the necessary imports are done, what will be the output of the following Java code? class ct {     public int x;     private int y; }     class uv extends ct {    void display() {        super.y = super.x + 1;        System.out.println(super.x + " " + super.y);     } }  class inheritance {    public static void main(String args[]) {         uv obj = new uv();         obj.x = 1;         obj.y = 2;            obj.display();          } }Options2 2Runtime Error3 3Compilation Error

What 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(); }}Options02Compilation Error1

elect the correct answerWhat will be the output of the following Java program? class parent { int a; public void display() { System.out.println(a); } } class child extends parent { int b; public void display() { System.out.println(b); } } class Dynamic_dispatch { public static void main(String args[]) { child obj2 = new child(); obj2.a = 2; obj2.b = 4; parent r; r = obj2; r.display(); } }Options6428

Assuming that all the necessary imports are done, what will be the output of the following Java code? class codetantra {     public int a;     public int b;     codetantra() {         a = 5;         b = 6; } }     class coding extends codetantra {     int i;  coding() {         super();     }  }     class inheritance {     public static void main(String args[]) {         coding obj = new coding();         System.out.println(obj.a + " " + obj.b);      } }Options6 55 6Runtime 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.