Output of Java Programs | Set 49
Find the output of below java program Q 1. What is the output of this program?
Java
public class Example { int x = 10 ; public static void main(String args[]) { Example obj; System.out.println(obj.x); } } |
Option A. 10 B. 0 C. Compile time error D. Run time error Output:
C. Compile time error
Explanation : In java, memory of object is created by ” new ” keyword .Here only reference variable is created .And reference variable is uninitialized so reference variable is blank.And we can’t use the blank variable without initialize. So it gives the compile time error. Q 2. Find the output of this anonymous class.
Java
interface I1 { String toString(); } class Example { public static void main(String args[]) { System.out.println( new I1() { String toString() { System.out.print("Example"); return ("A"); } }); } } |
Option A. A B. Example C. Compile time error D. ExampleA Output:
C. Compile time error
Explanation : In java, all methods of interface became public by default. And when we implement the interface then it is necessary to all methods are defined with public. If we are making an anonymous method and implements the interface then above rule is applied in anonymous class. So it gives compile time error. Q 3. What is the output of this program?
Java
class Example { public static void main(String args[]) { try { return ; } finally { System.out.println("Hello India"); } } } |
Option A. Hello India B. Code run with no output C. Compile time error D. Run time error Output:
A. Hello India
Explanation : In the case of try, catch, finally it is sure that finally block will run only few cases finally block will not run. Few cases are like “System.exit(0)” system call in try. Q 4. What is the output of this program?
Java
class Test { public static void main(String args[]) { try { int x = 5 / 0 ; } catch (Exception e) { System.out.print("Exception "); } catch (ArithmeticException e) { System.out.print("ArithmeticException "); } System.out.println("Last Line"); } } |
Option A. Exception B. ArithmeticException C. Exception Last Line D. ArithmeticException Last Line E. Compile time error Output:
E. Compile time error
Explanation : According to java, rule in case of try catch block we define the catch block derived class to super class and here ArithmeticException is the derived class of Exception class, and here is being violate the java rule, so it gives compile time error. Q 5.What is the output of this program?
Java
class String_Test { public static void main(String args[]) { String str1 = new String("Hello World"); String str2 = new String("Hello World"); if (str1 == str2) System.out.println("Hello England"); else System.out.println("Hello India"); } } |
Option A. Hello India B. Hello England C. Compiler time error D. Run time error Output:
A. Hello India
Explanation : If we use the new keyword then a new object created, if we use the double quote then only one object is created and all string reference point to this object. Here equal() check the string is equal or not and “==” check the reference point. This article is contributed by Amit Verma. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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.
Please Login to comment...