Arrays


  1. What is the output of this program?

    public class Result
    {
    public static void main(String args[])
    {
    int a[] = {5, 6, 7, 9, 11};
    for ( int k = 0; k < a.length - 1; ++k)
    System.out.print(a[k] + " ");
    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: B

    a.length() is 5, so the loop is executed for 4 times.
    output: 5 6 7 9


  1. What is the output of this program?
    import java.util.*;
    public class ArrayExample
    {
    public static void main(String args[])
    {
    int arr[] = new int [10];
    for (int i = 10; i > 0; i--)
    arr[10 - i] = i;
    Arrays.sort(arr);
    System.out.print(Arrays.binarySearch(arr, 6));
    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: E

    5



  1. What is the output of this program?
    import java.util.*;
    public class ArrayExample
    {
    public static void main(String args[])
    {
    int arr[] = new int [5];
    for (int k = 5; k > 0; k--)
    arr[5 - k] = k;
    Arrays.sort(arr);
    for (int k = 0; k < 5; ++k)
    System.out.print(arr[k]);;
    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    Arrays.sort(arr) method sorts the array into 1,2,3,4,5.


  1. What is the output of this program?
    import java.util.*;
    public class ArraylistExample
    {
    public static void main(String args[])
    {
    ArrayList object0 = new ArrayList();
    ArrayList object1 = new ArrayList();
    object0.add("I");
    object0.add("P");
    object1.add("I");
    object1.add(1, "P");
    System.out.println(object0.equals(object1));
    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: E

    object0 and object1 are an object of class ArrayList hence it is a dynamic array which can increase and decrease its size. object.add(“N”) adds to the array element N and object.add(1,”N”) adds element N at index position 1 in the list, Both the objects object0 and object1 contain same elements i:e I & P thus object0.equals(object1) method returns false.



  1. Which of these methods can be used to search an element in a list?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: B

    binaryserach() method uses binary search to find a specified value. This method must be applied to sorted arrays.