Menu
Question Index
...

What will be the output of following program?

import java.util.*;

public class StudentsPriority {
    public static void main(String[] args) {
        PriorityQueue<Student1> pq = new PriorityQueue<Student1>();
        pq.add(new Student1(15, "Pramod"));
        pq.add(new Student1(5, "Soumya"));
        pq.add(new Student1(2, "Santosh"));
        pq.add(new Student1(13, "Nyamath"));
        pq.peek().print();
        pq.element().print();
        pq.poll().print();
        pq.element().print();
    }            
}

class Student1 implements Comparable<Student1> {
    int id;
    String name;
    public Student1(int id, String name) {
        this.id = id;
        this.name = name;
    }
    public int compareTo(Student1 other) {
        return id - other.id;
    }
    public void print() {
        System.out.print(id + "-" + name + ",");
    }
}


2-Santosh,2-Santosh,2-Santosh,5-Soumya,
15-Pramod,15-Pramod,15-Pramod,13-Nyamath,
15-Pramod,15-Pramod,15-Pramod,5-Soumya,
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