Open In App

Output of Java programs | Set 29

Improve
Improve
Like Article
Like
Save
Share
Report

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.



Last Updated : 31 Aug, 2017
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads