Output of Java Programs | Set 50

Q 1.What is the output of this program?

class Test {
public final int a;
} class Example {
public static void main(String args[])
    {
        Test obj = new Test();
        System.out.println(obj.a);
    }
}

Option
A. 0
B. Garbage value
C. Compile time error : variable is not initialized
D. Run time error : a is the blank variable
Output:

C. Compile time error : variable is not initialized

Explanation : In java, final variable become 0 by default. Only three ways to initialize the final variable 1. using constructor 2. initialization block 3. At the time of variable declaration.

Q 2.What is the output of this program?

class Example {
private int x;
public static void main(String args[])
    {
        Example obj = new Example();
    }
public void Example(int x)
    {
        System.out.println(x);
    }
}

Option
A. 0
B. Garbage value
C. Compile time error
D. No output : Blank Screen
Output:

D. No output : Blank Screen

Explanation : There are no constructor in this program . This is a method just like name of class but it is not constructor because constructor does not be return type. So in this program output is blank screen.

Q 3.What is the output of this program?

class Example {
private int x;
public static void main(String args[])
    {
        Example obj = new Example(5);
    }
public Example(int x)
    {
        System.out.println("x = " + x);
    }
public void Example(int x)
    {
        System.out.println(x);
    }
}

Option
A. x = 5
B. 5
C. Compile time error : ambiguous call of Example(int)
D. Run time error
Output:

A. x = 5

Explanation : In this program “public Example(int)” is the constructor and “public void Example(int)” is the method so compiler will not be confused. And constructor will be automatically called at the time of object creation.

Q 4.What is the output of this program?


class Test {
public static void main(String args[])
    {
        String str1 = new String("Hello World");
        String str2 = new String("Hello World");

        String str3 = "Hello World";
        String str4 = "Hello World";

        int a = 0, b = 0, c = 0;

        if (str3 == str4)
            a = 1;
        else
            a = 2;

        if (str1.equals(str3))
            b = 1;
        else
            b = 2;

        if (str1 == str4)
            c = 1;
        else
            c = 2;
        System.out.println("a= " + a + " b= " + b + " c= " + c);
    }
}

Option
A. a=2 b=1 c=2
B. a=2 b=2 c=2
C. a=1 b=2 c=1
D. a=1 b=1 c=2
Output:

D. a=1 b=1 c=2

Explanation : When we make the object with the help of new keyword then a new memory created and reference variable contain the memory location. Here two times memory is created with same string but we are comparing objects not string, so object are pointing to different memory locations so they are not equal.

Q 5. How many object created in the above example?
Option
A. 1
B. 2
C. 3
D. 4
Output:

C. 3

This article is contributed by Amit Verma . 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.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:



0 Average Difficulty : 0/5.0
No votes yet.










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.