Java OCA OCP Practice Question 428

Question

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

  • A. save ( new ArrayList () );
  • B. Collection c = new ArrayList (); save ( c );
  • C. List l = new ArrayList (); save (l);
  • D. save (null);
  • E. save (0); //The argument is the number zero and not the letter o


Correct Options are  : A C D

Note

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.




PreviousNext

Related