4.9. Sample exam questions

Q4-1.

What is the output of the following code?

class EJavaGuruArray {
    public static void main(String args[]) {
        int[] arr = new int[5];
        byte b = 4; char c = 'c'; long longVar = 10;
        arr[0] = b;
        arr[1] = c;
        arr[3] = longVar;
        System.out.println(arr[0] + arr[1] + arr[2] + arr[3]);
    }
}
  1. 4c010
  2. 4c10
  3. 113
  4. 103
  5. Compilation error

Q4-2.

What is the output of the following code?

class EJavaGuruArray2 {
    public static void main(String args[]) {
        int[] arr1;
        int[] arr2 = new int[3];
        char[] arr3 = {'a', 'b'};
        arr1 = arr2;
        arr1 = arr3;
        System.out.println(arr1[0] + ":" + arr1[1]);
    }
}
  1. 0:0
  2. a:b
  3. 0:b
  4. a:0
  5. Compilation error

Q4-3.

Which of the following are valid lines of code to define a multidimensional int array?

  1. int[][] array1 ...

Get OCA Java SE 8 Programmer I Certification Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.