Knowee
Questions
Features
Study Tools

Which of the following statements is VALID?Group of answer choicesArrayList<? > l = new ArrayList<? extends Number >( );ArrayList<? > l = new ArrayList<?>( );ArrayList<? extends Number> l = new ArrayList<String>( );ArrayList<? super String> l = new ArrayList<Object>( );

Question

Which of the following statements is VALID?Group of answer choicesArrayList<? > l = new ArrayList<? extends Number >( );ArrayList<? > l = new ArrayList<?>( );ArrayList<? extends Number> l = new ArrayList<String>( );ArrayList<? super String> l = new ArrayList<Object>( );

🧐 Not the exact question you are looking for?Go ask a question

Solution 1

The valid statement among the given options is:

ArrayList<? super String> l = new ArrayList<Object>( );

Explanation:

  1. ArrayList<? > l = new ArrayList<? extends Number >( ); - This is not valid because you cannot instantiate a wildcard parameterized ArrayList.

  2. ArrayList<? > l = new ArrayList<?>( ); - This is not valid for the same reason as above.

  3. ArrayList<? extends Number> l = new ArrayList

This problem has been solved

Solution 2

The valid statement among the given options is:

ArrayList<? super String> l = new ArrayList<Object>( );

Explanation:

  1. ArrayList<? > l = new ArrayList<? extends Number >( ); - This is not valid because you cannot instantiate an ArrayList with wildcard extends.

  2. ArrayList<? > l = new ArrayList<?>( ); - This is not valid because you cannot instantiate an ArrayList with wildcard only.

  3. ArrayList<? extends Number> l = new ArrayList<String>( ); - This is not valid because String is not a subclass of Number.

  4. ArrayList<? super String> l = new ArrayList<Object>( ); - This is valid because Object is a superclass of String. Here, we are declaring an ArrayList which can hold objects of type String or any superclass of String.

This problem has been solved

Similar Questions

Which of the following Java code snippets gives a compilation error?Group of answer choicesclass MyClass<T> {         public void add(T item) {                  // ...         }}class MyClass<T extends Number & Comparable<T>> {          // ...}class MyClass<T super Number> {         // ...}class MyClass<T extends String> {          public void add(T item) {                     // ...           }}

Which of the following are Java primitive data types?  You can select more than one answer.Group of answer choicesArrayintbooleanstringClass

The following code gives a compilation error (TRUE/FALSE).ArrayList dates = new ArrayList();dates.add(new Date());dates.add(new String());Group of answer choicesFalseTrue

The elements of an ArrayList<T> in Java are homogeneous.Group of answer choicesTrueFalse

Which of the following array creation statements are incorrect?  Please select all that apply.Group of answer choicesint[] a = new int[2];int a[] = new int[2];int[] a = new int(2);int a = new int[2];int a() = new int[2];

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.