• Courses
  • Tutorials
  • DSA
  • Data Science
  • Web Tech
Switch to Dark Mode

Java Arrays

Last Updated : Mar 22, 2024
Discuss
Comments

Question 1

C
class Test {
   public static void main(String args[]) {
     int arr[2];  
     System.out.println(arr[0]);
     System.out.println(arr[1]);
   }
}
  • A
    0
    0
  • B
    garbage value
    garbage value
  • C
    Compiler Error
  • D
    Exception

Question 2

Output of following Java program?

Java
import java.util.Arrays;
class Test
{
    public static void main (String[] args) 
    {
        int arr1[] = {1, 2, 3};
        int arr2[] = {1, 2, 3};
        if (Arrays.equals(arr1, arr2))
            System.out.println("Same");
        else
            System.out.println("Not same");
    }
}
  • A

    Same

  • B

    Not Same

Question 3

Output of following Java program?

C
class Test
{
    public static void main (String[] args) 
    {
        int arr1[] = {1, 2, 3};
        int arr2[] = {1, 2, 3};
        if (arr1 == arr2) 
            System.out.println("Same");
        else
            System.out.println("Not same");
    }
}
  • A

    Same

  • B

    Not Same

Question 4

Java
class Test {
   public static void main(String args[]) {
     int arr[] = new int[2];  
     System.out.println(arr[0]);
     System.out.println(arr[1]);
   }
}
  • A
    0
    0
  • B
    garbage value
    garbage value
  • C

    Compiler Error

  • D

    Exception

Question 5

Java
class Test
{
    public static void main (String[] args) 
    {
        int arr1[] = {1, 2, 3};
        int arr2[] = {1, 2, 3};
        if (arr1.equals(arr2))
            System.out.println("Same");
        else
            System.out.println("Not same");
    }
}
  • A

    Same

  • B

    Not same

Question 6

Predict the output?

Java
// file name: Main.java
public class Main {
    public static void main(String args[]) {
       int arr[] = {10, 20, 30, 40, 50};
       for(int i=0; i < arr.length; i++)
       {
             System.out.print(" " + arr[i]);              
       }
    }
}
  • A

    10 20 30 40 50

  • B

    Compiler Error

  • C

    10 20 30 40

Question 7

Which of the following is FALSE about arrays in Java?

  • A

    A java array is always an object

  • B

    Length of array can be changed after creation of array

  • C

    Arrays in Java are always allocated on heap

Question 8

C
public class Main {
    public static void main(String args[]) {
        int arr[][] = new int[4][];
        arr[0] = new int[1];
        arr[1] = new int[2];
        arr[2] = new int[3];
        arr[3] = new int[4];
 
        int i, j, k = 0;
        for (i = 0; i < 4; i++) {
            for (j = 0; j < i + 1; j++) {
                arr[i][j] = k;
                k++;
            }
        }
        for (i = 0; i < 4; i++) {
            for (j = 0; j < i + 1; j++) {
                System.out.print(" " + arr[i][j]);
                k++;
            }
            System.out.println();
        }
    }
} 
  • A
    Compiler Error
  • B
     0
     1 2
     3 4 5
     6 7 8 9 
  • C
     0
     0 0
     0 0 0
     0 0 0 0 
  • D
     9
     7 8
     4 5 6
     0 1 2 3 

Master JAVA and also get 90% fee refund on completing 90% course in 90 days! Take the Three 90 Challenge today.

After successfully processing refunds worth over INR 5 Cr, GeeksforGeeks is back with the Three 90 challenge and this is your chance to upskill and get 90% refund. What more motivation do you need? Start the challenge right away!

There are 8 questions to complete.

Take a part in the ongoing discussion