Menu
Topics Index
...
`

GPREC Students2436


If you need explanation Read this topic

If you need Answer Take test on this topic

GPREC Students2436
What will be the output of the following program?
import java.util.*;
public class StudentRanks {
    public static void main(String[] args) {
        Set<Student> students = new TreeSet<Student>();
        students.add(new Student("sreeram", 5, 'C'));
        students.add(new Student("karthik", 1, 'A'));
        students.add(new Student("Ram", 1, 'B'));
        students.add(new Student("Yeshwanthi", 5, 'D'));
        students.add(new Student("bhavya", 2, 'E'));
        System.out.println(students);
    }
}
class Student implements Comparable<Student> {
    String name; int rollNumber; char section;
    public Student(String name, int rollNumber, char section) {
        this.name = name; this.rollNumber = rollNumber;
        this.section = section;
    }
    public int compareTo(Student o) {
        return name.compareTo(o.name);
    }
    public String toString() { return name; }
}

A. [bhavya, karthik, Ram, sreeram, Yeshwanthi]
B. [sreeram, Yeshwanthi, karthik, bhavya]
C. [Ram, Yeshwanthi, bhavya, karthik, sreeram]
D. Some other output
E. Compilation Error or Runtime Error
Topic: Java Comparators

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.

Ans is a...after comparing it returns names in alphabetical order

Posted by Lavanya Kategaru    2014-01-31 09:05:23


B is the ans.comparing the names and its roll no.we are printing accrdinglu

Posted by Shabaaz Shaikz    2014-01-31 09:05:37


Answered is C..  After comparing it returns names in alphabetical order

Posted by Ravi Theja Tallam    2014-01-31 10:21:07


D

Posted by Kick Bhasha    2014-01-31 11:55:10


Ans is A   Since the Treeset using Comparable interface which sort the TreeSet in alphabetical order the logic of which is wrote in compareTo(..) method and the s.o.p uses toString to display the content of students.

Posted by Mohd Khayam Uddin    2014-01-31 12:43:12


ans is A

Posted by Kailash Singh    2014-01-31 16:14:10


Congratulations Ravi Theja Tallam. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-02-03 04:35:55


© meritcampus 2019

All Rights Reserved.

Open In App