• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests

Java Arrays

Question 1

C
class Test {
   public static void main(String args[]) {
     int arr[2];  
     System.out.println(arr[0]);
     System.out.println(arr[1]);
   }
}
  • 0
    0
  • garbage value
    garbage value
  • Compiler Error
  • 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\");
    }
}
  • Same
  • Not Same

Question 3

Consider the following C program which is supposed to compute the transpose of a given 4 x 4 matrix M. Note that, there is an X in the program which indicates some missing statements. Choose the correct option to replace X in the program. C
#include<stdio.h>
#define ROW 4
#define COL 4
int M[ROW][COL] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
main()
{
    int i, j, t;
    for (i = 0; i < 4; ++i)
    {
        X
    }
    for (1 = 0; i < 4; ++i)
        for (j = 0; j < 4; ++j)
            printf (\"%d\", M[i][j]);
}
 
A)
for(j = 0; j < 4; ++j){
     t = M[i][j];
     M[i][j] = M[j][i];
     M[j][i] = t;
}
 
B)
for(j = 0; j < 4; ++j){
     M[i][j] = t;
     t = M[j][i];
     M[j][i] = M[i][j];
}
 
C)
for(j = i; j < 4; ++j){
     t = M[i][j];
     M[i][j] = M[j][i];
     M[j][i] = t;
}
 
D)
for(j = i; j < 4; ++j){
     M[i][j] = t;
     t = M[j][i];
     M[j][i] = M[i][j];
}
 
  • A
  • B
  • C
  • D

Question 4

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\");
    }
}
  • Same
  • Not Same

Question 5

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

Question 6

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\");
    }
}
  • Same
  • Not same

Question 7

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]);              
       }
    }
}
  • 10 20 30 40 50
  • Compiler Error
  • 10 20 30 40

Question 8

Which of the following is FALSE about arrays in Java?

  • A java array is always an object

  • Length of array can be changed after creation of array

  • Arrays in Java are always allocated on heap

Question 9

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();
        }
    }
} 
  • Compiler Error
  •  0
     1 2
     3 4 5
     6 7 8 9 
  •  0
     0 0
     0 0 0
     0 0 0 0 
  •  9
     7 8
     4 5 6
     0 1 2 3 

There are 9 questions to complete.

Last Updated :
Take a part in the ongoing discussion