10 Java Interview Sample Coding Questions On Arrays

10 Java Interview Sample Coding Questions On Arrays

1) What happens when you compile and run this program?
public class ArraysInJava {
      public static void main(String[] args) {
            int[] i = new int[0];

            System.out.println(i[0]);
      }
}
Ans : You will get ArrayIndexOutOfBoundsException at run time.
2)  What will be the output of this program?
public class ArraysInJava {
      public static void main(String[] args) {
            int[] a = new int[3];

            a[1] = 50;

            Object o = a;

            int[] b = (int[]) o;

            b[1] = 100;

            System.out.println(a[1]);

            ((int[]) o)[1] = 500;

            System.out.println(a[1]);
      }
}

Ans :
100
500
3) What will be the outcome of the following program?
public class ArraysInJava {
      static final int[] a;

      static {
            a = new int[] { 1, 2, 3 };
      }

      public static void main(String[] args) {
            a = new int[5];
      }
}

Ans : Compile time error.
4)What will be the outcome of this program?
public class ArraysInJava {
      public static void main(String[] args) {
            int[] a = { 1, 2, 3, 4, 5, 8 };

            System.out.println(a[-1]);
      }
}

Ans : ArrayIndexOutOfBoundsException at run time.
5) What happens when you compile and run the following program?
public class ArraysInJava {
      public static void main(String[] args) {
            int[][] a = { { 1, 2, }, { 3, 4 } };

            int[] b = (int[]) a[1];

            Object o1 = a;

            int[][] a2 = (int[][]) o1;

            int[] b2 = (int[]) o1;

            System.out.println(b[1]);
      }
}

Ans : Line 13 throws ClassCastException at run time.
6) What will be the output of this program?
public class ArraysInJava {
      static void methodOne(int[] a) {
            int[] b = new int[5];

            a = b;

            System.out.print(a.length);

            System.out.print(b.length);
      }

      public static void main(String[] args) {
            int[] a = new int[10];

            methodOne(a);

            System.out.print(a.length);
      }
}

Ans : 5510
7) Will this program compiles and runs successfully?
public class ArraysInJava {
      public static void main(String[] args) {
            int[] a = { 1 };

            int[] b[] = { { 1 } };

            int[][] c[] = { { { 1 } } };

            int[][] d[][] = { { { { 1 } } } };
      }
}

Ans : Yes.
8) What will be the output of this program?
public class ArraysInJava {
      public static void main(String[] args) {
            String[][][][] colors = { { { { "RED", "GREEN", "BLUE" },

                        { "GREEN", "RED", "BLUE" } },
                        { { "ORANGE", "GREEN", "WHITE" },

                                    { "BLACK", "INDIGO", "BLUE" } } },
                        { { { "SKY BLUE", "ALMOND", "AQUA" },

                                    { "APPLE GREEN", "PINK", "BLUE GREEN" } },
                                    { { "VIOLET", "BRASS", "GREY" },

                                                { "BROWN", "INDIGO", "CHERRY" } } } };

            System.out.println(colors[1][0][1][0]);

            System.out.println(colors[0][1][0][1]);

            System.out.println(colors[0][0][0][2]);

            System.out.println(colors[1][1][1][2]);

            System.out.println(colors[0][0][0][0]);

            System.out.println(colors[1][1][1][1]);
      }
}

Ans :
APPLE GREEN
GREEN
BLUE
CHERRY
RED
INDIGO
9) Does below program compile successfully?
class A {
      int i = 10;
}

class B extends A {
      int j = 20;
}

class C extends B {
      int k = 30;
}

class D extends C {
      int m = 40;
}

public class ArraysInJava {
      public static void main(String[] args) {
            A[] a = { new A(), new B(), new C(), new D() };

            System.out.println(a[3].i);

            System.out.println(a[2].j);

            System.out.println(a[1].k);

            System.out.println(a[0].m);
      }
}

Ans : No, It gives compile time error
10) What will be the output of this program?
public class ArraysInJava {
      static Double[] methodOne(Double[] D) {
            D[1] = 36.25;

            return methodTwo(D);
      }

      static Double[] methodTwo(Double[] D) {
            D[1] = 62.36;

            return methodThree(D);
      }

      static Double[] methodThree(Double[] D) {
            D[1] = 93.58;

            return D;
      }

      public static void main(String[] args) {
            Double[] D = { 10.55, 25.36, 58.29, 74.32, 32.21 };

            D = methodOne(D);

            System.out.println(D[1]);
      }
}

Ans : 93.58



EmoticonEmoticon