Output of Java programs | Set 29

Question 1. What is the output of the following question?

class Test1 {
public
    static void main(String[] args)
    {
        int String = 65;
        int Runnable = 97;

        System.out.print(String + " : " + Runnable);
    }
}

Option
A) Error
B) A : a
C) 65 : 97
D) None

Output: C

Explanation : We can use all predefined Java class name and interface name as identifiers.

Question 2. What is the output of the following question?

class Test2 {
public
    static void main(String[] args)
    {
        int if = 65;
        int else = 97;

        System.out.println(if + " : " + else);
    }
}

Option
A) Error
B) A : B
C) 65 : 97
D) None

Output: A

Explanation : We can’t use reserved words as identifiers.

Question 3. What is the output of the following question?

class Test3 {
public
    static void main(String[] args)
    {
        int x = 1;

        if (x) {
            System.out.print("GeeksForGeeks");
        } else {
            System.out.print("GFG");
        }
    }
}

Option
A) GeeksForGeeks
B) GFG
C) Error
D) None

Output: C

Explanation :In Java, Compiler gives error – Incompatible types : int can not be converted to boolean type.
But in C or C++ its a valid statement.

Question 4. What is the output of the following question?

class Test4 {
public
    static void main(String[] args)
    {
        double d1 = 123.456;
        double d2 = 12_3.4_5_6;
        double d3 = 12_3.4_56;

        System.out.println(d1);
        System.out.println(d2);
        System.out.println(d3);
    }
}

Option
A) Error
B) 123.456
12_3.4_5_6
12_3.4_56
C) 123.456
123.456
123.456
D) None

Output: C

Explanation : From (1.7v onwards)we can use ‘_'(under Score) Symbol between digits of numeric literals. See more at Java naming conventions.

Question 5. What is the output of the following question?

class Test5 {
public
    static void main(String[] args)
    {
        double d1 = _123 .456;
        double d2 = 12_3_.4_5_6;
        double d3 = 12_3.4_56_;

        System.out.println(d1);
        System.out.println(d2);
        System.out.println(d3);
    }
}

Option
A) Error
B) 123.456
12_3.4_5_6
12_3.4_56
C) 123.456
123.456
123.456
D) None

Output: A

Explanation : We can use the ‘_’ (under score) symbol only between the digits. if we are using anywhere else we will get compile time error – Illegal under score.

This article is contributed by Shivakant Jaiswal. 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:







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