aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes  treeset question Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark " treeset question " Watch " treeset question " New topic
Author

treeset question

geeta vemula
Ranch Hand

Joined: Jul 18, 2008
Posts: 208
I got this from K and B master exam (learnkey)

private Set<Integer> numbers=new TreeSet<Integer>();

public Nmbers(int... nums) {
for(int n: nums)
numbers.add(n);
}

public Numbers negate() {
Numbers negatives=new Numbers(); //Line 1
for(int n: numbers)
negatives.numbers.add(-n);
return negatives;
}

public void show() {
for(int n : numbers)
System.out.println(n+"");
}
public static void main(String[] args)
{
new Numbers(1,3,-5).negate().show();
}
}

Answer is : -3 -1 5

In this at line 1 it is trying to call Numbers negatives=new Numbers();
(no arg constructor) then won't it give compiler error?
Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
I am also surprised to this question,because compiler wont create no-arg constructor if we created one.In this case how it is possible (numbers n=new numbers() invoking show()?

Thanks
preetha
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

Where is the Numbers Class?



If they are same then it is legal to not pass any arguments if your constructor is taking var-args. You may or may not pass any number of arguments.
[ December 20, 2008: Message edited by: Himanshu Gupta ]

My Blog SCJP 5 SCWCD 5
Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
Ya , i got it. it can take zero as argument.so it i possible to create no-arg constructor.am i right?

Thanks
Preetha
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

YES you are right.
geeta vemula
Ranch Hand

Joined: Jul 18, 2008
Posts: 208
yes i got it..

Thanks,
Geeta Vemula
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: treeset question
 
Similar Threads
Another Doubt in K&b exam
K&B Mock Exam Question
TreeSet
Question on Q32 of the exam prep
confused about a question in mock quiz SCJP book by KS & BB