Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

braintester dan

 
Dan Polak
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check yourself , do not use JVM!!!

*List<Integer> v = new ArrayList();
*List<String> v1 = new ArrayList();
*
*out.print(v == v1); //line 3
*out.println(v.equals(v1));

a) prints falsefalse
b) compile time error
c) prints truetrue
d) if 'line 3' will be removed prints true
e) prints falsetrue
f) if 'line 3' will be removed prints false
 
Xiao Song
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
b
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
b and d
Explanations : Line 3 will results in a compilation error because it is comparing collection of two different types.
equals() in the ArrayList(implemented in AbstractList) compares all the collection objects one by one and if all matches it returns true else false.In this case there is no objects at all to compare with hence will return true.
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!