Menu
Topics Index
...
`

Minimum and Maximum in Numbers


If you need explanation Read this topic

If you need Answer Take test on this topic

Minimum and Maximum in Numbers
What will be the output of the following program?
import java.util.*;

public class MinMax
{
    public static void main(String[] args)
    {
        List<String> number = Arrays.asList("1 2 3 8 8 7 4 5 6 9".split(" "));
        List<String> string = Arrays.asList("one Two three Four Seven six one three Four".split(" "));
              
        System.out.println("In number maximum is : " + Collections.max(number));
        System.out.println("In number minimum is : " + Collections.min(number));
              
        System.out.println("In string maximum is : " + Collections.max(string));
        System.out.println("In string minimum is : " + Collections.min(string));
    }
}

A. In number maximum is : 3
In number minimum is : 4
In string maximum is : three
In string minimum is : Four
B. In number maximum is : 9
In number minimum is : 1
In string maximum is : Seven
In string minimum is : one
C. In number maximum is : 9
In number minimum is : 1
In string maximum is : three
In string minimum is : Four
D. In number maximum is : 8
In number minimum is : 1
In string maximum is : one
In string minimum is : Two
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.

Answer is C....Arrays.aslist will retrun a list backed by the specific array....Collections.max method    
Returns the maximum element of the given collection, according to the natural  ordering of its elements. All elements in the collection must implement the  Comparable interface...in case of a numbermax is 9 and min is 1 in this program....in case of a string,max and min as three and four by the natural ordering of the elements

Posted by Shashanka Mogaliraju    2015-03-03 13:31:57


ANS is C

Collections.max: returns the maximum elements of ha array.
Collections.max: returns the maximum elements of ha array.

Posted by Mânïshå Mùlchåndânï    2015-03-03 14:16:12


Ans is C..here we have the concept of the Arrays and Collections, here we create the String literal and invoke the split() on that String than it will return the array of Strings than by using the asList() we are converting the Array of Strings into Array of List, twice we are doing the same..
Execution:

first we are finding the max and min element in the list1, here list1 stores the objects of the Strings, so it will compare the Ascci values of it so max element is 9 and min element is 1, even if u have the 10,11 also it will give 9 only Bcz it will check it will check 1st char
In the case 2 here we are comparing the strings it will compare the 1st char so Four is the mina and Three is the Max  (According to the Ascci Difference)

Posted by Uday Kumar    2015-03-04 02:53:35


This dose is now closed and the winners are Shashanka Mogaliraju,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Mânïshå Mùlchåndânï for the 'Popular Comment'. The 'lucky liker' is Gopinath Manchikanti. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-03-04 04:04:34


© meritcampus 2019

All Rights Reserved.

Open In App