Open In App

Output of Java program | Set 27

Improve
Improve
Like Article
Like
Save
Share
Report

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.



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