aspose file tools*
The moose likes Java in General and the fly likes Java Interview Questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java Interview Questions" Watch "Java Interview Questions" New topic
Author

Java Interview Questions

Hussain Fakhruddin
Ranch Hand

Joined: Aug 13, 2004
Posts: 96
Today was my interview ... and there few question that I was unable to answer.

-I have a arraylist in a class that I want to make readyonly how can i do that.
-How to Select top n queries from a table just by using simple SQL query.


Hussain Fakruddin<br /> <br /><b>IBM 486-OOAD with UML</b>
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


I have a arraylist in a class that I want to make readyonly how can i do that

Did they mean you can't add and remove object from your ArrayList, or that the values of the objects contained in the List should be unchangeable?


How to Select top n queries from a table just by using simple SQL query.

As far as I am aware there is no way to do this with ANSI SQL, only by using some DB specific variation, such as top, limit or rownum.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Hussain Fakhruddin
Ranch Hand

Joined: Aug 13, 2004
Posts: 96
Did they mean you can't add and remove object from your ArrayList, or that the values of the objects contained in the List should be unchangeable?


Actually the question was like this ... 1st they asked me how to make a class readonly .. I said by making all the variables private and providing getters for them ... then they said what if one of the field is an ArrayList ... I said make it final .. they replied ArrayList should be able to change within the class ... but some who access arraylist from outside should only be able to see its contents.

As far as I am aware there is no way to do this with ANSI SQL, only by using some DB specific variation, such as top, limit or rownum.


Well this is what they ask.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18999
    
  40

Actually the question was like this ... 1st they asked me how to make a class readonly .. I said by making all the variables private and providing getters for them ... then they said what if one of the field is an ArrayList ... I said make it final .. they replied ArrayList should be able to change within the class ... but some who access arraylist from outside should only be able to see its contents.


An easy but tedious way to do this is to create a "filter" list object -- that contains the internal list. The implementation methods basically forwards all non mutable operations to the interal list object. Don't forget the iterator, you also need to create a "filter" iterator that does the same thing. This iterator is created and returned when an iterator is asked for.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Julien Grenier
Ranch Hand

Joined: Sep 01, 2005
Posts: 41

Actually the question was like this ... 1st they asked me how to make a class readonly .. I said by making all the variables private and providing getters for them ... then they said what if one of the field is an ArrayList ... I said make it final .. they replied ArrayList should be able to change within the class ... but some who access arraylist from outside should only be able to see its contents.


the answer is in the API



see the Javadoc for more information

Julien.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18999
    
  40

Wow... learn something new everyday...

Took a look at the source code, it actually does it the way I recommend doing it...

Henry
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Except that only guarentees you can't add or remove objects from this List, but makes no promises about changing the value of the objects stored in it. New list, but same object references.
[ September 28, 2005: Message edited by: Paul Sturrock ]
Hussain Fakhruddin
Ranch Hand

Joined: Aug 13, 2004
Posts: 96
Great the 1st question is solved so now what abt the second question

-How to Select top n queries from a table just by using simple SQL query.
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

-How to Select top n queries from a table just by using simple SQL query.
There are various database-dependent methods; many can use a TOP or LIMIT clause.
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
As has been mentioned, there are database-dependent ways of doing that, but just through SQL without using something database-dependent I don't think is possible.
Dan Bizman
Ranch Hand

Joined: Feb 25, 2003
Posts: 387
Well, this isn't a Java question and it's been a while but...I think this is ANSI compliant:

SELECT c1.NAME FROM CUSTOMER AS c1
INNER JOIN
CUSTOMER AS c2
ON c1.ID = c2.ID
GROUP BY
c1.ID
HAVING COUNT(DISTINCT c2.ID) = 10

Where 10 is your "n" rows. I may have something off in there, but I think that works. It's not efficient, but it answers the question.
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
The count approach is of limited value, although it does answer the question. I would probably have turned that around and asked what fetching only the top 10 records accomplished. Faster? No: it applies the filter after the query is done. Less bandwidth? Hardly: you can set the size of the result set and the default is 100 records. How do you then get the next 10 records after fetching the first? Count won't work in that case.
Hussain Fakhruddin
Ranch Hand

Joined: Aug 13, 2004
Posts: 96
Wow!


I wouldn't have been able to answer that.

well Rick O'Shay in the interview there are sometimes question that don't make sense practically ... but you need to know them and answer them.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18999
    
  40

Originally posted by Hussain Fakhruddin:

I wouldn't have been able to answer that.

well Rick O'Shay in the interview there are sometimes question that don't make sense practically ... but you need to know them and answer them.


I don't completely agree. I think the best questions are open ended, and sometimes don't have any answers. One of the goals of the interview is to show how you think. In this regard, if you don't have an answer, work out the problem out loud, involve the interviewer in the process. Let him/her know how you work, and work well, with others.

Henry
Hussain Fakhruddin
Ranch Hand

Joined: Aug 13, 2004
Posts: 96
An easy but tedious way to do this is to create a "filter" list object -- that contains the internal list. The implementation methods basically forwards all non mutable operations to the interal list object. Don't forget the iterator, you also need to create a "filter" iterator that does the same thing. This iterator is created and returned when an iterator is asked for.


Well that's wht my opinion tooo ... and I'am goin to do that when I'll be taking interviews.
Dan Bizman
Ranch Hand

Joined: Feb 25, 2003
Posts: 387
Originally posted by Rick O'Shay:
The count approach is of limited value, although it does answer the question. I would probably have turned that around and asked what fetching only the top 10 records accomplished. Faster? No: it applies the filter after the query is done. Less bandwidth? Hardly: you can set the size of the result set and the default is 100 records. How do you then get the next 10 records after fetching the first? Count won't work in that case.



Well, yes and no. It's not faster if you use straight SQL calls, but if you put that statement into a stored procedure, the database would monitor how it is used and over time make it more and more efficient.

Of course, I COMPLETELY disagree with doing that because you're putting middle tier logic into the DB.
Chetan Parekh
Ranch Hand

Joined: Sep 16, 2004
Posts: 3636
Originally posted by Julien Grenier:


see the Javadoc for more information

Julien.

I am not clear about which API you are talking.

It would be nice if you post link of that API here.

Thx
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

JavaTM 2 Platform Standard Edition 5.0 API Specification
Chetan Parekh
Ranch Hand

Joined: Sep 16, 2004
Posts: 3636


Thx Paul Sturrock

But API of which class?
Hussain Fakhruddin
Ranch Hand

Joined: Aug 13, 2004
Posts: 96
API of Collections class
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Note: There is also a similar Arrays class.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Interview Questions