Output of Java program | Set 27 - GeeksforGeeks

10 Aug, 2017
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 16B. 120 200 14C. 120 200 016D. 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 trueB. false falseC. true falseD. 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=579B. S1=123456, S2=123456C. S1=579, S2=579D. 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 4B. 0 0 0 0C. 1 2D. 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.
Java-Output
Program Output
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Output of Java program | Set 18 (Overriding)
Output of C++ programs | Set 34 (File Handling)
Different ways to copy a string in C/C++
Output of Python Program | Set 1
Output of C++ programs | Set 50
Runtime Errors
Output of C Programs | Set 2
C++ Programming Multiple Choice Questions
Output of Java Program | Set 20 (Inheritance)
Output of C++ Program | Set 1