Java Interface types and Comparator

Here are 10 essential multiple-choice questions on Java Interface types and Comparator, covering key concepts.



Last Updated :
Discuss
Comments

Question 1

Which of the following statements about functional interfaces is true?


  • A functional interface can have multiple abstract methods


  • Functional interfaces are only available from Java 11 onwards

  • A functional interface can have multiple default and static methods

  • Runnable is not a functional interface

Question 2

What will be the output of the following code?

Java
@FunctionalInterface
interface Geeks {
    void display();
    default void show() { 
        System.out.println("Default method"); 
        
    }
}
public class Test {
    public static void main(String[] args) {
        Geeks obj = () -> System.out.println("Lambda Executed");
        obj.display();
    }
}


  • Default method

  • Lambda Executed

  • Compilation Error

  • Runtime Error

Question 3

How is a nested interface defined in Java?


  • An interface declared inside another interface or class

  • An interface that extends another interface

  • An interface that has private methods

  • An interface with only default methods

Question 4

Which of the following is NOT true about nested interfaces?

  • A nested interface inside a class must be declared static

  • A nested interface can be accessed using OuterClass.InnerInterface


  • A nested interface can have constructors

  • A class implementing a nested interface must implement all its methods

Question 5

Which of the following is a correct example of a Marker Interface?

  • interface Serializable { void serialize(); }


  • interface Cloneable {}


  • interface Marker { void mark(); }

  • interface Runnable { void run(); }

Question 6

What is the primary use of a marker interface in Java?

  • To define constants

  • To enforce a specific method implementation


  • To provide metadata for runtime processing

  • To allow multiple inheritance

Question 7

What is true about Java’s Comparator interface?

  • It can compare only primitive types

  • It is used for natural ordering of objects

  • It defines the compare() method for custom sorting

  • It can only be used with TreeSet


Question 8

What will be the output of the following code?

Java
import java.util.*;
class Student {
    int age;
    Student(int age) { this.age = age; }
}
class AgeComparator implements Comparator<Student> {
    public int compare(Student s1, Student s2) {
        return s1.age - s2.age;
    }
}
public class Test {
    public static void main(String[] args) {
        List<Student> list = Arrays.asList(new Student(20), new Student(18));
        Collections.sort(list, new AgeComparator());
        System.out.println(list.get(0).age);
    }
}


  • 20

  • 18

  • Compilation Error


  • Runtime Error

Question 9

How is Comparator different from Comparable?

  • Comparator is implemented in the same class as the object being compared

  • Comparable provides multiple sorting sequences

  • Comparator allows sorting objects without modifying their class

  • Comparable is a class, whereas Comparator is an interface

Question 10

What is the benefit of using a lambda expression with a Comparator?

  • It makes sorting slower

  • It eliminates the need for method references

  • It reduces boilerplate code

  • It is only supported from Java 17 onwards

There are 10 questions to complete.

Take a part in the ongoing discussion