Question Index

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'));
class Student implements Comparable<Student> {
    String name; int rollNumber; char section;
    public Student(String name, int rollNumber, char section) { = name; this.rollNumber = rollNumber;
        this.section = section;
    public int compareTo(Student o) {
        return name.compareToIgnoreCase(;
    public String toString() { return name; }

[bhavya, karthik, Ram, sreeram, Yeshwanthi]
[sreeram, Yeshwanthi, karthik, bhavya]
[Ram, Yeshwanthi, bhavya, karthik, sreeram]
Some other output
Compilation Error or Runtime Error



Topic: Java Comparators

Read this topic
Take test on this topic

Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App