Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

Qn2 Share irce File ... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Java Programming, Comprehensive Version

Authors: Y. Daniel Liang

10th Edition

133761312, 978-0133761313

More Books

Students also viewed these Electrical Engineering questions