Question: 1. Predict the output of following Java Program: class A { int i; void display() { System.out.println(i); } } class B extends A { int

1. Predict the output of 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();

}

}

Question 1 options:

1

A.Compilation Error

0

2

2.

Predict the output of following Java Program

class A {

int i;

}

class B extends A {

int j;

void display() {

super.i = j + 1;

System.out.println(j + " " + i);

}

}

class inheritance_demo {

public static void main(String args[])

{

B obj = new B();

obj.i=1;

obj.j=2;

obj.display();

}

}

Question 2 options:

2 2

2 3

3 2

3 3

3.

Predict the output of following Java Program

class Grandparent {

public void Print() {

System.out.println("Grandparent'sPrint()");

}

}

class Parent extends Grandparent {

public void Print() {

System.out.println("Parent'sPrint()");

}

}

class Child extends Parent {

public void Print() {

super.super.Print();

System.out.println("Child'sPrint()");

}

}

public class Main {

public static void main(String[] args){

Child c = newChild();

c.Print();

}

}

Question 3 options:

Grandparent's Print() Parent's Print() Child's Print()

Compiler Error in super.super.Print()

Runtime error

Step by Step Solution

3.46 Rating (156 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Qn2 Share irce File ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Electrical Engineering Questions!