Menu
Topics Index
...
`

Creating method and Ternary operator


If you need explanation Read this topic

If you need Answer Take test on this topic

Creating method and Ternary operator
What will be the output of the following program?
public class GuessWhat
{
    public static void main(String args[])
    {
        int a[] = {11, 6, 17, 8, 9, 34, 14};
        System.out.println(guessWhat(a));
    }
    public static int guessWhat(int arr[])
    {
        int x = 0;
        for (int element : arr)
        x = x < element ? element : x;
        return x;
    }
}

A. Returns the index of the highest element in the array
B. Returns true/false if there are any elements that repeat in the array
C. Returns the highest element in the array
D. Returns the index of the lowest element in the array
E. Compilation Error or Runtime Error
Topic: For-each Loop In Core Java Programming

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.

Answer is C,
The highest element 34 is returned.

Posted by Amar Kumar Biradar    2014-02-21 04:59:28


Answer is :C it returns the highest element of the array elements...here the elements are passed throw the for each loop and then..int that       for-each loop(collection for loop) the condition is  evaluated that is bigger element in each iteration...so at the end the x value is stored the bigest element among the all elements in the array...so we get the 34 as output..

Posted by Sapparapu Pradeep Kumar    2014-02-21 06:25:26


Congratulations Amar Kumar Biradar. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-02-24 04:30:29


Thanks Merit Campus ;)
The amount is added to my account :)

Posted by Amar Kumar Biradar    2014-02-24 05:08:14


© meritcampus 2019

All Rights Reserved.

Open In App