File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Jobs Discussion and the fly likes My interview questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Careers » Jobs Discussion
Bookmark "My interview questions" Watch "My interview questions" New topic
Author

My interview questions

Manikandan Swaminathan
Ranch Hand

Joined: Aug 10, 2008
Posts: 117
Hi all,

Some of the interview questions faced by me recently and the answers below.

1. Do we really need a finally() block?
[Ans]: Yes its better to have a finally block as we can close the connection objects and free other resources here.

2. I have some statements after try,catch,finally block.Will that be executed?
[Ans]: If there is any exception, the program control will go to the catch block & finally block & then the program execution stops.
If there is no exception, then the statements after the finally() block will be executed.

3. There is a class A.java & it has a method. Class B extends Class A & Class C extends Class B inturn. How to prevent the method in Class A from being accessed in Class C using C's instance? (Method in A is public ONLY)
[Ans]: I told that we can override the method in Class C with the same name & syntax as that of Class A so that the overridden method is called.. Dont know whether my answer is correct

4. In a singleton java class, how to make sure that more than one instance of the object is not getting created at any point of time?
[Ans]: Dont know (Is it possible through cloning ??)

Can you please review and let me know if my answers are correct.

Any suggestions are warmly welcome.

Thanks

arulk pillai
Author
Ranch Hand

Joined: May 31, 2007
Posts: 3219
4. In a singleton java class, how to make sure that more than one instance of the object is not getting created at any point of time?


-- Firstly you need to ensure that your singleton class gets loaded by a single class loader.

-- How the instances are created? Eagerly instantiated or Lazily instantiated. If laziliy loaded, ensure that multiple threads cannot create more than a single instance. Google for double checked locking anti-pattern

-- Multiple singletons arising when Someone has subclassed your singleton. Ensure that the constructor is either private or protected to prevent instantiation from outside or extension. Where possible, mark your class as final.

-- it is possible for an instance of the class to be serialized once, then deserialized many times, resulting in many instance being created.

-- Reflection attacks are possible.


So, if you are using Java 5 or later then use the "enum".




for example --> http://whiteboxcomputing.com/java/enum_factory/


Also read, http://java.sun.com/developer/technicalArticles/Programming/singletons/




Java Interview Questions and Answers Blog | Amazon.com profile | Java Interview Books
Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

Raghuraman Guruswamy wrote:

2. I have some statements after try,catch,finally block.Will that be executed?
[Ans]: If there is any exception, the program control will go to the catch block & finally block & then the program execution stops.
If there is no exception, then the statements after the finally() block will be executed.



2. This is incorrect, if an Exception is thrown and it is the class (or subclass) of the Exception you have in your catch(..) then the catch block is executed, then the finally block and then after that the statements after the try-catch-finally will also be executed. If the exception is not caught by the catch block then the current execution will stop and an exception will be thrown which could be caught higher up.

Sean


I love this place!
arulk pillai
Author
Ranch Hand

Joined: May 31, 2007
Posts: 3219
Only time the finally block is not reached is when

-- there is a System.exit() statement.
-- there is an endless loop

Other times, the finally block is always executed




The ouput is:

Manikandan Swaminathan
Ranch Hand

Joined: Aug 10, 2008
Posts: 117
All,

Thanks a lot for all of your replies.

Would be great if you can review my third question also.

Cheers
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
arulk pillai wrote:Only time the finally block is not reached is when

-- there is a System.exit() statement.
-- there is an endless loop
There are other possibilities, as in this post from 2006.
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

Raghuraman Guruswamy wrote:All,

Thanks a lot for all of your replies.

Would be great if you can review my third question also.

Cheers


My answer would be a reverse question to the interviewer. 'If you did not want the method to be inherited, why did you extend the class to begin with ?'

Either I do not understand the purpose of question 3, or the interviewer does not understand OO. Or was he / she trying to allude to a OperationNotSupportedException ?

[EDIT]

Replaced 'it' with class to add clarity


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
James Hodgkiss
Ranch Hand

Joined: Jan 22, 2004
Posts: 401
2. I have some statements after try,catch,finally block.Will that be executed?

My quick answer which I'd guess they're looking for: It depends if the catch or finally block throws an exception.

That also answers the first question - i.e., yes, you need the finally block to ensure your clean up code (in the finally block) will be executed even if the catch block (intentionally or otherwise) throws an Exception.

Anyway, make sure you post back when you know if you got the job or not! (If you don't, I hope it's just an Exception!... )
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

-- it is possible for an instance of the class to be serialized once, then deserialized many times, resulting in many instance being created.


The class has to implement Serializable interface for this to happen. If the constructor is protected you can subclass a singleton class and serialize it as the subclass can access the no arg constructor of the superclass. If the constructor is private no issues as you cannot subclass.


Groovy
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9345
    
    2

What happens if we clone the singleton instance?? I guess it is a good practice to override clone and throw an exception if an attempt to clone a singleton instance is made.


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
jerry vn
Greenhorn

Joined: Jun 30, 2010
Posts: 8
Jothi Shankar Kumar wrote:What happens if we clone the singleton instance?? I guess it is a good practice to override clone and throw an exception if an attempt to clone a singleton instance is made.


Hi everybody,

I uploaded one file. But It can not display. I used internet explorer 8.0.

How can I repair? Or some problems happened to IE8?

Rgs
Sandeep Awasthi
Ranch Hand

Joined: Oct 23, 2003
Posts: 597
Check url, check server log, right click and check view source, use debugger ...........
Anyway this is job discussion forum. Ask question in respective forum.


Sandeep
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: My interview questions
 
Similar Threads
Are my answers correct ?
Exceptions
Finally block
Exception
try/catch/finally..................