Multiple choice

What is the output of the given program code?

public class ScopeTest
{
public static void main(String[] args)
{
ScopeTest sc, scA, scB;
sc = new ScopeTest();
scA = new ScopeTestA();
scB = new ScopeTestB();
System.out.println("Hash is : " + sc.getHash() + ", " + scA.getHash() + ", " + scB.getHash());
}
public int getHash()
{
return 111111;
}
}
class ScopeTestA extends ScopeTest
{
public long getHash()
{
return 44444444;
}
}
class ScopeTestB extends ScopeTest
{
public long getHash()
{
return 999999999;
}
}

  1. Hash is: 111111, 44444444, 999999999

  2. Blank output screen

  3. An exception is thrown at runtime

  4. Compilation Fails

  5. None of these

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

This is the correct choice. The compilation fails as SampleClassA and SampleClassB cannot override SampleClass’s getHash() method because the return type of SampleClass’s getHash() method is int, while the return type of SampleClassA and SampleClassB is long. Note: If all three classes had the same return type, the output would be: Hash is : 111111, 44444444, 999999999 So, this is the correct choice.