Return to site

JAVA CERTIFICATION QUESTION: Inheritance between Generics collections

March 26, 2022

 

Given:

 

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๊“›