What will be the output of the code given below?

class A {
	public int count;

	public A() {
		count = 10;
	}

	public int method1() {
		int count = 20;
		return this.count;
	}
}

class B extends A {
	public int method1() {
		return this.count = 15;
	}

	public int method2() {
		return 20;
	}
}

class C extends B {
	public int method2() {
		return 40;
	}
}

public class Tester {
	public static void main(String args[]) {
		A obj1 = new A();
		B obj2 = new B();
		C obj3 = new C();
		System.out.println(obj1.method1() + obj3.method1() + obj3.method2());

	}
}

75

65

70

45

Verified Answer
Correct Option - b
Anush said

The output of the code will be 70. Here's how:
1. obj1.method1(): In class A, the method1() returns this.count. Since the constructor of A sets count = 10, it returns 10.
2. obj3.method1(): The object obj3 is of class C, but since method1() is not overridden in C, it uses the implementation from class B. In B's method1(), this.count is set to 15, and it returns 15.
3. obj3.method2(): In class C, method2() is overridden and returns 40.
The final output is the sum of these values: 10 + 15 + 40 = 70.

To get all Self Assessment - Programming using Java Exam questions Join Telegram Group https://rebrand.ly/lex-telegram-236dee

Telegram

We're passionate about offering best placement materials and courses!! A one stop place for Placement Materials. We daily post Offcampus updates and Placement Materials.

Qtr No. 213, New Town Yehlanka Indore 454775

admin@prepflix.in

Updated on Mon, 8 Dec 2025