Output of Java Program | Set 11 - GeeksforGeeks

09 Jun, 2020
Predict the output of following Java programs:
Question 1 :
public class Base{    private int data;      public Base()    {        data = 5;    }      public int getData()    {        return this.data;    }}  class Derived extends Base{    private int data;    public Derived()    {        data = 6;    }    private int getData()    {        return data;    }      public static void main(String[] args)    {        Derived myData = new Derived();        System.out.println(myData.getData());    }}
a) 6b) 5c) Compile time errord) Run time error
Answer (c)Explanation: When overriding a method of superclass, the method declaration in subclass cannot be more restrictive than that declared in the superclass.
Question 2 :
public class Test{    private int data = 5;      public int getData()    {        return this.data;    }    public int getData(int value)    {        return (data+1);    }    public int getData(int... value)    {        return  (data+2);    }      public static void main(String[] args)    {        Test temp = new Test();        System.out.println(temp.getData(7, 8, 12));    }}
a) Either Compile time or Runtime errorb) 8c) 10d) 7
Answer (d)Explanation : (int... values) is passed as parameter to a method when you are not aware of the number of input parameter but know that the type of parameter(in this case it is int). When a new object is made in the main method, variable data is initialized to 5. A call to getData() method with attributes (7, 8 ,12), makes a call to the third getData() method, which increments the value of data variable by 2 and return 7.
Question 3:
public class Base{    private int multiplier(int data)    {        return data*5;    }}  class Derived extends Base{    private static int data;    public Derived()    {        data = 25;    }    public static void main(String[] args)    {        Base temp = new Derived();        System.out.println(temp.multiplier(data));    }}
a) 125b) 25c) Runtime errord) Compile time error
Answer (d)Explanation: Since the method multiplier is marked as private, it isn’t inherited and therefore is not visible to the Derived.
Question 4:Which of the following is FALSE about finally block?a) For each try block, there can be only 1 finally block.b) finally block will not be executed if program exits by calling System.exit();c) If an exception is not handled in the catch statement, before terminating the program, JVM executes the finally blockd) finally block contains important code segment and so the code inside finally block is executed with/without the presence of try block in java code.
Answer (d)Explanation:Statement (d) is false because finally blocks can exist only if it succeeds try or a catch block. Using a finally block without try block would give a compile time error.
Question 5:
import java.io.IOException;import java.util.EmptyStackException;  public class newclass{    public static void main(String[] args)    {        try        {            System.out.printf("%d", 1);            throw(new Exception());        }        catch(IOException e)        {            System.out.printf("%d", 2);        }        catch(EmptyStackException e)        {            System.out.printf("%d", 3);        }        catch(Exception e)        {            System.out.printf("%d", 4);        }        finally        {            System.out.printf("%d", 5);        }    }}
a) 12345b) 15c) 135d) 145
Answer (d)Explanation: The catch statements are written in the order: more specific to more general. In the code above, a new exception of type Exception is thrown. First the code jumps to first catch block to look for exception handler. But since the IOException is notof the same type it is moves down to second catch block and finally to the third, wherethe exception is caught and 4 is printed. Therefore, the answer is 145, as the orderof execution in terms of blocks is: try->catch->finally.
Question 6:
public class javaclass{    static    {        System.out.printf("%d", 1);    }    static    {        System.out.printf("%d", 2);    }    static    {        System.out.printf("%d", 3);    }    private static int myMethod()    {        return 4;    }    private int function()    {        return 5;    }      public static void main(String[] args)    {        System.out.printf("%d", (new javaclass()).function() + myMethod());    }}
a) 123b) 45c) 12345d) 1239
Answer (d)Explanation:static blocks in Java are executed even before the call to main is made by the compiler. In the main method, a new object of javaclass is made and its function() method is called which return 5 and the static method myMethod() returns 4 i.e., 4+5 = 9. Therefore, the output of the program is 1239, because 123 is printed on the console even before main method executes and main method on execution returns 9.
This article is contributed by Mayank. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
user_blzg
Java-Output
Java
Program Output
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Interfaces in Java
ArrayList in Java
Stack Class in Java
Singleton Class in Java
Arrow operator -> in C/C++ with Examples
Output of Java Program | Set 1
delete keyword in C++
Output of C Programs | Set 1
Different ways to copy a string in C/C++