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 = {{1, 2, 3}, {}, {1, 2,3, 4, 5}};
  2. int[][] array2 = new array() {{1, 2, 3}, {}, {1, 2,3, 4, 5}};
  3. int[][] array3 = {1, 2, 3}, {0}, {1, 2,3, 4, 5};
  4. int[][] array4 = new int[2][];

Q4-4.

Which of the following statements are correct?

  1. The following code executes without an error or exception:

    ArrayList<Long> lst = new ArrayList<>();
    lst.add(10);
  2. Because ArrayList stores only objects, you can’t pass an element of an ArrayList to a switch construct.
  3. Calling clear() or remove() on an ArrayList will remove all its elements.
  4. If you frequently add elements to an ArrayList, specifying a larger capacity will improve the code efficiency.
  5. Calling the method clone() on an ArrayList creates its shallow copy; that is, it doesn’t clone the individual list elements.

Q4-5.

Which of the following statements are correct?

  1. An ArrayList offers a resizable array, which is easily managed using the methods it provides. You can add and remove elements from an ArrayList.
  2. Values stored by an ArrayList can be modified.
  3. You can iterate through elements of an ArrayList using a for loop, Iterator, or ListIterator.
  4. An ArrayList requires you to specify the total number of elements before you can store any elements in it.
  5. An ArrayList can store any type of object.

Q4-6.

What is the output of the following code?

import java.util.*;                                             // line 1
class EJavaGuruArrayList {                                      // line 2
    public static void main(String args[]) {                    // line 3
        ArrayList<String> ejg = new ArrayList<>();              // line 4
        ejg.add("One");                                         // line 5
        ejg.add("Two");                                         // line 6
        System.out.println(ejg.contains(new String("One")));    // line 7
        System.out.println(ejg.indexOf("Two"));                 // line 8
        ejg.clear();                                            // line 9
        System.out.println(ejg);                                // line 10

        System.out.println(ejg.get(1));                         // line 11
    }                                                           // line 12
}                                                               // line 13

  1. Line 7 prints true.
  2. Line 7 prints false.
  3. Line 8 prints -1.
  4. Line 8 prints 1.
  5. Line 9 removes all elements of the list ejg.
  6. Line 9 sets the list ejg to null.
  7. Line 10 prints null.
  8. Line 10 prints [].
  9. Line 10 prints a value similar to ArrayList@16356.
  10. Line 11 throws an exception.
  11. Line 11 prints null.

Q4-7.

What is the output of the following code?

class EJavaGuruString {
    public static void main(String args[]) {
        String ejg1 = new String("E Java");
        String ejg2 = new String("E Java");
        String ejg3 = "E Java";
        String ejg4 = "E Java";
        do
            System.out.println(ejg1.equals(ejg2));
        while (ejg3 == ejg4);
    }
}

  1. true printed once
  2. false printed once
  3. true printed in an infinite loop
  4. false printed in an infinite loop

Q4-8.

What is the output of the following code?

class EJavaGuruString2 {
    public static void main(String args[]) {
        String ejg = "game".replace('a', 'Z').trim().concat("Aa");
        ejg.substring(0, 2);
        System.out.println(ejg);
    }
}

  1. gZmeAZ
  2. gZmeAa
  3. gZm
  4. gZ
  5. game

Q4-9.

What is the output of the following code?

class EJavaGuruString2 {
    public static void main(String args[]) {
        String ejg = "game";
        ejg.replace('a', 'Z').trim().concat("Aa");
        ejg.substring(0, 2);
        System.out.println(ejg);
    }
}

  1. gZmeAZ
  2. gZmeAa
  3. gZm
  4. gZ
  5. game

Q4-10.

What is the output of the following code?

class EJavaGuruStringBuilder {
    public static void main(String args[]) {
        StringBuilder ejg = new StringBuilder(10 + 2 + "SUN" + 4 + 5);
        ejg.append(ejg.delete(3, 6));
        System.out.println(ejg);
    }
}

  1. 12S512S5
  2. 12S12S
  3. 1025102S
  4. Runtime exception

Q4-11.

What is the output of the following code?

class EJavaGuruStringBuilder2 {
    public static void main(String args[]) {
        StringBuilder sb1 = new StringBuilder("123456");
        sb1.subSequence(2, 4);
        sb1.deleteCharAt(3);
        sb1.reverse();
        System.out.println(sb1);
    }
}

  1. 521
  2. Runtime exception
  3. 65321
  4. 65431

Q4-12.

What is the output of the following code?

String printDate = LocalDate.parse("2057-08-11")
                      .format(DateTimeFormatter.ISO_DATE_TIME);
System.out.println(printDate);

  1. August 11, 2057T00:00
  2. Saturday Aug 11,2057T00:00
  3. 08-11-2057T00:00:00
  4. Compilation error
  5. Runtime exception

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.109.234