• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Java Interview Questions

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JavaTM 2 Platform Standard Edition 5.0 API Specification
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Paul Sturrock:
JavaTM 2 Platform Standard Edition 5.0 API Specification



Thx Paul Sturrock

But API of which class?
 
Hussain Fakhruddin
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
API of Collections class
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note: There is also a similar Arrays class.
 
Amateurs built google. Professionals built the titanic. We can't find the guy that built this tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic