Generics, the new feature of Java. Don't really expect to people to be comfortable specially if they have been working in older java versions. But if, candidate is certified SCJP on Java 5, things change. One can expect at least such fellows to be aware of Generics.
Q: So, you are certified SCJP 5 ?
A: Yes.
Q: That means you must be aware of generics ? One of the features introduced in Java 5.
A: Confidently, yes.
Q: Generics. I am sure you know what it is. What benefit does it provide ?
A: Generics !!! (Now started to look puzzled). Generics makes it easy as generic class can be inherited and allows to re-use code in generic class.
Now we are puzzled how does generic class and inheritance come up in Generics. Probably, there was confusion over question. So repeated it and ensured that interviewee understood its GENERICS no GENERIC CLASS.
Still there was no change in answer. Generic class helps in re-use implementation.
....
...
With some more such responses we were puzzled whether interviewee actually holds SCJP 5 certificate. So much so we ended up searching for SCJP certificate and to our surprise we found one which looked real.
Tuesday, April 20, 2010
Monday, April 19, 2010
Secret of interface Set: It implements
How does set ensures that set doesn't end up adding same object in a set twice. In my previous blog I mentioned about compile time error.
There is another answer to set's ability to maintain uniqueness.
A: Set uses equals method to compare whether two objects are same or not. (Just as you think this fellow is going in correct direction, read next sentence). All set objects have equals method so objects are compared by set itself.
Q: As you mentioned there is "equals" method in Set and then there is also "equals" method in class String. Suppose I create Set of string which of the "equals" method would be used by Set ? The one in Set and the one in String?
A: Set. Set is implemented in such a way that it calls equals method. Since set itself has "equals" method it is called.
Wow ...... Set is implemented such a way.
Q: Suppose I want to create custom Set class. So I create class CustomSet implements Set. What would I have to do in order to check for duplicates in my CustomSet class.
A: Nothing. Set is implemented in such a way that it will call equals method automatically when new item is added into set..
Q: So, nothing needs to be done for new custom class ?
A: (After some thinking) Nothing needs to be done.
Wow !!! Interface Set is implemented such a way that it automatically calls equals method from add method when ever it is implemented. And guess what interfaces implement secretly thing what you would want.
There is another answer to set's ability to maintain uniqueness.
A: Set uses equals method to compare whether two objects are same or not. (Just as you think this fellow is going in correct direction, read next sentence). All set objects have equals method so objects are compared by set itself.
Q: As you mentioned there is "equals" method in Set and then there is also "equals" method in class String. Suppose I create Set of string which of the "equals" method would be used by Set ? The one in Set and the one in String?
A: Set. Set is implemented in such a way that it calls equals method. Since set itself has "equals" method it is called.
Wow ...... Set is implemented such a way.
Q: Suppose I want to create custom Set class. So I create class CustomSet implements Set. What would I have to do in order to check for duplicates in my CustomSet class.
A: Nothing. Set is implemented in such a way that it will call equals method automatically when new item is added into set..
Q: So, nothing needs to be done for new custom class ?
A: (After some thinking) Nothing needs to be done.
Wow !!! Interface Set is implemented such a way that it automatically calls equals method from add method when ever it is implemented. And guess what interfaces implement secretly thing what you would want.
Wednesday, April 14, 2010
More Synchronized funda from another SCJP
Nowadays I wonder whether synchronized is a keyword of JAVA or not. Because if it were, it would have been clear to people appearing for interview especially those who are SCJP.
Since two threads can't execute different synchronized method at a time. A lock on object is acquired to achieve this. Fair enough, that's what is written in books. Can a synchronized method call another synchronized method ? Consider below example ... can methodA call methodB where both are synchronized ?
class A{
public synchronized void methodA(){
//some operations
methodB();
}
public synchronized void methodB(){
//some operations
}
}
What do you think ?
Well this is answer from SCJP .... Since lock is already acquired methodB will not be able to acquire it again. It would be blocked sort of deadlock. !!!!
Tuesday, April 13, 2010
Compile time error at Runtime
Well what is a Set ? Its simple isn't it ? A collection that doesn't contain any duplicate objects.
But then what ensures that set doesn't end up adding same object in a set twice. What do you think ?
Okay, if you are think it is pretty straight answer to this. What do you do when you end up hearing below as answer.
A: Set implementation ensures no duplicates are added. It throws error, compile time error.
Q: Huh, compile time error ? What if we have a set which stores employee name and program at run time asks for employee names. User enters and name is then added into the set. How will uniqueness of objects be ensured in set , what will happen if user enters same name twice and program tries to add same name (string) in Set again?
A: As I said an error will be thrown at compile time.
OMG !!! This is new one, compile time error is thrown at runtime. I still am trying to understand, how can this happen. Help me, if possible in understanding this concept. Software industry is indeed moving quickly, I hope I would be able to keep up with the pace its changing :).
But then what ensures that set doesn't end up adding same object in a set twice. What do you think ?
Okay, if you are think it is pretty straight answer to this. What do you do when you end up hearing below as answer.
A: Set implementation ensures no duplicates are added. It throws error, compile time error.
Q: Huh, compile time error ? What if we have a set which stores employee name and program at run time asks for employee names. User enters and name is then added into the set. How will uniqueness of objects be ensured in set , what will happen if user enters same name twice and program tries to add same name (string) in Set again?
A: As I said an error will be thrown at compile time.
OMG !!! This is new one, compile time error is thrown at runtime. I still am trying to understand, how can this happen. Help me, if possible in understanding this concept. Software industry is indeed moving quickly, I hope I would be able to keep up with the pace its changing :).
Tuesday, April 6, 2010
Some interesting concepts
Concepts of a person give a good insight on how person thinks and would be able to think when a problem domain is in front of them. So, often interviews involving entry level include questions comprising of basic concepts. Although, interviewer doesn't except bookish answers he also doesn't except below too. Or do they ?
Some of the questions in one such interview where I was involved and almost started to wonder do I know what I know is correct.
....
....
Q: What is the purpose of finalize method ?
A: Finalize is one of the methods in object class. Flinalize method tells us whether GC (garbage collecter) is running or not.
Whoa !!! interesting ... just wondering why do i need to know the status of GC anyway.
....
....
Q: As you said wait / notify are methods of object. Can I call these methods anywhere I wish or there is any restriction ?
A: They can be called in synchronized block. .... and method also.
...
Q: So can we have static synchronized method ?
A: Let me think !!!! After some thinking ...... I don't think so. Static method is associated with class not instance. Since we don't have an object to have lock on, it is not possible.
Ohhh !!!! Answer with logical explaintion.
....
....
And then best of all
Q: What are prepared statements ?
A: They are pre-compiled statements, good for performance.
Yeah !!! This fellow knows what prepared statements are. I also thought so. Keep reading.
Q:So why are such statements called pre-compiled statements ?
A: Because the are compiled only once. Suppose we want to execute a query "select * from employee where employeeId=?" Such queries where ? are there and we set values before executing are prepared statements.
Q:Okay, we have ? in query, so our statement would be prepared statement ?
A: Yes
Q: What do you mean with compiling ... in pre-compiler statement ?
A: When we compile java class, query written in java also gets compiled. So we also call it pre-compiled statement.
Ohh no !!! he did it again another concept.
Q: Oh.. so If we want to fetch all employees from employee table "select * from employee" and compile java class. My query would get compiled first. So I would have pre-complied statement ?
A: Yes
Whoa !!!!
....
...
I hope after reading this, you found some thing not to learn from.
Thursday, April 1, 2010
What does a SCJP think synchronized keyword does ?
Sun Certified Java Programmer (SCJP)..... One would safely assume that a person who has cleared SCJP at least understands java as a language, its constructs keywords.
Q: What does synchronized keyword do when added as modified of a method?
A: It prevents two threads to execute method at a same time.
Hmm, answer is acceptable. So, whats the buzz about it being hilarious ..... read on ...
Q: So how does JVM ensure that only one thread executes synchronized method a time ?
A: Well locks are acquired against method.
Q: Okay, locks are acquired on methods !!!!! right ?
A: Yes
Now this fellow is trapped !!!!
Q: If in a class there are two synchronized methods. Can two separate threads execute those two synchronized methods at the same time as both will have lock of separate methods will be acquired ?
A: Absolutely
Disclaimer: Although I am SCJP, I wasn't answering these questions in this manner. Please do not confuse me and interviewee.
Q: What does synchronized keyword do when added as modified of a method?
A: It prevents two threads to execute method at a same time.
Hmm, answer is acceptable. So, whats the buzz about it being hilarious ..... read on ...
Q: So how does JVM ensure that only one thread executes synchronized method a time ?
A: Well locks are acquired against method.
Q: Okay, locks are acquired on methods !!!!! right ?
A: Yes
Now this fellow is trapped !!!!
Q: If in a class there are two synchronized methods. Can two separate threads execute those two synchronized methods at the same time as both will have lock of separate methods will be acquired ?
A: Absolutely
Disclaimer: Although I am SCJP, I wasn't answering these questions in this manner. Please do not confuse me and interviewee.
Creating object of a class with private constructor outside the class
How to create object of a class with private constructor outside the class ? Pretty easy question !!! But below is hilarious concept revealed by a person who came for interview. I guess Sun, ohh Oracle should consider it as a feature for JDK 8.0
class RR {
private RR { }
}
//Outside class RR
this.rr { }
And object of class RR will be created.
class RR {
private RR { }
}
//Outside class RR
this.rr { }
And object of class RR will be created.
Subscribe to:
Comments (Atom)