Consider the following code to count objects and save the most recent object.
int i = 0 ; Object prevObject ; public void save (List e ){ prevObject = e ; i++ ; }
Which of the following calls will work without throwing an exception?
Select 3 options
Correct Options are : A C D
A. is a correct answer. Because an ArrayList is a List.
B. is wrong. save()
cannot accept c because c is declared of type Collection, which is a superclass of List, but the save()
method expects a List.
In option E. 0 is an int, which means it is a primitive.
So it will be boxed into an Integer object when you pass it to a method that expects an Object.
However, Integer cannot be passed to a method that expects a List.
Therefore, this option is not valid.
Had the method bean save (Object obj), it would have been valid because an Integer is an Object.