Examveda
Examveda

What will be the result after compiling this code?
class SuperClass{
      public int doIt(String str, Integer... data)throws Exception{
            String signature = "(String, Integer[])";
            System.out.println(str + " " + signature);
            return 1;
      }
}

public class Test extends SuperClass{
      public int doIt(String str, Integer... data){
            String signature = "(String, Integer[])";
            System.out.println("Overridden: " + str + " " +signature);
            return 0;
      }

      public static void main(String... args){
            SuperClass sb = new Test();
            sb.doIt("hello", 3);
      }
}

A. Overridden: hello (String, Integer[])

B. hello (String, Integer[])

C. Compilation fails

D. None of these

Answer: Option C

Solution(By Examveda Team)

Exceptions must be caught or declared to be thrown.


This Question Belongs to Java Program >> Exceptions

Join The Discussion

Comments ( 3 )

  1. Sachin Sude
    Sachin Sude :
    1 year ago

    In this code everything is correct. only main method didn't handle exception throws by do it method.
    because of that answer is compile time error.
    if we modify main() throws Exception then we will get an output that is :Overridden: hello (String, Integer[])

  2. Yogesh Sharma
    Yogesh Sharma :
    2 years ago

    As we are passing base class reference into child class object, so child class object will call base class "doIt" method.

  3. Latika Bajaj
    Latika Bajaj :
    7 years ago

    please provide the well detailed solution by which we can understand easily.

Related Questions on Exceptions