Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Interview Questions

 
Hussain Fakhruddin
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Hussain Fakhruddin
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20889
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Julien Grenier
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Marshal
Pie
Posts: 20889
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
-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
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20889
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Thx Paul Sturrock

But API of which class?
 
Hussain Fakhruddin
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
API of Collections class
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note: There is also a similar Arrays class.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic