Multiple choice technology programming languages

Which of the following statements hold true in the following program? package generics; import java.util.ArrayList; import java.util.List; public class Ques04 { public static void main(String[] args) { List extends Instrument> allInstruments = new ArrayList(); // -->X allInstruments.add(new Guitar()); allInstruments.add(new Violin()); } } interface Instrument { public void play(); } class Guitar implements Instrument { public void play(){ System.out.println("Playing Guitar."); } } class Violin implements Instrument { public void play(){ System.out.println("Playing Violin."); } }

  1. The program will not compile.

  2. The program will compile but will throw run-time exception.

  3. The statement 'X' manifests that objects of type Instrument as well as sub-types can be added to the list 'allInstruments'.

  4. It is not possible to add any type of objects to the list 'allInstruments'.

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

The wildcard ? extends Instrument means the list can hold any subtype of Instrument, but the compiler cannot guarantee type safety for add operations. You can only add null to such a list because the actual type could be any specific subclass (like Guitar or Violin), and adding the wrong subtype would break type safety. The code attempts to add Guitar and Violin objects, which causes a compilation error.