Multiple choice technology web technology

Given: 3. import java.util.*; 4. class Business { } 5. class Hotel extends Business { } 6. class Inn extends Hotel { } 7. public class Travel { 8. ArrayList go() { 9. // insert code here 10. } 11. } Which, inserted independently at line 9, will compile? (Choose all that apply.)

  1. return new ArrayList<Inn>();

  2. return new ArrayList<Hotel>();

  3. return new ArrayList<Object>();

  4. return new ArrayList<Business>();

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

The method return type is ArrayList. Option B works because you can return an ArrayList directly - the types match exactly. Option A fails because Inn is a subtype of Hotel, and ArrayList is not assignable to ArrayList (generics are invariant). Option C fails because Object is not a Hotel. Option D fails because Business is a supertype of Hotel, and ArrayList is not assignable to ArrayList.