Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Output of Java program | Set 27

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Ques1. What is the output of the following?




import java.util.*;
  
public class Test {
public static void main(String[] args)
    {
        int[] x = new int[3];
        System.out.println("x[0] is " + x[0]);
    }
}


Options :
A. The program has a compile error because the size of the array wasn’t specified when declaring the array.
B. The program has a runtime error because the array elements are not initialized.
C. The program runs fine and displays x[0] is 0.
D. The program has a runtime error because the array element x[0] is not defined.

Answer : C

Explanation : Program is syntactically correct, so no error. In java, if the array is not initialized at the time of declaration and creation then all the elements of the array are initialized to 0 by default.

Ques2. What is the output of the following?




import java.util.*;
  
public class Test {
public static void main(String[] args)
    {
        int[] x = { 120, 200, 016 };
        for (int i = 0; i < x.length; i++)
            System.out.print(x[i] + " ");
    }
}


Options :
A. 120 200 16
B. 120 200 14
C. 120 200 016
D. 016 is a compile error. It should be written as 16.

Answer : B

Explanation : 016 is an octal number. The prefix 0 indicates that a number is in octal and in octal 16 is equal to 14.

Ques3. What is the output of the following?




import java.util.*;
  
public class Test {
public static void main(String args[])
    {
        String s1 = "java";
        String s2 = "java";
        System.out.println(s1.equals(s2));
        System.out.println(s1 == s2);
    }
}


Options :
A. false true
B. false false
C. true false
D. true true

Answer : D

Explanation : Both == and equals() are same things and evaluate to true/false.

Ques4. What is the output of the following?




import java.util.*;
  
public class Test {
public static void main(String args[])
    {
        String S1 = "S1 =" + "123" + "456";
        String S2 = "S2 =" + (123 + 456);
        System.out.println(S1);
        System.out.println(S2);
    }
}


Options :
A. S1=123456, S2=579
B. S1=123456, S2=123456
C. S1=579, S2=579
D. None of This

Answer : A

Explanation : If a number is quoted in “” then it becomes a string, not a number any more. So in S1 it is concatenated as string and in S2 as numeric values.

Ques5. What is the output of the following?




import java.util.*;
public class Test {
public static void main(String[] args)
    {
        int[] x = { 1, 2, 3, 4 };
        int[] y = x;
  
        x = new int[2];
  
        for (int i = 0; i < x.length; i++)
            System.out.print(y[i] + " ");
    }
}


Options :
A. 1 2 3 4
B. 0 0 0 0
C. 1 2
D. 0 0

Answer : C

Explanation : The length of x array is 2. So the loop will execute from i=0 to i=1.

This article is contributed by Rishabh Jain. 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.


My Personal Notes arrow_drop_up
Last Updated : 10 Aug, 2017
Like Article
Save Article
Similar Reads