aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes John Meyers Mock exam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "John Meyers Mock exam" Watch "John Meyers Mock exam" New topic
Author

John Meyers Mock exam

Mamadou Touré
Ranch Hand

Joined: Dec 27, 2007
Posts: 189
The correct answer is:
// Fragment 1
List <Integer> l = Arrays.asList( new int[] {1,2,3,4} );


// Fragment 2
List <Object> list = new List <Object> ();

Both fragments will not compile. The asList() method takes a type T as an argument so int[] does not fit that description. You cannot instantiate the interface List.

I don't understand the explanation given for the compilation failure of fragment1( The asList() method takes a type T as an argument so int[] does not fit that description)

Could someone explain me please ?


SCJP 5 (76%)
SCWCD 5 (86%)
SCBCD 5(70%)
--------------------
"The greatest glory in living lies not in never falling, but in raising every time we fall.".. Nelson Mandela
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19060
    
  40

Generic types are objects. It is expecting an array of Type T (which has to be an object type). Type T can't be an int, because ints is a primitive type.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Mamadou Touré
Ranch Hand

Joined: Dec 27, 2007
Posts: 189
Hello Henry,

I tried to create an array of Integer, but it doesnt work either. why ?

Integer[] ar = {1,2,3,4};
List<Integer> l = Arrays.asList(ar);
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19060
    
  40

I tried to create an array of Integer, but it doesnt work either. why ?


It works fine for me. What is the error that you are getting?

Henry
Mamadou Touré
Ranch Hand

Joined: Dec 27, 2007
Posts: 189
Oh thanks, I forgot to import java.util.*

sorry
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

Aarggh ! Henry beat me to it

His explanation is quite to the point.

Good luck with your preparation Mamadou


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: John Meyers Mock exam