Menu
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'));
        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.substring(rollNumber).compareToIgnoreCase(o.name.substring(o.rollNumber));
    }
    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

Doubts

Problems

Topic: Java Comparators

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App