Open In App

Output of Java Programs | Set 49

Improve
Improve
Like Article
Like
Save
Share
Report

Find the output of below java program Q 1. 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. 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




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. 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 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. 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





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.



Last Updated : 06 Sep, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads