Multiple choice technology programming languages

package inheritance; public class TestInheritance2 { public int empId =100; public String name ="tcs" ; } package inheritance1; import inheritance.TestInheritance2; public class TestInheritance_2 extends TestInheritance2 { void printEmpId() { System.out.println("EmpId is "+empId); } public static void main(String[] args) { TestInheritance_2 T_2 = new TestInheritance_2(); T_2.printEmpId(); } }

  1. Compiler Error

  2. EmpId is 0

  3. EmpId is 100

  4. Exception

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

The code compiles and runs successfully because empId is a public field in TestInheritance2, and public members are accessible from any package. The subclass TestInheritance_2 directly inherits empId and can access it, so the output is "EmpId is 100". Option A is wrong because public access across packages is valid. Option B is wrong because empId retains its initialized value of 100, not the default 0.

AI explanation

The field empId is declared public, so it's accessible from anywhere, including a subclass in a different package, as long as that subclass properly imports the parent class — which it does here. Since the subclass inherits the public field and its value is set to 100 in the parent's field initializer, printing empId from within the subclass correctly outputs "EmpId is 100" with no compiler error or exception.