Return to site

JAVA CERTIFICATION QUESTION: Inheritance between Generics collections

· java,ocp

 

Given:

List l = new ArrayList(); // Line 1

l.add(0); // Line 2

 

What happens when compiling and executing the given code?

A. Compilation fails on line 1

B. Compilation fails on line 2

C. ClassCastException is thrown on line 1

D. ClassCastException is thrown on line 2

E. No compile-time or runtime error occurs

 

 

Solution👇👇👇

Despite the fact that Number is a supertype of Long, the parameterized types List and ArrayList have no relationship.

The assignment on line 1 is therefore between incompatible types, which results in a compile-time error.

(𝘈𝘳𝘳𝘢𝘺𝘓𝘪𝘴𝘵<𝘓𝘰𝘯𝘨>() 𝘥𝘰𝘦𝘴 𝘯𝘰𝘵 𝘪𝘯𝘩𝘦𝘳𝘪𝘵 𝘧𝘳𝘰𝘮 𝘓𝘪𝘴𝘵<𝘕𝘶𝘮𝘣𝘦𝘳> 𝘸𝘩𝘪𝘭𝘦 𝘓𝘰𝘯𝘨 𝘪𝘯𝘩𝘦𝘳𝘪𝘵𝘴 𝘧𝘳𝘰𝘮 𝘕𝘶𝘮𝘣𝘦𝘳).

 

 

 

·Ɐ sᴉ ɹǝʍsuɐ ʇɔǝɹɹoɔ ǝɥꓕ :uoᴉsnʅɔuoꓛ

All Posts
×

Almost done…

We just sent you an email. Please click the link in the email to confirm your subscription!

OK