Menu
Topics Index
...
`

Print Array Values


If you need explanation Read this topic

If you need Answer Take test on this topic

Print Array Values
What will be the output of the following program?
import java.util.*;
public class Google {
    public static void main(String[] args) {
        int[] array = new int[10];
        for (int i = 0; i < 10; i++) {
            array[i] = 2 * i;
        }
        dispay(array);
        Arrays.fill(array, 7, 9, 22);
        Arrays.sort(array);
        int foundAt = Arrays.binarySearch(array, 0, 5, 3);
        System.out.println(foundAt);
        int temp = 0;
        int tempArray[] = new int[array.length / 2 + 1];
        for (int i = 0; i < array.length; i = i + 2) {
            tempArray[temp++] = array[i];
        }
        Arrays.sort(array);
        for (int i = 0; i < array.length; i = i + 2) {
            array[i] = tempArray[--temp];
        }
        dispay(array);
    }
    private static void dispay(int[] array) {
        for (int in : array) {
            System.out.print(in + " ");
        }
        System.out.println();
    }
}

A. 0 2 4 6 8 10 12 14 16 18 
-3
22 18 16 14 12 10 8 6 4 2
B. 0 2 4 6 8 10 12 14 16 18 
-3
22 2 12 6 8 10 4 14 18 0
C. 0 2 4 6 8 10 12 14 16 18 
-2
22 2 12 6 8 10 4 18 0 22
D. Some other output
E. Compilation Error or Runtime Error
Topic: java.util.Arrays - Class Arrays In Collection Framework

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

D.some other output occurs. such as
0 2 4 6 8 10 12 14 16 18
-3
0 2 0 6 0 10 0 18 0 22  from 0-18 are inserted in aray using for loop .the its sorted and using Bsearch values for repective numberes are found ie; their locations

Posted by Shabaaz Shaikz    2013-12-16 12:59:50


ans msut be D other output bcoz the output must be
0 2 4 6 8 10 12 14 16 18
-3
22 2 12 6 8 10 4 18 0 22
firstly array is filled wth multiples of 2 and is printed and then sorted and then value 3 is searched in the array using binary search function..this function returns index if elements is found succesfully, if not found in case of sorted array it returns (-insertionpoint-1) so in dis case it returns -2-1 i.e -3 and two loops are executed which forms array as shown in third line of output

Posted by Pramod Jain    2013-12-16 13:16:50


d is the answer . we get answer similar to c where the second line value is -3. here we don't get any index for the binary search  dats y it returns negative value. post that we take some vals to temp array and then override the "array" with the temp array vals

Posted by Karteek Paruchuri    2013-12-16 13:17:38


Congratulations Shabaaz Shaikz. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2013-12-17 03:38:08


TQ>:-)

Posted by Shabaaz Shaikz    2013-12-17 08:26:02


© meritcampus 2019

All Rights Reserved.

Open In App