10
November

Oh no, this is not an yet another article posting plain old java interview questions. I’ll post some tricky java interview questions, that are scenario based and design based. I have been asked some of these questions in my technical interviews (yes, I don’t want to disclose the company names!).

Here they go.

  1. Give the use and an example of the scenario where you would use serialVersionUID in a your Java class?
  2. Design a Employee class with name, empId, age and address fields.
    • Any two Employee objects are compared based on their age.
    • The empId is the unique id for Employee class. The Employee class can be used in Collections and HashMap.
  3. Explain what goes in a HashMap or Hashtable, when you store the objects with same hashcode values. Explain your answer in very depth.
  4. In JVM Memory, where the private, public, protected, static, String literal and object references are stored?
  5. How will you store your exception stack trace in a file? (you should not redirect the console output)
  6. What is the use of Weak, Soft, Phantom references in Garbage Collection of java? Give scenarios.
  7. How will you keep an object alive for ever in the JVM memory? (trick: use finalize() method)

That’s all for now.

So, the answers? I will leave the answer part to my readers! :) You could post your answers as comments. Either, if you want the answers, let me know (in the comment).

Related posts:

  1. Design Patterns Interview Questions Patterns are so important, if you are giving a software...
  2. Java Collections API Interview Questions Here we go: QUESTIONS: 1. You need to insert huge...
  3. JSP Servlet Interview questions Many people have requested to post JSP Servlets scenario based...
  4. Java Threads Interview Questions Many people feel that Threads and Collection APIs are somewhat...
  5. Java, J2EE Performance Tuning Interview Questions And, I’m back with some performance related interview questions. The...

Related posts brought to you by Yet Another Related Posts Plugin.

Category : Interview Questions / Java J2EE Interview Questions / Technical

Comments

Sivakumar November 13, 2008

can you please post the answer for question:
4.In JVM Memory, where the private, public, protected, static, String literal and object references are stored?

hanumanth Bandi November 13, 2008

How will you store your exception stack trace in a file?
ANS:

try
{
printWriter=new PrintWriter(”D:/Exception.txt”);
int i=10,j=0;
int res=i/j;
}
catch(Exception e)
{
StackTraceElement[]str=e.getStackTrace(); printWriter.write(e.getMessage());
printWriter.println(”————-”);
for(int i=0;i<str.length;i++)
{ printWriter.write(str[i].toString()); printWriter.write(str[i].getLineNumber()); printWriter.println();
}
printWriter.close();
}

it will generates a file name Exception.txt in D:

Thanks,
Hanu

Manivannan Palanichamy November 13, 2008

Thanks Hanumant! That’s really nice work! :)

gerlad November 16, 2008

post the answers please.

Manivannan Palanichamy November 16, 2008

ya, sure. give enough time!

cgma January 28, 2009

hey mani, probably enough time has gone by now… =)

Manivannan Palanichamy February 1, 2009

@cgma: 23 patterns right… can write a 250-page book! :)

Dinesh July 21, 2009

Q5: How will you store your exception stack trace in a file? (you should not redirect the console output)

java.lang.Throwable.printStackTrace() method has two more overloaded implementations…

printStackTrace(PrintStream s) and printStackTrace(PrintWriter s)

thus the solution….

Gaurika November 2, 2009

Can you please send the answers to me at my email id. I would be very greatful. they are inded very informative

Vithya January 19, 2010

COuld you please post the answers …

Manivannan January 19, 2010

Sure guys will post the answers ;)

das January 21, 2010

Mani, Can you please post these answers to my email.

Neeraj Vashisht January 31, 2010

Please send me the answers to the questions and please send me more scenrio based question and answer related to design and architecture.

Gajendra March 7, 2010

After seeing all the comments i have concluded that author of this question too dont know the answers , thats why he is not posting the answer

Manoj March 19, 2010

I am also waiting of the Answers

arokia April 14, 2010

1. you would use when u implement interface Serializable.

Anonymous April 16, 2010

Ans) During object serialization, the default Java serialization mechanism writes the metadata about the object, which includes the class name, field names and types, and superclass. This class definition is stored as a part of the serialized object. This stored metadata enables the deserialization process to reconstitute the objects and map the stream data into the class attributes with the appropriate type
Everytime an object is serialized the java serialization mechanism automatically computes a hash value. ObjectStreamClass’s computeSerialVersionUID() method passes the class name, sorted member names, modifiers, and interfaces to the secure hash algorithm (SHA), which returns a hash value.The serialVersionUID is also called suid.
So when the serilaize object is retrieved , the JVM first evaluates the suid of the serialized class and compares the suid value with the one of the object. If the suid values match then the object is said to be compatible with the class and hence it is de-serialized. If not InvalidClassException exception is thrown.

Changes to a serializable class can be compatible or incompatible. Following is the list of changes which are compatible:

•Add fields
•Change a field from static to non-static
•Change a field from transient to non-transient
•Add classes to the object tree
List of incompatible changes:

•Delete fields
•Change class hierarchy
•Change non-static to static
•Change non-transient to transient
•Change type of a primitive field
So, if no suid is present , inspite of making compatible changes, jvm generates new suid thus resulting in an exception if prior release version object is used .
The only way to get rid of the exception is to recompile and deploy the application again.

If we explicitly metion the suid using the statement:

private final static long serialVersionUID =

then if any of the metioned compatible changes are made the class need not to be recompiled. But for incompatible changes there is no other way than to compile again.

Qaisar May 5, 2010

Hi
Thanks for posting such a good question, kindly can you send me the answer for the these question to my email ID.

Vaibhav May 25, 2010

adding a reference to the object being finalized to a static linked list that is still “live” will resurrect an object

Anonymous May 29, 2010

ghhj

Arvind June 8, 2010

pls send the solutions to my email id

Munish Gogna June 8, 2010

Redirecting the exception TRACE to a file —

try {
throw new Exception(”hurray!!!”);
} catch (Exception e) {
File file = new File(”test”);
FileWriter fileWriter = new FileWriter(file);
PrintWriter printWriter = new PrintWriter(fileWriter, true);
e.printStackTrace(printWriter);
}

kin June 12, 2010

Most of the question’s solution is easily google. Thanks for posting the question.

I think know the question is good enough. Let’s do some very little research on google. You will find the answer.

Leave a comment

Spam protection by WP Captcha-Free