public void takeList(List<? extends String> list) {

Given:

3. import java.util.*;
4. public class G1 {
5. public void takeList(List<? extends String> list) {
6. // insert code here
7. }
8. }

Which three code fragments, inserted independently at line 6, will compile? (Choose three.)

Given:

3. import java.util.*;
4. public class G1 {
5. public void takeList(List<? extends String> list) {
6. // insert code here
7. }
8. }

Which three code fragments, inserted independently at line 6, will compile? (Choose three.)

A.
list.add(“foo”);

B.
Object o = list;

C.
String s = list.get(0);

D.
list = new ArrayList<String>();

E.
list = new ArrayList<Object>();

Explanation:
A:
Main.java:6: cannot find symbol
symbol : method add(java.lang.String)
location: interface java.util.List<capture#824 of ? extends java.lang.String>
list.add(“foo”);
^
1 error

B:
compiled successfully

C:
compiled successfully

D:
compiled successfully

E:
Main.java:6: incompatible types
found : java.util.ArrayList<java.lang.Object>
required: java.util.List<? extends java.lang.String>
list = new ArrayList<Object>();
^
1 error



Leave a Reply 0

Your email address will not be published. Required fields are marked *