I recently started studying Java's ArrayList class, and learned about how we can use an ArrayList <Object> to hold elements of different class types. This question, however, confused me.
My general thinking pattern in answering this question was that we can first eliminate answers A and B, because the compiler would complain about using the + operator on two Objects that have not been cast. When I looked at answers C and D, however, it seemed to me that both of them would work correctly, because they both cast the values returns from list.get() to a type where the operator + is defined. This led me to choose answer E, as I believed both C and D could work in this piece of code.
As you can see, however, E was the wrong answer. To compound my confusion, I tested this piece of code on repl.it, where it was confirmed that answer choices A and B result in compilation errors while answer choices C and D both store the correct value of 9 into i. So now, I'm really confused. Is there some rule with Objects that I am glossing over here? Can someone point me in the right direction?

ewhen you know other ways to sum both numbers that is not listed there? If yes, theneis correct as well, because there are several other ways to fill the "blank".inttakes place on the entirelist.get()operation. The key phrase beingmore correct.) has higher precedence than the cast. That's the reason you need to do((Whatever)x).y()if you want to castxtoWhateverbefore invoking theymethod