Output of Java Program | Set 11
Predict the output of following Java programs:
Question 1 :
c) Compile time error
d) Run time error
Explanation: When overriding a method of superclass, the method declaration in subclass cannot be more restrictive than that declared in the superclass.
Question 2 :
a) Either Compile time or Runtime error
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.
c) Runtime error
d) Compile time error
Explanation: Since the method multiplier is marked as private, it isn’t inherited and therefore is not visible to the Derived.
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 block
d) 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.
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.
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 not
of the same type it is moves down to second catch block and finally to the third, where
the exception is caught and 4 is printed. Therefore, the answer is 145, as the order
of execution in terms of blocks is: try->catch->finally.
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 email@example.com. 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.