• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SCJP 5: Question on Generics

 
Krishna Srinivasan
Ranch Hand
Posts: 1862
Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

/*

what will be the output?

1)SCJP SCWCD
2)no output.
3)It will not compile.

*/

( tags added, option numbering typo fixed)

[ January 11, 2005: Message edited by: Barry Gaunt ]

(Modified topic title to emphasize that it is for the new SCJP 5 beta exam)
[ January 12, 2005: Message edited by: Barry Gaunt ]
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
3
 
Pratap Rana
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will not compile.

For more explanation look at the Topic "2.8 Generics and Type Conversions" explained in the book "Java 1.5 Tiger: A Developer's Notebook"
 
Krishna Srinivasan
Ranch Hand
Posts: 1862
Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
one more question:

 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
2 IS THE ANSWER.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the question here?
Everyone (almost) knows it won't compile.
You'd know that too if you tried.
Are you asking why?
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tony Morris:
What is the question here?
Everyone (almost) knows it won't compile.
You'd know that too if you tried.
Are you asking why?


Why dont you post some mock questions here for the beta exam?
 
Krishna Srinivasan
Ranch Hand
Posts: 1862
Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What is the question here?
Everyone (almost) knows it won't compile.
You'd know that too if you tried.
Are you asking why?


You can post some mock question and let others to post the answers.
As we know there is no mock exams for Java 1.5. This is the way to prepare for tiger exam.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually it prints:

[Java, Added]

which is interesting because it implies that it is first converted to an array and then java.util.Arrays.toString is used to print it.
 
Krishna Srinivasan
Ranch Hand
Posts: 1862
Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Actually it prints:

[Java, Added]

which is interesting because it implies that it is first converted to an array and then java.util.Arrays.toString is used to print it.



Barry,
Can you explain it?
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Barry Gaunt:
Actually it prints:

[Java, Added]

which is interesting because it implies that it is first converted to an array and then java.util.Arrays.toString is used to print it.


For %s

If the argument is null, then the result is "null". If the argument implements Formattable, then its formatTo method is invoked. Otherwise, the result is obtained by invoking the argument's toString() method.

So AbstracCollection's toString method is invoked.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Explaination. You are passing list and list1 both reference the same ArrayList, so you can add a String to list1, but you wouldn't be able to add a String to list. But since list is still pointing to the same ArrayList it will have the second added String in the ArrayList and print it.

Mark
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What is the question here?
Everyone (almost) knows it won't compile.
You'd know that too if you tried.
Are you asking why?


My bad - I was responding to the original post.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic